From 9264170d0bdf8637d6d5c6c69dae278218f9e488 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Mon, 12 Dec 2016 01:26:27 +0300 Subject: [PATCH] Fix #26. Top users page fixed. --- .../Controller/UserController.php | 4 +-- .../PointToolsBundle/DTO/TopUserDTO.php | 20 +++++++++++++-- .../Repository/UserRepository.php | 25 ++++++++++++++++--- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php index 0d92a98..5212074 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php @@ -73,8 +73,8 @@ class UserController extends Controller // Preparing chart data foreach ($topUsers as $user) { - $chartData['titles'][] = $user->login; - $chartData['subscribers'][] = $user->subscribersCount; + $chartData['titles'][] = $user->getLogin(); + $chartData['subscribers'][] = $user->getSubscribersCount(); } // Chart diff --git a/src/Skobkin/Bundle/PointToolsBundle/DTO/TopUserDTO.php b/src/Skobkin/Bundle/PointToolsBundle/DTO/TopUserDTO.php index ca1f126..ac62e9c 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/DTO/TopUserDTO.php +++ b/src/Skobkin/Bundle/PointToolsBundle/DTO/TopUserDTO.php @@ -10,16 +10,32 @@ class TopUserDTO /** * @var string */ - public $login; + private $login; /** * @var int */ - public $subscribersCount; + private $subscribersCount; public function __construct($login, $subscribersCount) { $this->login = $login; $this->subscribersCount = $subscribersCount; } + + /** + * @return string + */ + public function getLogin() + { + return $this->login; + } + + /** + * @return int + */ + public function getSubscribersCount() + { + return $this->subscribersCount; + } } \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Repository/UserRepository.php b/src/Skobkin/Bundle/PointToolsBundle/Repository/UserRepository.php index 3cd4122..7bdaf11 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Repository/UserRepository.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Repository/UserRepository.php @@ -87,6 +87,10 @@ class UserRepository extends EntityRepository } /** + * Returns top users by subscribers count + * + * @param int $limit + * * @return TopUserDTO[] */ public function getTopUsers($limit = 30) @@ -95,16 +99,29 @@ class UserRepository extends EntityRepository throw new \InvalidArgumentException('$limit must be an integer'); } - // TODO: refactor query $qb = $this->getEntityManager()->getRepository('SkobkinPointToolsBundle:Subscription')->createQueryBuilder('s'); - return $qb - ->select(['COUNT(s.subscriber) as cnt', 'NEW Skobkin\Bundle\PointToolsBundle\DTO\TopUserDTO(a.login, COUNT(s.subscriber))']) + $rows = $qb + ->select([ + 'NEW Skobkin\Bundle\PointToolsBundle\DTO\TopUserDTO(a.login, COUNT(s.subscriber))', + 'COUNT(s.subscriber) as subscribers_count' + ]) ->innerJoin('s.author', 'a') - ->orderBy('cnt', 'desc') + ->orderBy('subscribers_count', 'desc') ->groupBy('a.id') ->setMaxResults($limit) ->getQuery()->getResult() ; + + $result = []; + + // Removing subscribers_count element, saving TopUserDTO + // @todo remove crutches, refactor query + foreach ($rows as $row) { + unset($row['subscribers_count']); + $result[] = reset($row); + } + + return $result; } } \ No newline at end of file