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