Completed
Push — master ( ad6910...d77fda )
by Tarmo
18s queued 12s
created

UserResource   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 34
ccs 9
cts 9
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getUsersForGroup() 0 18 1
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 228
    public function __construct(
48
        protected Repository $repository,
49
        private RolesService $rolesService,
50
    ) {
51 228
    }
52
53
    /**
54
     * Method to fetch users for specified user group, note that this method will also check user role inheritance so
55
     * return value will contain all users that belong to specified user group via role inheritance.
56
     *
57
     * @return array<int, Entity>
58
     *
59
     * @throws Throwable
60
     */
61 5
    public function getUsersForGroup(UserGroup $userGroup): array
62
    {
63
        /**
64
         * Filter method to see if specified user belongs to certain user group.
65
         *
66
         * @param Entity $user
67
         *
68
         * @return bool
69
         */
70 5
        $filter = fn (Entity $user): bool => in_array(
71 5
            $userGroup->getRole()->getId(),
72 5
            $this->rolesService->getInheritedRoles($user->getRoles()),
73 5
            true
74
        );
75
76 5
        $users = $this->find();
77
78 5
        return array_values(array_filter($users, $filter));
79
    }
80
}
81