Passed
Branch feature/deny (0fd688)
by Enea
02:29
created

PermissionEvaluator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 27
ccs 10
cts 12
cp 0.8333
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A evaluate() 0 7 3
A allWereDenied() 0 7 2
A existsInRoles() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Created on 07/03/18 by enea dhack.
7
 */
8
9
namespace Enea\Authorization\Drivers\Database;
10
11
use Enea\Authorization\Contracts\DeniableOwner;
12
use Enea\Authorization\Contracts\PermissionsOwner;
13
use Enea\Authorization\Contracts\RolesOwner;
14
15
class PermissionEvaluator extends Evaluator
16
{
17 7
    public function evaluate(PermissionsOwner $owner, array $permissions): bool
18
    {
19 7
        if ($this->allWereDenied($owner, $permissions)) {
20 3
            return false;
21
        }
22
23 7
        return $this->existsInRoles($owner, $permissions) || $this->has($owner->permissions()->getQuery())($permissions);
24
    }
25
26 7
    private function existsInRoles(PermissionsOwner $owner, array $permissions): bool
27
    {
28 7
        if ($owner instanceof RolesOwner) {
29 7
            return $owner->roles()->limit(1)->whereHas('permissions', $this->same($permissions))->exists();
30
        }
31
32
        return false;
33
    }
34
35 7
    private function allWereDenied(PermissionsOwner $owner, array $permissions): bool
36
    {
37 7
        if ($owner instanceof DeniableOwner) {
38 7
            return $owner->denied()->whereIn('secret_name', $permissions)->count('permissions.id') === count($permissions);
39
        }
40
41
        return false;
42
    }
43
}
44