RoleHierarchyRepository   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
eloc 22
c 1
b 0
f 1
dl 0
loc 77
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllChildRoleIds() 0 10 2
A getAllRoleIdsHierarchy() 0 5 1
A getChildIds() 0 11 1
A hasChildRoleId() 0 20 5
1
<?php
2
3
namespace Potievdev\SlimRbac\Models\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\Query\QueryException;
7
8
/**
9
 * UserRoleRepository
10
 *
11
 * This class was generated by the Doctrine ORM. Add your own custom
12
 * repository methods below.
13
 */
14
class RoleHierarchyRepository extends EntityRepository
15
{
16
    /**
17
     * Finding child identifier in roles three where $parentRoleId is in the top of three
18
     * @throws QueryException
19
     */
20
    public function hasChildRoleId(int $parentRoleId, int $findingChildId): bool
21
    {
22
        $childIds = $this->getChildIds([$parentRoleId]);
23
24
        if (count($childIds) > 0) {
25
26
            if (in_array($findingChildId, $childIds)) {
27
                return true;
28
            }
29
30
            foreach ($childIds as $childId) {
31
32
                if ($this->hasChildRoleId($childId, $findingChildId) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
33
                    return true;
34
                }
35
36
            }
37
        }
38
39
        return false;
40
    }
41
42
    /**
43
     * @param integer[] $rootRoleIds
44
     * @return integer[]
45
     * @throws QueryException
46
     */
47
    public function getAllRoleIdsHierarchy(array $rootRoleIds): array
48
    {
49
        $childRoleIds = $this->getAllChildRoleIds($rootRoleIds);
50
51
        return array_merge($rootRoleIds, $childRoleIds);
52
    }
53
54
    /**
55
     * Returns all hierarchically child role ids for given parent role ids.
56
     *
57
     * @param integer[] $parentIds
58
     * @return integer[]
59
     * @throws QueryException
60
     */
61
    private function getAllChildRoleIds(array $parentIds): array
62
    {
63
        $allChildIds = [];
64
65
        while (count($parentIds) > 0) {
66
            $parentIds = $this->getChildIds($parentIds);
67
            $allChildIds = array_merge($allChildIds, $parentIds);
68
        };
69
70
        return $allChildIds;
71
    }
72
73
    /**
74
     * Returns array of child role ids for given parent role ids.
75
     *
76
     * @param integer[] $parentIds
77
     * @return integer[]
78
     * @throws QueryException
79
     */
80
    private function getChildIds(array $parentIds): array
81
    {
82
        $qb = $this->createQueryBuilder('roleHierarchy');
83
84
        $qb->select('roleHierarchy.childRoleId')
85
            ->where($qb->expr()->in( 'roleHierarchy.parentRoleId', $parentIds))
86
            ->indexBy('roleHierarchy', 'roleHierarchy.childRoleId');
87
88
        $childRoleIds =  $qb->getQuery()->getArrayResult();
89
90
        return array_keys($childRoleIds);
91
    }
92
}
93