GuardCollection::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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