Lines 100.00% 12 / 12
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Index 100.00% 12 / 12 100.00% 1 / 1 3 100.00% 1 / 1
 __invoke 100.00% 12 / 12 100.00% 1 / 1 3
1<?php
2
3declare(strict_types=1);
4
5namespace App\Controller\AgentStat;
6
7use App\ApiResource\Bannergress;
8use App\Controller\BaseController;
9use App\Entity\AgentStat;
10use App\Enum\UserRole;
11use App\Medal\MedalChecker;
12use App\Repository\AgentStatRepository;
13use App\Repository\DoneBannerRepository;
14use App\Repository\TimelineEventRepository;
15use App\Timeline\TimelineBuilder;
16use App\Timeline\TimelineEntry;
17use Symfony\Component\HttpFoundation\Response;
18use Symfony\Component\Routing\Attribute\Route;
19use Symfony\Component\Security\Http\Attribute\IsGranted;
20
21/**
22 * The user's latest stats with medals and custom medals, as in the app, what the app synced from
23 * Bannergress and the number of timeline entries of each kind, linking to the timeline (Timeline).
24 */
25#[Route('/agent-stats', name: 'app_agent_stat_index', methods: ['GET'])]
26#[IsGranted(UserRole::USER->value)]
27final class Index extends BaseController
28{
29    public function __invoke(
30        AgentStatRepository $agentStatRepository,
31        TimelineEventRepository $timelineEventRepository,
32        MedalChecker $medalChecker,
33        TimelineBuilder $timelineBuilder,
34        DoneBannerRepository $doneBannerRepository,
35    ): Response {
36        $agentStats = $agentStatRepository->findByOwner($this->getUser());
37        $events = $timelineEventRepository->findByOwner($this->getUser());
38        $latest = $agentStats[0] ?? null;
39
40        return $this->render('agent_stat/index.html.twig', [
41            'latest' => $latest,
42            'first' => \array_last($agentStats),
43            'medals' => $latest instanceof AgentStat ? $medalChecker->medalLevels($latest) : [],
44            'coreBadges' => $latest instanceof AgentStat ? $medalChecker->coreBadges($latest->getStats()['months_subscribed'] ?? 0) : [],
45            'customMedals' => $medalChecker->customMedalTiers($this->getUser()->getCustomMedals()),
46            'timelineCounts' => TimelineEntry::counts($timelineBuilder->build($agentStats, $events, $doneBannerRepository->findDatedByOwner($this->getUser()))),
47            'bannergress' => Bannergress::fromStored($this->getUser()->getBannergress()),
48        ]);
49    }
50}