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
3declare(strict_types=1);
4
5namespace App\Controller\AgentStat;
6
7use App\Controller\BaseController;
8use App\Entity\TimelineEvent;
9use App\Enum\UserRole;
10use App\Form\TimelineEventType;
11use App\Repository\AgentStatRepository;
12use App\Repository\DoneBannerRepository;
13use App\Repository\TimelineEventRepository;
14use App\Timeline\TimelineBuilder;
15use App\Timeline\TimelineChart;
16use Doctrine\ORM\EntityManagerInterface;
17use Symfony\Component\HttpFoundation\Request;
18use Symfony\Component\HttpFoundation\Response;
19use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
20use Symfony\Component\Routing\Attribute\Route;
21use 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)]
30final 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}