Transition::getState()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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
     * @param string          $name
48
     * @param string|array    $initialStates
49
     * @param string          $state
50
     * @param callable|null   $guard
51
     * @param OptionsResolver $propertiesOptionsResolver
52
     */
53 215
    public function __construct(
54
        $name,
55
        $initialStates,
56
        $state,
57
        $guard = null,
58
        OptionsResolver $propertiesOptionsResolver = null
59
    ) {
60 215
        if (null !== $guard && !is_callable($guard)) {
61
            throw new \InvalidArgumentException('Invalid callable guard argument passed to Transition::__construct().');
62
        }
63
64 215
        $this->name = $name;
65 215
        $this->state = $state;
66 215
        $this->initialStates = (array) $initialStates;
67 215
        $this->guard = $guard;
68 215
        $this->propertiesOptionsResolver = $propertiesOptionsResolver ?: new OptionsResolver();
69 215
    }
70
71
    /**
72
     * @param string|StateInterface $state
73
     */
74
    public function addInitialState($state)
75
    {
76
        if ($state instanceof StateInterface) {
77
            $state = $state->getName();
78
        }
79
80
        $this->initialStates[] = $state;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 170
    public function getInitialStates()
87
    {
88 170
        return $this->initialStates;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 185
    public function getState()
95
    {
96 185
        return $this->state;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 20
    public function process(StateMachineInterface $stateMachine)
103
    {
104 20
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 185
    public function getName()
110
    {
111 185
        return $this->name;
112
    }
113
114
    /**
115
     * @return callable|null
116
     */
117 45
    public function getGuard()
118
    {
119 45
        return $this->guard;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 65
    public function resolveProperties(array $properties)
126
    {
127
        try {
128 65
            return $this->propertiesOptionsResolver->resolve($properties);
129
        } catch (MissingOptionsException $e) {
130
            throw new TransitionException(
131
                'Testing or applying this transition need a parameter. Provide it or set it optional.',
132
                $e->getCode(),
133
                $e
134
            );
135
        } catch (UndefinedOptionsException $e) {
136
            throw new TransitionException(
137
                'You provided an unknown property to test() or apply(). Remove it or declare it in your graph.',
138
                $e->getCode(),
139
                $e
140
            );
141
        }
142
    }
143
144
    /**
145
     * {@inheritDoc}
146
     */
147 10
    public function has($property)
148
    {
149 10
        return array_key_exists($property, $this->getProperties());
150
    }
151
152
    /**
153
     * {@inheritDoc}
154
     */
155 10
    public function get($property, $default = null)
156
    {
157 10
        $properties = $this->getProperties();
158
159 10
        return $this->has($property) ? $properties[$property] : $default;
160
    }
161
162
    /**
163
     * {@inheritDoc}
164
     */
165 10
    public function getProperties()
166
    {
167 10
        $missingOptions = $this->propertiesOptionsResolver->getMissingOptions();
168
169 10
        if (0 === count($missingOptions)) {
170 5
            return $this->propertiesOptionsResolver->resolve(array());
171
        }
172
173 5
        $options = array_combine($missingOptions, array_fill(0, count($missingOptions), null));
174
175 5
        return array_diff_key(
176 5
            $this->propertiesOptionsResolver->resolve($options),
177 5
            array_combine($missingOptions, $missingOptions)
178 3
        );
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    public function __toString()
185
    {
186
        return $this->getName();
187
    }
188
}
189