Lines 100.00% 9 / 9
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AgentStatIndex 100.00% 9 / 9 100.00% 1 / 1 1 100.00% 1 / 1
 __invoke 100.00% 9 / 9 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace App\Controller\Api;
6
7use App\Controller\BaseController;
8use App\Dto\AgentStatInput;
9use App\Entity\AgentStat;
10use App\Enum\UserRole;
11use App\Repository\AgentStatRepository;
12use Symfony\Component\HttpFoundation\JsonResponse;
13use Symfony\Component\Routing\Attribute\Route;
14use Symfony\Component\Security\Http\Attribute\IsGranted;
15
16/**
17 * The user's agent stats, newest first, in the format of POST /api/agent-stats (plus the id).
18 */
19#[Route('/api/agent-stats', name: 'api_agent_stat_index', methods: ['GET'], format: 'json')]
20#[IsGranted(UserRole::USER->value)]
21final class AgentStatIndex extends BaseController
22{
23    public function __invoke(AgentStatRepository $agentStatRepository): JsonResponse
24    {
25        return $this->json(\array_map(
26            static fn (AgentStat $agentStat): array => [
27                'id' => $agentStat->id,
28                'datetime' => $agentStat->datetime->format(AgentStatInput::DATETIME_FORMAT),
29                'nickname' => $agentStat->nickname,
30                'faction' => $agentStat->faction,
31            ] + $agentStat->getStats(),
32            $agentStatRepository->findByOwner($this->getUser()),
33        ));
34    }
35}