Completed
Push — master ( 83329d...934458 )
by Park Jong-Hun
03:08
created

DatabasePermissionManager::getAllPermission()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 16
rs 9.4285
1
<?php
2
3
namespace App\Auth\PermissionManager;
4
5
use App\Auth\PermissionManagerAbstract;
6
use App\Entity\Permission;
7
use App\Entity\Role;
8
use Core\Utils\ArrayUtils;
9
use Core\Utils\EntityUtils\EntitySelect;
10
use Doctrine\Common\Collections\Collection;
11
12
class DatabasePermissionManager extends PermissionManagerAbstract
13
{
14
    public function __construct(array $settings = [])
15
    {
16
    }
17
18
    /**
19
     * @return array|null
20
     */
21
    public function getRolesByOperation($operation)
22
    {
23
        $allPermission = $this->getAllPermission() ?: [];
24
        $operationKey = ArrayUtils::find($allPermission, $operation);
25
26
        if($operationKey === false) {
27
            return null;
28
        }
29
30
        return $allPermission[$operationKey];
31
    }
32
33
    private function getAllPermission()
34
    {
35
        $permissionRole = [];
36
        $permissions = EntitySelect::select(Permission::class)->findAll();
37
38
        if ($permissions === null) {
39
            return null;
40
        }
41
42
        /** @var Permission $permission */
43
        foreach ($permissions as $permission) {
44
            $permissionRole[$permission->getOperation()] = $this->getRoles($permission->getRoles());
45
        }
46
47
        return $permissionRole;
48
    }
49
50
    private function getRoles(Collection $roleCollection)
51
    {
52
        $roles = [];
53
54
        /** @var Role $role */
55
        foreach ($roleCollection as $role) {
56
            $roles[] = $role->getName();
57
        }
58
59
        return $roles;
60
    }
61
}
62