AutorizableTrait::canAny()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 3
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