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

State   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 60%

Importance

Changes 6
Bugs 1 Features 2
Metric Value
wmc 20
c 6
b 1
f 2
cbo 1
dl 0
loc 190
ccs 24
cts 40
cp 0.6
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A isInitial() 0 4 1
A isFinal() 0 4 1
A isNormal() 0 4 1
A getType() 0 4 1
A addTransition() 0 8 2
A setTransitions() 0 6 2
A getTransitions() 0 4 1
A can() 0 8 2
A has() 0 4 1
A get() 0 4 2
A getProperties() 0 4 1
A getName() 0 4 1
A setProperties() 0 4 1
A getCallbacks() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace Finite\State;
4
5
use Finite\Transition\TransitionInterface;
6
7
/**
8
 * The base State class.
9
 * Feel free to extend it to fit to your needs.
10
 *
11
 * @author Yohan Giarelli <[email protected]>
12
 * @author Michal Dabrowski <[email protected]>
13
 */
14
class State implements StateInterface
15
{
16
    /**
17
     * The state type.
18
     *
19
     * @var int
20
     */
21
    protected $type;
22
23
    /**
24
     * The transition name.
25
     *
26
     * @var array
27
     */
28
    protected $transitions;
29
30
    /**
31
     * Callbacks of the state
32
     *
33
     * @var array
34
     */
35
    protected $callbacks;
36
37
    /**
38
     * The state name.
39
     *
40
     * @var string
41
     */
42 230
    protected $name;
43
44 230
    /**
45 230
     * @var array
46 230
     */
47 230
    protected $properties;
48 230
49
    /**
50
     * State constructor.
51
     *
52
     * @param $name
53
     * @param string $type
54
     * @param array $transitions
55
     * @param array $properties
56
     * @param array $callbacks
57
     */
58
    public function __construct(
59
        $name,
60
        $type = self::TYPE_NORMAL,
61
        array $transitions = [],
62
        array $properties = [],
63
        array $callbacks = []
64
    ) {
65
        $this->name = $name;
66
        $this->type = $type;
0 ignored issues
show
Documentation Bug introduced by
The property $type was declared of type integer, but $type is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
67
        $this->transitions = $transitions;
68
        $this->properties = $properties;
69
        $this->callbacks = $callbacks;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function isInitial()
76
    {
77 20
        return self::TYPE_INITIAL === $this->type;
78
    }
79 20
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function isFinal()
84
    {
85 190
        return self::TYPE_FINAL === $this->type;
86
    }
87 190
88 180
    /**
89 144
     * {@inheritdoc}
90
     */
91 190
    public function isNormal()
92 190
    {
93
        return self::TYPE_NORMAL === $this->type;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getType()
100
    {
101
        return $this->type;
102
    }
103
104
    /**
105
     * @param $transition
106
     */
107 70
    public function addTransition($transition)
108
    {
109 70
        if ($transition instanceof TransitionInterface) {
110
            $transition = $transition->getName();
111
        }
112
113
        $this->transitions[] = $transition;
114
    }
115
116
    /**
117
     * @param array $transitions
118
     */
119
    public function setTransitions(array $transitions)
120
    {
121
        foreach ($transitions as $transition) {
122
            $this->addTransition($transition);
123
        }
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 20
    public function getTransitions()
130
    {
131 20
        return $this->transitions;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     *
137 5
     * @deprecated Deprecated since version 1.0.0-BETA2. Use {@link StateMachine::can($transition)} instead.
138
     */
139 5
    public function can($transition)
140
    {
141
        if ($transition instanceof TransitionInterface) {
142
            $transition = $transition->getName();
143
        }
144
145 10
        return in_array($transition, $this->transitions);
146
    }
147 10
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function has($property)
152
    {
153 195
        return array_key_exists($property, $this->properties);
154
    }
155 195
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function get($property, $default = null)
160
    {
161
        return $this->has($property) ? $this->properties[$property] : $default;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function getProperties()
168
    {
169
        return $this->properties;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function getName()
176
    {
177
        return $this->name;
178
    }
179
180
    /**
181
     * @param array $properties
182
     */
183
    public function setProperties(array $properties)
184
    {
185
        $this->properties = $properties;
186
    }
187
188
    /**
189
     * @return array
190
     */
191
    public function getCallbacks()
192
    {
193
        return $this->callbacks;
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function __toString()
200
    {
201
        return $this->getName();
202
    }
203
}
204