UserResource::getUsersForGroup()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 18
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Resource/UserResource.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Resource;
10
11
use App\DTO\RestDtoInterface;
12
use App\Entity\Interfaces\EntityInterface;
13
use App\Entity\User as Entity;
14
use App\Entity\UserGroup;
15
use App\Repository\UserRepository as Repository;
16
use App\Rest\RestResource;
17
use App\Security\RolesService;
18
use Throwable;
19
use function array_filter;
20
use function array_values;
21
use function in_array;
22
23
/**
24
 * Class UserResource
25
 *
26
 * @package App\Resource
27
 * @author TLe, Tarmo Leppänen <[email protected]>
28
 *
29
 * @psalm-suppress LessSpecificImplementedReturnType
30
 * @codingStandardsIgnoreStart
31
 *
32
 * @method Entity getReference(string $id)
33
 * @method Repository getRepository()
34
 * @method Entity[] find(?array $criteria = null, ?array $orderBy = null, ?int $limit = null, ?int $offset = null, ?array $search = null)
35
 * @method Entity|null findOne(string $id, ?bool $throwExceptionIfNotFound = null)
36
 * @method Entity|null findOneBy(array $criteria, ?array $orderBy = null, ?bool $throwExceptionIfNotFound = null)
37
 * @method Entity create(RestDtoInterface $dto, ?bool $flush = null, ?bool $skipValidation = null)
38
 * @method Entity update(string $id, RestDtoInterface $dto, ?bool $flush = null, ?bool $skipValidation = null)
39
 * @method Entity patch(string $id, RestDtoInterface $dto, ?bool $flush = null, ?bool $skipValidation = null)
40
 * @method Entity delete(string $id, ?bool $flush = null)
41
 * @method Entity save(EntityInterface $entity, ?bool $flush = null, ?bool $skipValidation = null)
42
 *
43
 * @codingStandardsIgnoreEnd
44
 */
45
class UserResource extends RestResource
46
{
47 664
    public function __construct(
48
        Repository $repository,
49
        private readonly RolesService $rolesService,
50
    ) {
51 664
        parent::__construct($repository);
52
    }
53
54
    /**
55
     * Method to fetch users for specified user group, note that this method will also check user role inheritance so
56
     * return value will contain all users that belong to specified user group via role inheritance.
57
     *
58
     * @return array<int, Entity>
59
     *
60
     * @throws Throwable
61
     */
62 6
    public function getUsersForGroup(UserGroup $userGroup): array
63
    {
64
        /**
65
         * Filter method to see if specified user belongs to certain user group.
66
         *
67
         * @param Entity $user
68
         *
69
         * @return bool
70
         */
71 6
        $filter = fn (Entity $user): bool => in_array(
72 6
            $userGroup->getRole()->getId(),
73 6
            $this->rolesService->getInheritedRoles($user->getRoles()),
74 6
            true
75 6
        );
76
77 6
        $users = $this->find();
78
79 6
        return array_values(array_filter($users, $filter));
80
    }
81
}
82