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 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace App\Controller\Api; | |
| 6 | ||
| 7 | use App\Controller\BaseController; | |
| 8 | use App\Dto\AgentStatInput; | |
| 9 | use App\Enum\UserRole; | |
| 10 | use App\Exception\StatsAlreadyAddedException; | |
| 11 | use App\Service\AgentStatService; | |
| 12 | use Symfony\Component\HttpFoundation\JsonResponse; | |
| 13 | use Symfony\Component\HttpFoundation\Response; | |
| 14 | use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; | |
| 15 | use Symfony\Component\Routing\Attribute\Route; | |
| 16 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
| 17 | use 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)] | |
| 21 | final 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 | } |