AutorizableTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 45
rs 10
c 1
b 0
f 0
ccs 18
cts 18
cp 1
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A canAny() 0 9 3
A canAll() 0 9 3
A can() 0 3 1
A detach() 0 3 1
A attach() 0 3 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\GRBAC;
11
12
trait AutorizableTrait
13
{
14
    /**
15
     * @var array<string, RoleInterface>
16
     */
17
    protected $roles = [];
18
19 4
    public function can(string $slug): bool
20
    {
21 4
        return $this->isAllowed($slug);
22
    }
23
24 1
    public function canAny(array $slugs): bool
25
    {
26 1
        foreach ($slugs as $slug) {
27 1
            if ($this->isAllowed($slug)) {
28 1
                return true;
29
            }
30
        }
31
32 1
        return false;
33
    }
34
35 1
    public function canAll(array $slugs): bool
36
    {
37 1
        foreach ($slugs as $slug) {
38 1
            if (!$this->isAllowed($slug)) {
39 1
                return false;
40
            }
41
        }
42
43 1
        return true;
44
    }
45
46 5
    public function attach(RoleInterface $role): void
47
    {
48 5
        $this->roles[$role->name()] = $role;
49 5
    }
50
51 5
    public function detach(RoleInterface $role): void
52
    {
53 5
        unset($this->roles[$role->name()]);
54 5
    }
55
56
    abstract protected function isAllowed(string $slug): bool;
57
}
58