Lines 93.33% 14 / 15
Functions and Methods 50.00% 1 / 2
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
MeProvider 93.33% 14 / 15 50.00% 1 / 2 4.00 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 provide 92.85% 13 / 14 0.00% 0 / 1 3.00
1<?php
2
3declare(strict_types=1);
4
5namespace App\State;
6
7use ApiPlatform\Metadata\Operation;
8use ApiPlatform\State\ProviderInterface;
9use App\ApiResource\Bannergress;
10use App\ApiResource\CustomMedals;
11use App\ApiResource\Me;
12use App\Entity\User;
13use Symfony\Bundle\SecurityBundle\Security;
14
15/**
16 * @implements ProviderInterface<Me>
17 */
18final readonly class MeProvider implements ProviderInterface
19{
20    public function __construct(private Security $security)
21    {
22    }
23
24    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?Me
25    {
26        $user = $this->security->getUser();
27        if (!$user instanceof User || null === $user->id) {
28            return null;
29        }
30
31        return new Me(
32            $user->id,
33            $user->getIdentifier(),
34            $user->getRole()->value,
35            $user->getAgentName(),
36            CustomMedals::fromArray($user->getCustomMedals()),
37            $user->getVisibleParts(),
38            $user->getVisibilityChosenAt() instanceof \DateTimeImmutable,
39            $user->getAvatar(),
40            Bannergress::fromStored($user->getBannergress()),
41        );
42    }
43}