Lines 100.00% 20 / 20
Methods 100.00% 1 / 1
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __invoke 100.00% 20 / 20 100.00% 1 / 1 4
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}