Lines
100.00%
20 / 20
Functions and Methods
100.00%
1 / 1
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Timeline | 100.00% | 20 / 20 | 100.00% | 1 / 1 | 4 | 100.00% | 1 / 1 | |||
| __invoke | 100.00% | 20 / 20 | 100.00% | 1 / 1 | 4 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace App\Controller\AgentStat; | |
| 6 | ||
| 7 | use App\Controller\BaseController; | |
| 8 | use App\Entity\TimelineEvent; | |
| 9 | use App\Enum\UserRole; | |
| 10 | use App\Form\TimelineEventType; | |
| 11 | use App\Repository\AgentStatRepository; | |
| 12 | use App\Repository\DoneBannerRepository; | |
| 13 | use App\Repository\TimelineEventRepository; | |
| 14 | use App\Timeline\TimelineBuilder; | |
| 15 | use App\Timeline\TimelineChart; | |
| 16 | use Doctrine\ORM\EntityManagerInterface; | |
| 17 | use Symfony\Component\HttpFoundation\Request; | |
| 18 | use Symfony\Component\HttpFoundation\Response; | |
| 19 | use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; | |
| 20 | use Symfony\Component\Routing\Attribute\Route; | |
| 21 | use Symfony\Component\Security\Http\Attribute\IsGranted; | |
| 22 | ||
| 23 | /** | |
| 24 | * The user's timeline, as the app's timeline screen: the chart, a form for a new event and every | |
| 25 | * import, custom event and Bannergress banner done with a day. With from and to (Y-m-d) the chart | |
| 26 | * is zoomed to those days; with chart=1 only the chart, for the date filter. | |
| 27 | */ | |
| 28 | #[Route('/agent-stats/timeline', name: 'app_agent_stat_timeline', methods: ['GET', 'POST'])] | |
| 29 | #[IsGranted(UserRole::USER->value)] | |
| 30 | final class Timeline extends BaseController | |
| 31 | { | |
| 32 | public function __invoke( | |
| 33 | Request $request, | |
| 34 | AgentStatRepository $agentStatRepository, | |
| 35 | TimelineEventRepository $timelineEventRepository, | |
| 36 | TimelineBuilder $timelineBuilder, | |
| 37 | DoneBannerRepository $doneBannerRepository, | |
| 38 | EntityManagerInterface $entityManager, | |
| 39 | #[MapQueryParameter(filter: \FILTER_VALIDATE_REGEXP, options: ['regexp' => TimelineChart::DAY])] | |
| 40 | ?string $from = null, | |
| 41 | #[MapQueryParameter(filter: \FILTER_VALIDATE_REGEXP, options: ['regexp' => TimelineChart::DAY])] | |
| 42 | ?string $to = null, | |
| 43 | #[MapQueryParameter] | |
| 44 | bool $chart = false, | |
| 45 | ): Response { | |
| 46 | $event = new TimelineEvent(); | |
| 47 | $event->date = new \DateTimeImmutable('today'); | |
| 48 | ||
| 49 | $form = $this->createForm(TimelineEventType::class, $event); | |
| 50 | $form->handleRequest($request); | |
| 51 | ||
| 52 | if ($form->isSubmitted() && $form->isValid()) { | |
| 53 | $event->owner = $this->getUser(); | |
| 54 | $entityManager->persist($event); | |
| 55 | $entityManager->flush(); | |
| 56 | ||
| 57 | $this->addFlash('success', 'timeline.event.added'); | |
| 58 | ||
| 59 | return $this->redirectToRoute('app_agent_stat_timeline', [], Response::HTTP_SEE_OTHER); | |
| 60 | } | |
| 61 | ||
| 62 | $agentStats = $agentStatRepository->findByOwner($this->getUser()); | |
| 63 | $events = $timelineEventRepository->findByOwner($this->getUser()); | |
| 64 | $timelineChart = TimelineChart::build($agentStats, $events, $request->getLocale(), TimelineChart::date($from), TimelineChart::date($to)); | |
| 65 | ||
| 66 | if ($chart) { | |
| 67 | return $this->render('agent_stat/_timeline_chart_frame.html.twig', ['chart' => $timelineChart]); | |
| 68 | } | |
| 69 | ||
| 70 | return $this->render('agent_stat/timeline.html.twig', [ | |
| 71 | 'timeline' => $timelineBuilder->build($agentStats, $events, $doneBannerRepository->findDatedByOwner($this->getUser())), | |
| 72 | 'chart' => $timelineChart, | |
| 73 | 'eventForm' => $form, | |
| 74 | ]); | |
| 75 | } | |
| 76 | } |