Passed
Push — master ( c69ced...32a2e1 )
by Derek Stephen
08:53 queued 07:11
created

PassportControl   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 105
ccs 51
cts 57
cp 0.8947
rs 10
wmc 20

10 Methods

Rating   Name   Duplication   Size   Complexity  
A isAuthorized() 0 21 5
A __construct() 0 4 1
A hasPassportRole() 0 9 4
A findUserPassport() 0 7 1
A createNewRole() 0 6 1
A findPassportRoles() 0 5 1
A revokeEntitlement() 0 6 1
A grantEntitlement() 0 18 4
A removeRole() 0 4 1
A findRole() 0 4 1
1
<?php
2
3
namespace Del\Passport;
4
5
use Del\Passport\Passport;
6
use Del\Passport\Entity\PassportRole;
7
use Del\Passport\Entity\Role;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\Common\Collections\Collection;
10
use Doctrine\ORM\EntityManager;
11
12
class PassportControl
13
{
14 3
    public function __construct(
15
        private EntityManager $entityManager
16
    )
17 3
    {}
18
19 1
    public function isAuthorized(PassportInterface $passport, ResourceInterface $resource, string $roleName): bool
20
    {
21 1
        $entitlements = $passport->getEntitlements();
22
23 1
        foreach ($entitlements as $passportRole) {
24 1
            $role = $passportRole->getRole();
25 1
            codecept_debug($role->getRoleName());
26 1
            codecept_debug($passportRole->getEntityId());
27 1
            codecept_debug('resource ' . $resource->getResourceId());
28 1
            codecept_debug($resource->getResourceType());
29
30 1
            if ($role->getRoleName() === $roleName
31 1
                && $role->getClass() === $resource->getResourceType()
32 1
                && $passportRole->getEntityId() === $resource->getResourceId()
33
            ) {
34 1
                return true;
35
            }
36
        }
37
38
39 1
        return false;
40
    }
41
42 1
    public function grantEntitlement(
43
        PassportInterface $passport,
44
        RoleInterface $role,
45
        ResourceInterface $resource = null,
46
        int $approvedBy = null,
47
    ): bool {
48 1
        $userId = $passport->getUserId();
49 1
        $resource ? $resourceId = $resource->getResourceId() : null;
50 1
        $entitlement = new PassportRole();
51 1
        $entitlement->setUserId($userId);
52 1
        $entitlement->setRole($role);
53 1
        $resourceId ? $entitlement->setEntityId($resourceId) : null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $resourceId does not seem to be defined for all execution paths leading up to this point.
Loading history...
54 1
        $approvedBy ? $entitlement->setApprovedById($approvedBy) : null;
55 1
        $this->entityManager->persist($entitlement);
56 1
        $this->entityManager->flush();
57 1
        $passport->getEntitlements()->add($entitlement);
58
59 1
        return true;
60
    }
61
62 1
    public function revokeEntitlement(PassportRole $passportRole): bool
63
    {
64 1
        $this->entityManager->remove($passportRole);
65 1
        $this->entityManager->flush();
66
67 1
        return true;
68
    }
69
70 2
    public function createNewRole(RoleInterface $role): RoleInterface
71
    {
72 2
        $this->entityManager->persist($role);
73 2
        $this->entityManager->flush();
74
75 2
        return $role;
76
    }
77
78 2
    public function removeRole(RoleInterface $role): void
79
    {
80 2
        $this->entityManager->remove($role);
81 2
        $this->entityManager->flush();
82
    }
83
84 1
    public function findRole(string $name): ?Role
85
    {
86 1
        return $this->entityManager->getRepository(Role::class)->findOneBy([
87 1
            'roleName' => $name,
88 1
        ]);
89
    }
90
91
    public function findPassportRoles(Role $role, int $entityId): array
92
    {
93
        return $this->entityManager->getRepository(PassportRole::class)->findBy([
94
            'role' => $role,
95
            'entityId' => $entityId,
96
        ]);
97
    }
98
99 1
    public function findUserPassport(int $userId): Passport
100
    {
101 1
        $roles = $this->entityManager->getRepository(PassportRole::class)->findBy([
102 1
            'userId' => $userId,
103 1
        ]);
104
105 1
        return new Passport($userId, new ArrayCollection($roles));
106
    }
107
108 1
    public function hasPassportRole(PassportInterface $passport, string $roleName, ?int $entityId = null): bool
109
    {
110 1
        foreach ($passport->getEntitlements() as $entitlement) {
111 1
            if ($entitlement->getRole()->getRoleName() === $roleName && $entitlement->getEntityId() === $entityId) {
112 1
                return true;
113
            }
114
        }
115
116
        return false;
117
    }
118
}
119