Lines 100.00% 17 / 17
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AgentStatCreate 100.00% 17 / 17 100.00% 1 / 1 2 100.00% 1 / 1
 __invoke 100.00% 17 / 17 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace App\Controller\Api;
6
7use App\Controller\BaseController;
8use App\Dto\AgentStatInput;
9use App\Enum\UserRole;
10use App\Exception\StatsAlreadyAddedException;
11use App\Service\AgentStatService;
12use Symfony\Component\HttpFoundation\JsonResponse;
13use Symfony\Component\HttpFoundation\Response;
14use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
15use Symfony\Component\Routing\Attribute\Route;
16use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17use Symfony\Component\Security\Http\Attribute\IsGranted;
18
19#[Route('/api/agent-stats', name: 'api_agent_stat_create', methods: ['POST'], format: 'json')]
20#[IsGranted(UserRole::USER->value)]
21final class AgentStatCreate extends BaseController
22{
23    public function __invoke(
24        #[MapRequestPayload(acceptFormat: 'json')] AgentStatInput $input,
25        AgentStatService $agentStatService,
26    ): JsonResponse {
27        try {
28            $agentStat = $agentStatService->create($this->getUser(), $input);
29        } catch (StatsAlreadyAddedException $statsAlreadyAddedException) {
30            return $this->json(['error' => $statsAlreadyAddedException->getMessage()], Response::HTTP_CONFLICT);
31        }
32
33        return $this->json(
34            [
35                'id' => $agentStat->id,
36                'createdAt' => $agentStat->createdAt->format(\DATE_ATOM),
37            ],
38            Response::HTTP_CREATED,
39            [
40                'Location' => $this->generateUrl(
41                    'app_agent_stat_show',
42                    ['id' => $agentStat->id],
43                    UrlGeneratorInterface::ABSOLUTE_URL
44                ),
45            ],
46        );
47    }
48}