Passed
Push — master ( 95c142...58f956 )
by Beau
02:43
created

GuardCollection::named()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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