Lines 100.00% 17 / 17
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
UserPrivacyProvider 100.00% 17 / 17 100.00% 2 / 2 2 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 provide 100.00% 16 / 16 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace App\State;
6
7use ApiPlatform\Metadata\Operation;
8use ApiPlatform\State\ProviderInterface;
9use App\ApiResource\UserPrivacy;
10use App\Entity\User;
11use App\Enum\ProfilePart;
12use App\Repository\AgentStatRepository;
13use App\Repository\UserRepository;
14
15/**
16 * GET /api/users: every user, by agent name (users without one last, by identifier).
17 *
18 * @implements ProviderInterface<UserPrivacy>
19 */
20final readonly class UserPrivacyProvider implements ProviderInterface
21{
22    public function __construct(
23        private UserRepository $userRepository,
24        private AgentStatRepository $agentStatRepository,
25    ) {
26    }
27
28    /**
29     * @return list<UserPrivacy>
30     */
31    public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
32    {
33        $users = $this->userRepository->findAllByAgentName();
34        $latest = $this->agentStatRepository->findLatestByOwners($users);
35
36        return \array_values(\array_map(static function (User $user) use ($latest): UserPrivacy {
37            $stat = $latest[(int) $user->id] ?? null;
38
39            return new UserPrivacy(
40                (int) $user->id,
41                $user->getIdentifier(),
42                $user->getRole()->value,
43                $user->getAgentName(),
44                $stat?->faction,
45                $stat?->level,
46                $user->getAvatar(),
47                \array_map(static fn (ProfilePart $part): string => $part->value, $user->getVisibleParts()),
48                $user->getVisibilityChosenAt() instanceof \DateTimeImmutable,
49            );
50        }, $users));
51    }
52}