Passed
Pull Request — master (#5329)
by Angel Fernando Quiroz
08:20
created

UserStateProvider::provide()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 3
dl 0
loc 21
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\State;
6
7
use ApiPlatform\Doctrine\Orm\Paginator;
8
use ApiPlatform\Doctrine\Orm\State\CollectionProvider;
9
use ApiPlatform\Doctrine\Orm\State\ItemProvider;
10
use ApiPlatform\Metadata\CollectionOperationInterface;
11
use ApiPlatform\Metadata\Operation;
12
use ApiPlatform\State\ProviderInterface;
13
use Chamilo\CoreBundle\Component\Utils\NameConvention;
14
use Chamilo\CoreBundle\Entity\User;
15
16
/**
17
 * @template-implements ProviderInterface<User>
18
 */
19
final class UserStateProvider implements ProviderInterface
20
{
21
    public function __construct(
22
        private readonly ItemProvider $itemProvider,
23
        private readonly CollectionProvider $collectionProvider,
24
        private readonly NameConvention $nameConvention
25
    ) {}
26
27
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
28
    {
29
        if ($operation instanceof CollectionOperationInterface) {
30
            $providedUsers = $this->collectionProvider->provide($operation, $uriVariables, $context);
31
32
            assert($providedUsers instanceof Paginator);
33
34
            /** @var User $user */
35
            foreach ($providedUsers as $user) {
36
                $this->nameConvention->getPersonName($user);
37
            }
38
39
            return $providedUsers;
40
        }
41
42
        /** @var User $user */
43
        $user = $this->itemProvider->provide($operation, $uriVariables, $context);
44
45
        $this->nameConvention->getPersonName($user);
46
47
        return $user;
48
    }
49
}
50