RbacAccessChecker::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Potievdev\SlimRbac\Component;
4
5
use Doctrine\ORM\Query\QueryException;
6
use Potievdev\SlimRbac\Models\RepositoryRegistry;
7
8
class RbacAccessChecker
9
{
10
    /** @var  RepositoryRegistry $repositoryRegistry */
11
    private $repositoryRegistry;
12
13
    /**
14
     * @param RepositoryRegistry $repositoryRegistry
15
     */
16
    public function __construct(RepositoryRegistry $repositoryRegistry)
17
    {
18
        $this->repositoryRegistry = $repositoryRegistry;
19
    }
20
21
    /**
22
     * Checks access status.
23
     *
24
     * @throws QueryException
25
     */
26
    public function hasAccess(string $userId, string $permissionName): bool
27
    {
28
        /** @var integer $permissionId */
29
        $permissionId = $this->repositoryRegistry
30
            ->getPermissionRepository()
31
            ->getPermissionIdByName($permissionName);
32
33
        if ($permissionId === null) {
0 ignored issues
show
introduced by
The condition $permissionId === null is always false.
Loading history...
34
            return false;
35
        }
36
37
        /** @var integer[] $rootRoleIds */
38
        $rootRoleIds = $this->repositoryRegistry
39
            ->getUserRoleRepository()
40
            ->getUserRoleIds($userId);
41
42
        if (count($rootRoleIds) == 0) {
43
            return false;
44
        }
45
46
        $allRoleIds = $this->repositoryRegistry
47
            ->getRoleHierarchyRepository()
48
            ->getAllRoleIdsHierarchy($rootRoleIds);
49
50
        return $this->repositoryRegistry
51
            ->getRolePermissionRepository()
52
            ->isPermissionAssigned($permissionId, $allRoleIds);
53
    }
54
}