TransitionHelper   B
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 328
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 38
c 5
b 0
f 0
lcom 1
cbo 8
dl 0
loc 328
rs 8.3999

17 Methods

Rating   Name   Duplication   Size   Complexity  
A setFactory() 0 4 1
A setTranslator() 0 4 1
A getTranslationState() 0 4 1
A getTranslationTransition() 0 4 1
A getTransition() 0 4 1
A isState() 0 4 1
C __construct() 0 26 8
A getFunctions() 0 12 1
A getTranslation() 0 8 2
A getTransitionColor() 0 7 2
A isNegativeColor() 0 8 2
A isPositiveColor() 0 8 2
A getState() 0 8 2
A getStateColor() 0 11 3
A getConstant() 0 6 1
A getTransitions() 0 13 3
B getPosibleTransitions() 0 33 6
1
<?php
2
3
namespace DoS\ResourceBundle\Twig\Extension;
4
5
use DoS\ResourceBundle\Model\StatableInterface;
6
use SM\Factory\Factory;
7
use Symfony\Component\PropertyAccess\PropertyAccess;
8
use Symfony\Component\Translation\TranslatorInterface;
9
10
abstract class TransitionHelper extends \Twig_Extension
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $options = array(
16
        'state' => array(
17
            'translation' => null,
18
            'default_color' => 'black',
19
            'negative_colors' => array('red'),
20
            'positive_colors' => array('green'),
21
        ),
22
        'transition' => array(
23
            'translation' => null,
24
            'default_color' => 'black',
25
            'negative_colors' => array('red'),
26
            'positive_colors' => array('green'),
27
        ),
28
    );
29
30
    /**
31
     * @var array
32
     */
33
    protected $colors = array();
34
35
    /**
36
     * @var array
37
     */
38
    protected $negativeColors = array();
39
40
    /**
41
     * @var array
42
     */
43
    protected $positiveColors = array();
44
45
    /**
46
     * @var Factory
47
     */
48
    protected $factory;
49
50
    /**
51
     * @var TranslatorInterface
52
     */
53
    protected $translator;
54
55
    /**
56
     * @param array $options
57
     */
58
    public function __construct(array $options = array())
