Lines 85.71% 6 / 7
Functions and Methods 50.00% 1 / 2
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AgentStatProcessor 85.71% 6 / 7 50.00% 1 / 2 3.03 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 process 83.33% 5 / 6 0.00% 0 / 1 2.02
1<?php
2
3declare(strict_types=1);
4
5namespace App\State;
6
7use ApiPlatform\Metadata\Operation;
8use ApiPlatform\State\ProcessorInterface;
9use App\Dto\AgentStatInput;
10use App\Entity\AgentStat;
11use App\Entity\User;
12use App\Notifier\ChatActivity;
13use App\Service\AgentStatService;
14use Symfony\Bundle\SecurityBundle\Security;
15
16/**
17 * POST /api/agent-stats: saves the validated input for the current user and posts it to Slack and Telegram.
18 * A StatsAlreadyAddedException is a 409, an AgentNameException a 422 (see AgentStat's #[ApiResource]).
19 *
20 * @implements ProcessorInterface<AgentStatInput, AgentStat>
21 */
22final readonly class AgentStatProcessor implements ProcessorInterface
23{
24    public function __construct(
25        private AgentStatService $agentStatService,
26        private Security $security,
27        private ChatActivity $chatActivity,
28    ) {
29    }
30
31    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): AgentStat
32    {
33        $user = $this->security->getUser();
34        if (!$user instanceof User) {
35            throw new \LogicException('The operation requires ROLE_USER.');
36        }
37
38        $agentStat = $this->agentStatService->create($user, $data);
39        $this->chatActivity->statsUploaded($agentStat);
40
41        return $agentStat;
42    }
43}