From 6384527720a86123eea3fa315ee4b0f28798baaf Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Tue, 23 Jun 2015 12:38:43 +0300 Subject: [PATCH] Some refactoring. --- .../Controller/MainController.php | 34 ++----------------- .../PointToolsBundle/Entity/Subscription.php | 2 +- .../Entity/SubscriptionEvent.php | 2 +- .../Entity/SubscriptionEventRepository.php | 25 ++++++++++++++ .../Entity/SubscriptionRepository.php | 24 +++++++++++++ .../Bundle/PointToolsBundle/Entity/User.php | 2 +- .../Entity/UserRepository.php | 18 ++++++++++ 7 files changed, 73 insertions(+), 34 deletions(-) create mode 100644 src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEventRepository.php create mode 100644 src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionRepository.php create mode 100644 src/Skobkin/Bundle/PointToolsBundle/Entity/UserRepository.php diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/MainController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/MainController.php index 876a6d4..b821e17 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/MainController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/MainController.php @@ -13,38 +13,10 @@ class MainController extends Controller /** @var EntityManager $em */ $em = $this->getDoctrine()->getManager(); - /** @var QueryBuilder $qb */ - $qb = $em->getRepository('SkobkinPointToolsBundle:User')->createQueryBuilder('u'); - - // All users in the system count - $usersCount = $qb->select('COUNT(u)')->getQuery()->getSingleScalarResult(); - - $qb = $em->getRepository('SkobkinPointToolsBundle:Subscription')->createQueryBuilder('s'); - - // Service subscribers count - $subscribersCount = $qb - ->select('COUNT(s)') - ->innerJoin('s.author', 'a') - ->where('a.login = :login') - ->setParameter('login', $this->container->getParameter('point_login')) - ->getQuery()->getSingleScalarResult() - ; - - $qb = $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->createQueryBuilder('se'); - - $now = new \DateTime(); - - $eventsCount = $qb - ->select('COUNT(se)') - ->where('se.date > :time') - ->setParameter('time', $now->sub(new \DateInterval('PT24H'))) - ->getQuery()->getSingleScalarResult() - ; - return $this->render('SkobkinPointToolsBundle:Main:index.html.twig', [ - 'users_count' => $usersCount, - 'subscribers_count' => $subscribersCount, - 'events_count' => $eventsCount, + 'users_count' => $em->getRepository('SkobkinPointToolsBundle:User')->getUsersCount(), + 'subscribers_count' => $em->getRepository('SkobkinPointToolsBundle:Subscription')->getUserSubscribersCountById($this->container->getParameter('point_id')), + 'events_count' => $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->getLastDayEventsCount(), 'service_login' => $this->container->getParameter('point_login'), ]); } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php index 03b18a3..588c5e4 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php @@ -10,7 +10,7 @@ use Doctrine\ORM\Mapping as ORM; * @ORM\Table(name="subscriptions.subscriptions", uniqueConstraints={ * @ORM\UniqueConstraint(name="subscription_unique", columns={"author_id", "subscriber_id"})} * ) - * @ORM\Entity + * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionRepository") */ class Subscription { diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php index 2b98686..1c1c4a2 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php @@ -12,7 +12,7 @@ use Doctrine\ORM\Mapping as ORM; * @ORM\Index(name="subscriber_idx", columns={"subscriber_id"}), * @ORM\Index(name="date_idx", columns={"date"}) * }) - * @ORM\Entity + * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEventRepository") * @ORM\HasLifecycleCallbacks */ class SubscriptionEvent diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEventRepository.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEventRepository.php new file mode 100644 index 0000000..c22c588 --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEventRepository.php @@ -0,0 +1,25 @@ +createQueryBuilder('se'); + + $now = new \DateTime(); + + $eventsCount = $qb + ->select('COUNT(se)') + ->where('se.date > :time') + ->setParameter('time', $now->sub(new \DateInterval('PT24H'))) + ->getQuery()->getSingleScalarResult() + ; + } +} \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionRepository.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionRepository.php new file mode 100644 index 0000000..5684c03 --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionRepository.php @@ -0,0 +1,24 @@ +createQueryBuilder('s'); + return $qb + ->select('COUNT(s)') + ->innerJoin('s.author', 'a') + ->where('a.id = :id') + ->setParameter('id', $id) + ->getQuery()->getSingleScalarResult() + ; + } +} \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php index 051f9be..35ee886 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php @@ -9,7 +9,7 @@ use Doctrine\ORM\Mapping as ORM; * User * * @ORM\Table(name="users.users") - * @ORM\Entity + * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Entity\UserRepository") * @ORM\HasLifecycleCallbacks */ class User diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRepository.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRepository.php new file mode 100644 index 0000000..f5a736a --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRepository.php @@ -0,0 +1,18 @@ +createQueryBuilder('u'); + + return $qb->select('COUNT(u)')->getQuery()->getSingleScalarResult(); + } +} \ No newline at end of file