59
    {
60
        $this->options = array_replace_recursive($this->options, $options);
61
62
        foreach ($this->options as $type => $options) {
63
            if (!empty($options['colors'])) {
64
                foreach ($options['colors'] as $color => $states) {
65
                    if (!is_array($states)) {
66
                        $states = (array) $states;
67
                    }
68
69
                    foreach ($states as $state) {
70
                        $this->colors[$type][$state] = $color;
71
                    }
72
                }
73
            }
74
75
            if (!empty($options['negative_colors'])) {
76
                $this->negativeColors[$type] = $options['negative_colors'];
77
            }
78
79
            if (!empty($options['positive_colors'])) {
80
                $this->positiveColors[$type] = $options['positive_colors'];
81
            }
82
        }
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getFunctions()
89
    {
90
        return array(
91
            new \Twig_SimpleFunction('ts_is', array($this, 'isState')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
92
            new \Twig_SimpleFunction('ts_state', array($this, 'getState')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
93
            new \Twig_SimpleFunction('ts_color', array($this, 'getStateColor')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
94
            new \Twig_SimpleFunction('ts_color_t', array($this, 'getTransitionColor')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
95
            new \Twig_SimpleFunction('ts_transitions', array($this, 'getPosibleTransitions')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
96
            new \Twig_SimpleFunction('ts_trans_s', array($this, 'getTranslationState')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
97
            new \Twig_SimpleFunction('ts_trans_t', array($this, 'getTranslationTransition')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
98
        );
99
    }
100
101
    /**
102
     * @param Factory $factory
103
     */
104
    public function setFactory(Factory $factory)
105
    {
106
        $this->factory = $factory;
107
    }
108
109
    /**
110
     * @param TranslatorInterface $translator
111
     */
112
    public function setTranslator(TranslatorInterface $translator)
113
    {
114
        $this->translator = $translator;
115
    }
116
117
    /**
118
     * @param string|StatableInterface $key
119
     * @param string $type
120
     * @return string
121
     */
122
    public function getTranslation($key, $type = 'state')
123
    {
124
        if (strtolower($type) === 'state') {
125
            return $this->translator->trans($this->getState($key));
126
        }
127
128
        return $this->translator->trans($this->getTransition($key));
0 ignored issues
show
Bug introduced by
It seems like $key defined by parameter $key on line 122 can also be of type object<DoS\ResourceBundl...odel\StatableInterface>; however, DoS\ResourceBundle\Twig\...Helper::getTransition() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
129
    }
130
131
    /**
132
     * @param string|StatableInterface $keyOrObject
133
     *
134
     * @return string
135
     */
136
    public function getTranslationState($keyOrObject)
137
    {
138
        return $this->getTranslation($keyOrObject, 'state');
139
    }
140
141
    /**
142
     * @param $key
143
     *
144
     * @return string
145
     */
146
    public function getTranslationTransition($key)
147
    {
148
        return $this->getTranslation($key, 'transition');
149
    }
150
151
    /**
152
     * Get transition translator key.
153
     *
154
     * @param string $transition
155
     *
156
     * @return string
157
     */
158
    public function getTransition($transition)
159
    {
160
        return $this->options['transition']['translation'].'.'.$transition;
161
    }
162
163
    /**
164
     * Get transition color.
165
     *
166
     * @param string $transition
167
     *
168
     * @return string
169
     */
170
    public function getTransitionColor($transition)
171
    {
172
        return empty($this->colors['transition'][$transition])
173
            ? $this->options['transition']['default_color']
174
            : $this->colors['transition'][$transition]
175
        ;
176
    }
177
178
    /**
179
     * Check color is negative?
180
     *
181
     * @param $color
182
     * @param $type
183
     *
184
     * @return bool
185
     */
186
    public function isNegativeColor($color, $type)
187
    {
188
        if (empty($this->negativeColors[$type])) {
189
            return false;
190
        }
191
192
        return in_array($color, $this->negativeColors[$type]);
193
    }
194
195
    /**
196
     * Check color is positive?
197
     *
198
     * @param $color
199
     * @param $type
200
     *
201
     * @return bool
202
     */
203
    public function isPositiveColor($color, $type)
204
    {
205
        if (empty($this->positiveColors[$type])) {
206
            return false;
207
        }
208
209
        return in_array($color, $this->positiveColors[$type]);
210
    }
211
212
    /**
213
     * Get state translator key.
214
     *
215
     * @param string|object $state
216
     *
217
     * @return string
218
     */
219
    public function getState($state)
220
    {
221
        if ($state instanceof StatableInterface) {
222
            $state = $state->getState();
223
        }
224
225
        return $this->options['state']['translation'].'.'.$state;
226
    }
227
228
    /**
229
     * Get state color.
230
     *
231
     * @param string|object $state
232
     *
233
     * @return string
234
     */
235
    public function getStateColor($state)
236
    {
237
        if ($state instanceof StatableInterface) {
238
            $state = $state->getState();
239
        }
240
241
        return empty($this->colors['state'][$state])
242
            ? $this->options['state']['default_color']
243
            : $this->colors['state'][$state]
244
        ;
245
    }
246
247
    /**
248
     * @param string $class Class name
249
     * @param string $name  Constant name
250
     *
251
     * @return mixed
252
     */
253
    protected function getConstant($class, $name)
254
    {
255
        $cls = new \ReflectionClass($class);
256
257
        return $cls->getConstant($name);
258
    }
259
260
    /**
261
     * @param $class
262
     * @param string $prefix
263
     *
264
     * @return array
265
     * @deprecated implement StatableInterface in client class.
266
     */
267
    protected function getTransitions($class, $prefix = 'TS')
268
    {
269
        $cls = new \ReflectionClass($class);
270
271
        $transitions = array();
272
        foreach ($cls->getConstants() as $const => $value) {
273
            if (preg_match(sprintf('/^%s_.*/', $prefix), $const)) {
274
                $transitions[$const] = $value;
275
            }
276
        }
277
278
        return $transitions;
279
    }
280
281
    /**
282
     * Get all of posible transitions.
283
     *
284
     * @param StatableInterface $object
285
     * @param string $objectIdentifier
286
     *
287
     * @return array|null
288
     *
289
     * @throws \SM\SMException
290
     */
291
    public function getPosibleTransitions(StatableInterface $object =  null, $objectIdentifier = 'id')
292
    {
293
        if (empty($object)) {
294
            return;
295
        }
296
297
        $sm = $this->factory->get($object, $object->getStateGraph());
298
299
        if (empty($sm)) {
300
            return;
301
        }
302
303
        $tasks = array();
304
        $accessor = PropertyAccess::createPropertyAccessor();
305
306
        foreach ($object->getStateTransitions() as $transition) {
307
            if ($sm->can($transition)) {
308
309
                $color = $this->getTransitionColor($transition);
310
                $tasks[] = array(
311
                    'id' => $accessor->getValue($object, $objectIdentifier),
312
                    'name' => $transition,
313
                    'color' => $color,
314
                    'graph' => $object->getStateGraph(),
315
                    'label' => $this->getTransition($transition),
316
                    'negative' => $this->isNegativeColor($color, 'transition'),
317
                    'positive' => $this->isPositiveColor($color, 'transition'),
318
                );
319
            }
320
        }
321
322
        return empty($tasks) ? null : $tasks;
323
    }
324
325
    /**
326
     * Compare state.
327
     *
328
     * @param array|string $stateCompare State(s) to compare
329
     * @param StatableInterface $object
330
     *
331
     * @return bool
332
     */
333
    public function isState($stateCompare, StatableInterface $object)
334
    {
335
        return in_array(strtolower($object->getState()), (array) $stateCompare);
336
    }
337
}
338