Completed
Push — master ( 8ae0f5...03d31a )
by Oliver
07:06
created

SetupHelper::createNewEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Metabor\Statemachine\Util;
4
5
use Metabor\Statemachine\State;
6
use Metabor\Statemachine\StateCollection;
7
use Metabor\Statemachine\Transition;
8
use MetaborStd\Event\EventInterface;
9
use MetaborStd\Statemachine\ConditionInterface;
10
use MetaborStd\Statemachine\StateCollectionInterface;
11
use MetaborStd\Statemachine\StateInterface;
12
use MetaborStd\Statemachine\TransitionInterface;
13
14
/**
15
 * @author Oliver Tischlinger
16
 */
17
class SetupHelper
18
{
19
    /**
20
     * @var StateCollectionInterface
21
     */
22
    protected $stateCollection;
23
24
    /**
25
     * @param StateCollectionInterface $stateCollection
26
     */
27 17
    public function __construct(StateCollectionInterface $stateCollection)
28
    {
29 17
        $this->stateCollection = $stateCollection;
30 17
    }
31
32
    /**
33
     * @param string $name
34
     * @return StateInterface
35
     */
36 4
    protected function createNewState($name)
37
    {
38 4
        return new State($name);
39
    }
40
41
    /**
42
     * @param $name
43
     *
44
     * @return \MetaborStd\Statemachine\StateInterface
45
     *
46
     * @throws \Exception
47
     */
48 4
    public function findOrCreateState($name)
49
    {
50 4
        if (!$this->stateCollection->hasState($name)) {
51 4
            if ($this->stateCollection instanceof StateCollection) {
52 4
                $this->stateCollection->addState($this->createNewState($name));
53 4
            } else {
54
                throw new \InvalidArgumentException('Overwrite this method to implement a different type!');
55
            }
56 4
        }
57
58 4
        return $this->stateCollection->getState($name);
59
    }
60
61
    /**
62
     * @param StateInterface     $sourceState
63
     * @param StateInterface     $targetState
64
     * @param string             $eventName
65
     * @param ConditionInterface $condition
66
     *
67
     * @return TransitionInterface
68
     */
69 4
    protected function findTransition(StateInterface $sourceState, StateInterface $targetState, $eventName = null, ConditionInterface $condition = null)
70
    {
71 4
        $conditionName = $condition ? $condition->getName() : null;
72
        /* @var $transition TransitionInterface */
73 4
        foreach ($sourceState->getTransitions() as $transition) {
74 1
            $hasSameTargetState = ($transition->getTargetState() === $targetState);
75 1
            $hasSameCondition = ($transition->getConditionName() == $conditionName);
76 1
            $hasSameEvent = ($transition->getEventName() == $eventName);
77 1
            if ($hasSameTargetState && $hasSameCondition && $hasSameEvent) {
78
                return $transition;
79
            }
80 4
        }
81 4
    }
82
83
    /**
84
     * @param StateInterface      $sourceState
85
     * @param TransitionInterface $sourceTransition
86
     *
87
     * @throws \InvalidArgumentException
88
     */
89 4
    protected function addTransition(StateInterface $sourceState, TransitionInterface $sourceTransition)
90
    {
91 4
        if ($sourceState instanceof State) {
92 4
            $sourceState->addTransition($sourceTransition);
93 4
        } else {
94
            throw new \InvalidArgumentException('Overwrite this method to implement a different type!');
95
        }
96 4
    }
97
98
    /**
99
     * @param StateInterface     $sourceState
100
     * @param StateInterface     $targetState
101
     * @param string             $eventName
102
     * @param ConditionInterface $condition
103
     *
104
     * @return TransitionInterface
105
     */
106 4
    public function createNewTransition(StateInterface $sourceState, StateInterface $targetState, $eventName = null, ConditionInterface $condition = null)
0 ignored issues
show
Unused Code introduced by
The parameter $sourceState is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
    {
108 4
        return new Transition($targetState, $eventName, $condition);
109
    }
110
111
    /**
112
     * @param string             $sourceStateName
113
     * @param string             $targetStateName
114
     * @param string             $eventName
115
     * @param ConditionInterface $condition
116
     *
117
     * @return TransitionInterface
118
     */
119 4
    public function findOrCreateTransition($sourceStateName, $targetStateName, $eventName = null, ConditionInterface $condition = null)
120
    {
121 4
        $sourceState = $this->findOrCreateState($sourceStateName);
122 4
        $targetState = $this->findOrCreateState($targetStateName);
123 4
        $transition = $this->findTransition($sourceState, $targetState, $eventName, $condition);
124 4
        if (!$transition) {
125 4
            $transition = $this->createNewTransition($sourceState, $targetState, $eventName, $condition);
126 4
            $this->addTransition($sourceState, $transition);
127 4
        }
128
129 4
        return $transition;
130
    }
131
132
    /**
133
     * @param StateInterface     $sourceState
134
     * @param string             $eventName
135
     *
136
     * @return EventInterface
137
     */
138
    public function createNewEvent(StateInterface $sourceState, $eventName)
139
    {
140
        if ($sourceState instanceof State) {
141
            return $sourceState->getEvent($eventName);
142
        } else {
143
            throw new \InvalidArgumentException('Overwrite this method to implement a different type!');
144
        }
145
    }
146
147
    /**
148
     * @param string $sourceStateName
149
     * @param string $eventName
150
     * @return EventInterface
151
     */
152
    public function findOrCreateEvent($sourceStateName, $eventName)
153
    {
154
        $sourceState = $this->findOrCreateState($sourceStateName);
155
        if ($sourceState->hasEvent($eventName)) {
156
            return $sourceState->getEvent($eventName);
157
        } else {
158
            return $this->createNewEvent($sourceState, $eventName);
159
        }
160
    }
161
162
    /**
163
     * If there is no Transition from the SourceState with this Event use addCommandAndSelfTransition().
164
     *
165
     * @param $sourceStateName
166
     * @param string  $eventName
167
     * @param \SplObserver $command
168
     */
169
    public function addCommand($sourceStateName, $eventName, \SplObserver $command)
170
    {
171
        $this->findOrCreateEvent($sourceStateName, $eventName)->attach($command);
172
    }
173
174
    /**
175
     * @param $sourceStateName
176
     * @param string  $eventName
177
     * @param \SplObserver $command
178
     */
179
    public function addCommandAndSelfTransition($sourceStateName, $eventName, \SplObserver $command)
180
    {
181
        $this->addCommand($sourceStateName, $eventName, $command);
182
        $this->findOrCreateTransition($sourceStateName, $sourceStateName, $eventName);
183
    }
184
}
185