Completed
Pull Request — master (#124)
by
unknown
04:50 queued 01:06
created

Transition   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 3

Test Coverage

Coverage 64.15%

Importance

Changes 9
Bugs 3 Features 2
Metric Value
wmc 21
c 9
b 3
f 2
cbo 3
dl 0
loc 186
ccs 34
cts 53
cp 0.6415
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 4
A addInitialState() 0 8 2
A getInitialStates() 0 4 1
A getState() 0 4 1
A process() 0 3 1
A getName() 0 4 1
A getGuard() 0 4 1
A resolveProperties() 0 18 3
A has() 0 4 1
A get() 0 6 2
A getProperties() 0 15 2
A getCallbacks() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace Finite\Transition;
4
5
use Finite\Exception\TransitionException;
6
use Finite\StateMachine\StateMachineInterface;
7
use Finite\State\StateInterface;
8
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
9
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
12
/**
13
 * The base Transition class.
14
 * Feel free to extend it to fit to your needs.
15
 *
16
 * @author Yohan Giarelli <[email protected]>
17
 * @author Michal Dabrowski <[email protected]>
18
 */
19
class Transition implements PropertiesAwareTransitionInterface
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $initialStates;
25
26
    /*
27
     * @var string
28
     */
29
    protected $state;
30
31
    /**
32
     * @var string
33
     */
34
    protected $name;
35
36
    /**
37
     * @var callable
38
     */
39
    protected $guard;
40
41
    /**
42
     * @var OptionsResolver
43
     */
44
    protected $propertiesOptionsResolver;
45
46
    /**
47
     * @var array
48
     */
49
    protected $callbacks;
50
51
    /**
52
     * @param string          $name
53 210
     * @param string|array    $initialStates
54
     * @param string          $state
55
     * @param callable|null   $guard
56
     * @param OptionsResolver $propertiesOptionsResolver
57
     * @param array           $callbacks
58
     */
59
    public function __construct(
60 210
        $name,
61
        $initialStates,
62
        $state,
63
        $guard = null,
64 210
        OptionsResolver $propertiesOptionsResolver = null,
65 210
        array $callbacks = []
66 210
    ) {
67 210
        if (null !== $guard && !is_callable($guard)) {
68 210
            throw new \InvalidArgumentException('Invalid callable guard argument passed to Transition::__construct().');
69 210
        }
70
71
        $this->name = $name;
72
        $this->state = $state;
73
        $this->initialStates = (array) $initialStates;
74
        $this->guard = $guard;
75
        $this->propertiesOptionsResolver = $propertiesOptionsResolver ?: new OptionsResolver();
76
        $this->callbacks = $callbacks;
77
    }
78
79
    /**
80
     * @param string|StateInterface $state
81
     */
82
    public function addInitialState($state)
83
    {
84
        if ($state instanceof StateInterface) {
85
            $state = $state->getName();
86 165
        }
87
88 165
        $this->initialStates[] = $state;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 180
    public function getInitialStates()
95
    {
96 180
        return $this->initialStates;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 20
    public function getState()
103
    {
104 20
        return $this->state;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109 180
     */
110
    public function process(StateMachineInterface $stateMachine)
111 180
    {
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 45
    public function getName()
118
    {
119 45
        return $this->name;
120
    }
121
122
    /**
123
     * @return callable|null
124
     */
125 65
    public function getGuard()
126
    {
127
        return $this->guard;
128 65
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function resolveProperties(array $properties)
134
    {
135
        try {
136
            return $this->propertiesOptionsResolver->resolve($properties);
137
        } catch (MissingOptionsException $e) {
138
            throw new TransitionException(
139
                'Testing or applying this transition need a parameter. Provide it or set it optional.',
140
                $e->getCode(),
141
                $e
142
            );
143
        } catch (UndefinedOptionsException $e) {
144
            throw new TransitionException(
145
                'You provided an unknown property to test() or apply(). Remove it or declare it in your graph.',
146
                $e->getCode(),
147 10
                $e
148
            );
149 10
        }
150
    }
151
152
    /**
153
     * {@inheritDoc}
154
     */
155 10
    public function has($property)
156
    {
157 10
        return array_key_exists($property, $this->getProperties());
158
    }
159 10
160
    /**
161
     * {@inheritDoc}
162
     */
163
    public function get($property, $default = null)
164
    {
165 10
        $properties = $this->getProperties();
166
167 10
        return $this->has($property) ? $properties[$property] : $default;
168
    }
169 10
170 5
    /**
171
     * {@inheritDoc}
172
     */
173 5
    public function getProperties()
174
    {
175 5
        $missingOptions = $this->propertiesOptionsResolver->getMissingOptions();
176 5
177 4
        if (0 === count($missingOptions)) {
178 4
            return $this->propertiesOptionsResolver->resolve(array());
179
        }
180
181
        $options = array_combine($missingOptions, array_fill(0, count($missingOptions), null));
182
183
        return array_diff_key(
184
            $this->propertiesOptionsResolver->resolve($options),
185
            array_combine($missingOptions, $missingOptions)
186
        );
187
    }
188
189
    /**
190
     * @return array
191
     */
192
    public function getCallbacks()
193
    {
194
        return $this->callbacks;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function __toString()
201
    {
202
        return $this->getName();
203
    }
204
}
205