From c87192a1f28c201886e1c6c00aa199ab9732c69e Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Tue, 23 Jun 2015 13:34:53 +0300 Subject: [PATCH] More refactoring. --- .../Controller/UserController.php | 59 +++---------------- .../Entity/SubscriptionEventRepository.php | 23 ++++++++ .../Entity/SubscriptionRepository.php | 21 +++++++ .../Entity/UserRepository.php | 40 +++++++++++++ 4 files changed, 91 insertions(+), 52 deletions(-) diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php index d294de5..22d2e12 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php @@ -2,9 +2,7 @@ namespace Skobkin\Bundle\PointToolsBundle\Controller; -use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\EntityManager; -use Skobkin\Bundle\PointToolsBundle\Entity\TopUserDTO; use Skobkin\Bundle\PointToolsBundle\Entity\User; use Skobkin\Bundle\PointToolsBundle\Service\UserApi; use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -17,16 +15,11 @@ class UserController extends Controller */ public function showAction($login) { - /** @var QueryBuilder $qb */ - $qb = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:User')->createQueryBuilder('u'); + /** @var EntityManager $em */ + $em = $this->getDoctrine()->getManager(); - $user = $qb - ->select('u') - ->where('LOWER(u.login) = LOWER(:login)') - ->setMaxResults(1) - ->setParameter('login', $login) - ->getQuery()->getOneOrNullResult() - ; + /** @var User $user */ + $user = $em->getRepository('SkobkinPointToolsBundle:User')->findUserByLogin($login); if (!$user) { throw $this->createNotFoundException('User ' . $login . ' not found.'); @@ -34,56 +27,18 @@ class UserController extends Controller $userApi = $this->container->get('skobkin_point_tools.api_user'); - $qb = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:User')->createQueryBuilder('u'); - - $subscribers = $qb - ->select('u') - ->innerJoin('u.subscriptions', 's') - ->where('s.author = :author') - ->orderBy('u.login', 'asc') - ->setParameter('author', $user->getId()) - ->getQuery()->getResult() - ; - - $qb = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->createQueryBuilder('se'); - - $subscriptionsEvents = $qb - ->select() - ->where('se.author = :author') - ->orderBy('se.date', 'desc') - ->setMaxResults(10) - ->setParameter('author', $user) - ->getQuery()->getResult() - ; - return $this->render('SkobkinPointToolsBundle:User:show.html.twig', [ 'user' => $user, - 'subscribers' => $subscribers, - 'log' => $subscriptionsEvents, + 'subscribers' => $em->getRepository('SkobkinPointToolsBundle:User')->findUserSubscribersById($user->getId()), + 'log' => $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->getUserLastSubscriptionEventsById($user, 10), 'avatar_url' => $userApi->getAvatarUrl($user, UserApi::AVATAR_SIZE_LARGE), ]); } public function topAction() { - /** @var EntityManager $em */ - $em = $this->getDoctrine()->getManager(); - - /** @var QueryBuilder $qb */ - $qb = $em->getRepository('SkobkinPointToolsBundle:Subscription')->createQueryBuilder('s'); - - /** @var TopUserDTO[] $topUsers */ - $topUsers = $qb - ->select(['COUNT(s.subscriber) as cnt', 'NEW SkobkinPointToolsBundle:TopUserDTO(a.login, COUNT(s.subscriber))']) - ->innerJoin('s.author', 'a') - ->orderBy('cnt', 'desc') - ->groupBy('a.id') - ->setMaxResults(30) - ->getQuery()->getResult() - ; - return $this->render('@SkobkinPointTools/User/top.html.twig', [ - 'top_users' => $topUsers + 'top_users' => $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:Subscription')->getTopUsers(), ]); } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEventRepository.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEventRepository.php index 47b6f27..8937a90 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEventRepository.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEventRepository.php @@ -22,4 +22,27 @@ class SubscriptionEventRepository extends EntityRepository ->getQuery()->getSingleScalarResult() ; } + + /** + * @param User $user + * @param integer $limit + * @return SubscriptionEvent[] + */ + public function getUserLastSubscriptionEventsById(User $user, $limit) + { + if (!is_int($limit)) { + throw new \InvalidArgumentException('$limit must be an integer'); + } + + $qb = $this->createQueryBuilder('se'); + + return $qb + ->select() + ->where('se.author = :author') + ->orderBy('se.date', 'desc') + ->setMaxResults($limit) + ->setParameter('author', $user) + ->getQuery()->getResult() + ; + } } \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionRepository.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionRepository.php index 5684c03..1dcd55f 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionRepository.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionRepository.php @@ -12,6 +12,10 @@ class SubscriptionRepository extends EntityRepository */ public function getUserSubscribersCountById($id) { + if (!is_int($id)) { + throw new \InvalidArgumentException('$id must be an integer'); + } + $qb = $this->createQueryBuilder('s'); return $qb ->select('COUNT(s)') @@ -21,4 +25,21 @@ class SubscriptionRepository extends EntityRepository ->getQuery()->getSingleScalarResult() ; } + + /** + * @return TopUserDTO[] + */ + public function getTopUsers() + { + $qb = $this->createQueryBuilder('s'); + + return $qb + ->select(['COUNT(s.subscriber) as cnt', 'NEW SkobkinPointToolsBundle:TopUserDTO(a.login, COUNT(s.subscriber))']) + ->innerJoin('s.author', 'a') + ->orderBy('cnt', 'desc') + ->groupBy('a.id') + ->setMaxResults(30) + ->getQuery()->getResult() + ; + } } \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRepository.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRepository.php index f5a736a..ed2dd2b 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRepository.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRepository.php @@ -6,6 +6,24 @@ use Doctrine\ORM\EntityRepository; class UserRepository extends EntityRepository { + /** + * @param string $login + * @return User[] + * @throws \Doctrine\ORM\NonUniqueResultException + */ + public function findUserByLogin($login) + { + $qb = $this->createQueryBuilder('u'); + + return $qb + ->select('u') + ->where('LOWER(u.login) = LOWER(:login)') + ->setMaxResults(1) + ->setParameter('login', $login) + ->getQuery()->getOneOrNullResult() + ; + } + /** * @return integer */ @@ -15,4 +33,26 @@ class UserRepository extends EntityRepository return $qb->select('COUNT(u)')->getQuery()->getSingleScalarResult(); } + + /** + * @param integer $id + * @return User[] + */ + public function findUserSubscribersById($id) + { + if (!is_int($id)) { + throw new \InvalidArgumentException('$id must be an integer'); + } + + $qb = $this->createQueryBuilder('u'); + + return $qb + ->select('u') + ->innerJoin('u.subscriptions', 's') + ->where('s.author = :author') + ->orderBy('u.login', 'asc') + ->setParameter('author', $id) + ->getQuery()->getResult() + ; + } } \ No newline at end of file