Passed
Push — master ( 73bfc2...a878a7 )
by Adrien
13:45 queued 10:48
created

MultipleRoles   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 62
ccs 15
cts 17
cp 0.8824
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 3 1
A __construct() 0 4 2
A getRoles() 0 3 1
A __toString() 0 3 1
A addRole() 0 6 1
A has() 0 3 1
A getRoleId() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Acl;
6
7
use ArrayIterator;
8
use Exception;
9
use IteratorAggregate;
10
use Laminas\Permissions\Acl\Role\RoleInterface;
11
use Stringable;
12
use Traversable;
13
14
/**
15
 * A role containing multiple roles.
16
 */
17
class MultipleRoles implements IteratorAggregate, RoleInterface, Stringable
18
{
19
    /**
20
     * @var string[]
21
     */
22
    private array $roles = [];
23
24
    /**
25
     * @param string[] $roles
26
     */
27 5
    public function __construct(array $roles = [])
28
    {
29 5
        foreach ($roles as $role) {
30 4
            $this->addRole($role);
31
        }
32
    }
33
34
    /**
35
     * Add a role to the list.
36
     */
37 4
    public function addRole(string $role): void
38
    {
39 4
        $this->roles[] = $role;
40
41 4
        $this->roles = array_unique($this->roles);
42 4
        sort($this->roles);
43
    }
44
45 1
    public function getRoleId(): never
46
    {
47 1
        throw new Exception('This should never be called. If it is, then it means this class is not used correctly');
48
    }
49
50
    /**
51
     * Return the role list.
52
     *
53
     * @return string[]
54
     */
55 4
    public function getRoles(): array
56
    {
57 4
        return $this->roles;
58
    }
59
60
    /**
61
     * Return whether at least one of the given role is included in the list.
62
     */
63 1
    public function has(string ...$roles): bool
64
    {
65 1
        return (bool) array_intersect($this->roles, $roles);
66
    }
67
68 2
    public function __toString(): string
69
    {
70 2
        return '[' . implode(', ', $this->roles) . ']';
71
    }
72
73
    /**
74
     * @return Traversable<int, string>
75
     */
76
    public function getIterator(): Traversable
77
    {
78
        return new ArrayIterator($this->roles);
79
    }
80
}
81