Ported UserController. Fixed PhpDoc type hinting in SubscriptionEventRepository and UserRepository.

This commit is contained in:
Alexey Skobkin 2023-04-01 21:00:16 +03:00
parent 77fa05f457
commit 1393e2d53f
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
3 changed files with 21 additions and 27 deletions

View file

@ -1,32 +1,24 @@
<?php <?php
declare(strict_types=1);
namespace src\PointToolsBundle\Controller; namespace App\Controller;
use App\DTO\{TopUserDTO, DailyEventsDTO};
use Knp\Component\Pager\PaginatorInterface; use Knp\Component\Pager\PaginatorInterface;
use src\PointToolsBundle\DTO\{TopUserDTO};
use src\PointToolsBundle\DTO\DailyEvents;
use src\PointToolsBundle\Entity\User;
use src\PointToolsBundle\Repository\UserRenameEventRepository;
use src\PointToolsBundle\Repository\UserRepository;
use src\PointToolsBundle\Repository\{SubscriptionEventRepository};
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Ob\HighchartsBundle\Highcharts\Highchart; use Ob\HighchartsBundle\Highcharts\Highchart;
use App\Entity\User;
use App\Repository\{SubscriptionEventRepository, UserRenameEventRepository, UserRepository};
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{Request, Response}; use Symfony\Component\HttpFoundation\{Request, Response};
use Symfony\Component\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
class UserController extends AbstractController class UserController extends AbstractController
{ {
/** @var TranslatorInterface */ public function __construct(
private $translator; private readonly TranslatorInterface $translator,
) {
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
} }
/**
* @param string $login
*/
public function showAction( public function showAction(
Request $request, Request $request,
string $login, string $login,
@ -68,16 +60,16 @@ class UserController extends AbstractController
} }
/** /**
* @param DailyEventsDTO[] $eventsByDay
*@todo move to the service *@todo move to the service
* *
* @param DailyEvents[] $eventsByDay
*/ */
private function createEventsDynamicChart(array $eventsByDay = []): Highchart private function createEventsDynamicChart(array $eventsByDay = []): Highchart
{ {
$data = []; $data = [];
foreach ($eventsByDay as $dailyEvents) { foreach ($eventsByDay as $dailyEvents) {
$data[$dailyEvents->getDate()->format('d.m')] = $dailyEvents->getEventsCount(); $data[$dailyEvents->date->format('d.m')] = $dailyEvents->eventsCount;
} }
return $this->createChart('eventschart', 'line', $data, 'Events by day', 'amount'); return $this->createChart('eventschart', 'line', $data, 'Events by day', 'amount');
@ -93,7 +85,7 @@ class UserController extends AbstractController
$data = []; $data = [];
foreach ($topUsers as $topUser) { foreach ($topUsers as $topUser) {
$data[$topUser->getLogin()] = $topUser->getSubscribersCount(); $data[$topUser->login] = $topUser->subscribersCount;
} }
return $this->createChart('topchart', 'bar', $data, 'Top users', 'amount'); return $this->createChart('topchart', 'bar', $data, 'Top users', 'amount');

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Repository; namespace App\Repository;
use App\DTO\DailyEventsDTO;
use App\Entity\SubscriptionEvent; use App\Entity\SubscriptionEvent;
use App\Entity\User; use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
@ -101,14 +102,14 @@ class SubscriptionEventRepository extends ServiceEntityRepository
return $qb->getQuery()->getResult(); return $qb->getQuery()->getResult();
} }
/** @return SubscriptionEvent[] */ /** @return DailyEventsDTO[] */
public function getLastEventsByDay(int $days = 30): array public function getLastEventsByDay(int $days = 30): array
{ {
$qb = $this->createQueryBuilder('se'); $qb = $this->createQueryBuilder('se');
$rows = $qb $rows = $qb
->select([ ->select([
'NEW Skobkin\Bundle\PointToolsBundle\DTO\DailyEvents(DAY(se.date), COUNT(se))', 'NEW App\DTO\DailyEventsDTO(DAY(se.date), COUNT(se))',
'DAY(se.date) as day', 'DAY(se.date) as day',
]) ])
->groupBy('day') ->groupBy('day')

View file

@ -1,9 +1,10 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace App\Repository; namespace App\Repository;
use App\DTO\TopUserDTO;
use App\Entity\Subscription;
use App\Entity\User; use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
@ -114,11 +115,11 @@ class UserRepository extends ServiceEntityRepository
*/ */
public function getTopUsers(int $limit = 30): array public function getTopUsers(int $limit = 30): array
{ {
$qb = $this->getEntityManager()->getRepository('SkobkinPointToolsBundle:Subscription')->createQueryBuilder('s'); $qb = $this->getEntityManager()->getRepository(Subscription::class)->createQueryBuilder('s');
$rows = $qb $rows = $qb
->select([ ->select([
'NEW Skobkin\Bundle\PointToolsBundle\DTO\TopUserDTO(a.login, COUNT(s.subscriber))', 'NEW App\DTO\TopUserDTO(a.login, COUNT(s.subscriber))',
'COUNT(s.subscriber) as subscribers_count' 'COUNT(s.subscriber) as subscribers_count'
]) ])
->innerJoin('s.author', 'a') ->innerJoin('s.author', 'a')