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

GroupMembersStateProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 2
A __construct() 0 3 1
A provide() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chamilo\CoreBundle\State;
6
7
use ApiPlatform\Metadata\Operation;
8
use ApiPlatform\State\ProviderInterface;
9
use Chamilo\CoreBundle\Entity\Usergroup;
10
use Doctrine\ORM\EntityManagerInterface;
11
12
/**
13
 * @template-implements ProviderInterface<array<int, Usergroup>>
14
 */
15
final class GroupMembersStateProvider implements ProviderInterface
16
{
17
    private EntityManagerInterface $entityManager;
18
19
    public function __construct(EntityManagerInterface $entityManager)
20
    {
21
        $this->entityManager = $entityManager;
22
    }
23
24
    public function supports(Operation $operation, array $uriVariables = [], array $context = []): bool
25
    {
26
        return Usergroup::class === $operation->getClass() && 'get_group_members' === $operation->getName();
27
    }
28
29
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable
30
    {
31
        $groupId = $uriVariables['id'] ?? null;
32
33
        if (null === $groupId) {
34
            return [];
35
        }
36
37
        $usergroupRepository = $this->entityManager->getRepository(Usergroup::class);
38
39
        return $usergroupRepository->getUsersByGroup((int) $groupId);
40
    }
41
}
42