GuardCollection   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 6
eloc 11
dl 0
loc 34
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 1
A __construct() 0 4 2
A cannot() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dflydev\FiniteStateMachine\Guard;
6
7
use Dflydev\FiniteStateMachine\State\State;
8
use Dflydev\FiniteStateMachine\Transition\Transition;
9
10
class GuardCollection
11
{
12
    /**
13
     * @var array<int, Guard>
14
     */
15
    private array $guards = [];
16
17
    /**
18
     * @var array<string, Guard>
19
     */
20
    private array $guardsByName = [];
21
22 21
    public function __construct(Guard ...$guards)
23
    {
24 21
        foreach ($guards as $guard) {
25
            $this->add($guard);
26
        }
27 21
    }
28
29 21
    public function add(Guard $guard): void
30
    {
31 21
        $this->guards[] = $guard;
32 21
        $this->guardsByName[$guard->name()] = $guard;
33 21
    }
34
35 13
    public function cannot(object $object, Transition $transition, State $fromState, State $toState): bool
36
    {
37 13
        foreach ($this->guards as $guard) {
38 13
            if ($guard->cannot($object, $transition, $fromState, $toState)) {
39 2
                return true;
40
            }
41
        }
42
43 11
        return false;
44
    }
45
}
46