Passed
Pull Request — master (#19)
by Yannick
02:27
created

ManyToOneTransition::isAllowed()   A

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
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Star\Component\State\Transitions;
4
5
use Star\Component\State\RegistryBuilder;
6
use Star\Component\State\StateRegistry;
7
use Star\Component\State\StateTransition;
8
use Star\Component\State\StateVisitor;
9
use Star\Component\State\TransitionVisitor;
10
use Webmozart\Assert\Assert;
11
12
final class ManyToOneTransition implements StateTransition
13
{
14
    /**
15
     * @var string[]
16
     */
17
    private $fromStates;
18
19
    /**
20
     * @var string
21
     */
22
    private $to;
23
24
    /**
25
     * @param string[] $fromStates
26
     * @param string $to
27
     */
28 21
    public function __construct(array $fromStates, $to)
29
    {
30 21
        Assert::greaterThanEq(count($fromStates), 1, 'Expected at least %2$s state. Got: %s');
31 21
        Assert::allString($fromStates);
32 21
        Assert::string($to);
33 21
        $this->fromStates = $fromStates;
34 21
        $this->to = $to;
35 21
    }
36
37
    /**
38
     * @param string $state
39
     *
40
     * @return bool
41
     */
42 4
    public function isAllowed($state)
43
    {
44 4
        Assert::string($state);
45 4
        return in_array($state, $this->fromStates, true);
46
    }
47
48
    /**
49
     * @param RegistryBuilder $registry
50
     */
51 18
    public function onRegister(RegistryBuilder $registry)
52
    {
53 18
        foreach ($this->fromStates as $from) {
54 18
            $registry->registerState($from, []);
55
        }
56
57 18
        $registry->registerState($this->to, []);
58 18
    }
59
60 3
    public function getDestinationState()
61
    {
62 3
        return $this->to;
63
    }
64
65
    /**
66
     * @param TransitionVisitor $visitor
67
     */
68 2
    public function acceptTransitionVisitor(TransitionVisitor $visitor)
69
    {
70 2
        foreach ($this->fromStates as $from) {
71 2
            $visitor->visitFromState($from);
72
        }
73
74 2
        $visitor->visitToState($this->to);
75 2
    }
76
77
    /**
78
     * @param StateVisitor $visitor
79
     * @param StateRegistry $registry
80
     */
81 1
    public function acceptStateVisitor(StateVisitor $visitor, StateRegistry $registry)
82
    {
83 1
        foreach ($this->fromStates as $from) {
84 1
            $registry->getState($from)->acceptStateVisitor($visitor);
85
        }
86
87 1
        $registry->getState($this->to)->acceptStateVisitor($visitor);
88 1
    }
89
}
90