GuardCollection::cannot()   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
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 4
crap 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