GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 138d30...5011e7 )
by Андрей
07:49
created

ActionDescriptor::writeXml()   F

Complexity

Conditions 18
Paths 3074

Size

Total Lines 110
Code Lines 71

Duplication

Lines 37
Ratio 33.64 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 37
loc 110
rs 2
cc 18
eloc 71
nc 3074
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @link    https://github.com/old-town/old-town-workflow
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace OldTown\Workflow\Loader;
7
8
use DOMElement;
9
use OldTown\Workflow\Exception\InvalidDescriptorException;
10
use OldTown\Workflow\Exception\InvalidWorkflowDescriptorException;
11
use OldTown\Workflow\Exception\InvalidWriteWorkflowException;
12
use SplObjectStorage;
13
use DOMDocument;
14
15
/**
16
 * Class ConditionDescriptor
17
 *
18
 * @package OldTown\Workflow\Loader
19
 */
20
class ActionDescriptor extends AbstractDescriptor
21
    implements
22
        Traits\NameInterface,
23
        ValidateDescriptorInterface,
24
        WriteXmlInterface
25
{
26
    use Traits\NameTrait, Traits\IdTrait;
27
28
    /**
29
     * @var ConditionalResultDescriptor[]|SplObjectStorage
30
     */
31
    protected $conditionalResults;
32
33
    /**
34
     * @var FunctionDescriptor[]|SplObjectStorage
35
     */
36
    protected $postFunctions;
37
38
    /**
39
     * @var FunctionDescriptor[]|SplObjectStorage
40
     */
41
    protected $preFunctions;
42
43
    /**
44
     * @var ValidatorDescriptor[]|SplObjectStorage
45
     */
46
    protected $validators;
47
48
    /**
49
     * @var array
50
     */
51
    protected $metaAttributes = [];
52
53
    /**
54
     * @var RestrictionDescriptor
55
     */
56
    protected $restriction;
57
58
    /**
59
     * @var ResultDescriptor
60
     */
61
    protected $unconditionalResult;
62
63
    /**
64
     * @var string
65
     */
66
    protected $view;
67
68
    /**
69
     * @var bool
70
     */
71
    protected $autoExecute = false;
72
73
    /**
74
     * @var bool
75
     */
76
    protected $common = false;
77
78
    /**
79
     * @var bool
80
     */
81
    protected $finish = false;
82
83
    /**
84
     * @param $element
85
     * @throws InvalidWorkflowDescriptorException
86
     */
87
    public function __construct(DOMElement $element = null)
88
    {
89
        $this->validators = new SplObjectStorage();
90
        $this->preFunctions = new SplObjectStorage();
91
        $this->conditionalResults = new SplObjectStorage();
92
        $this->postFunctions = new SplObjectStorage();
93
94
        parent::__construct($element);
95
96
        if (null !== $element) {
97
            $this->init($element);
98
        }
99
    }
100
101
    /**
102
     * @param DOMElement $action
103
     *
104
     * @return void
105
     * @throws InvalidWorkflowDescriptorException
106
     */
107
    protected function init(DOMElement $action)
108
    {
109
        $this->parseId($action);
110
        $this->parseName($action);
111
112
        if ($action->hasAttribute('view')) {
113
            $view = XmlUtil::getRequiredAttributeValue($action, 'view');
114
            $this->setView($view);
115
        }
116
117
        if ($action->hasAttribute('auto')) {
118
            $autoValue = XmlUtil::getRequiredAttributeValue($action, 'auto');
119
            $auto = strtolower($autoValue);
120
            $autoExecute = 'true' === $auto;
121
            $this->setAutoExecute($autoExecute);
122
        }
123
124
        if ($action->hasAttribute('finish')) {
125
            $finishValue = XmlUtil::getRequiredAttributeValue($action, 'finish');
126
            $finish = strtolower($finishValue);
127
            $finish = 'true' === $finish;
128
            $this->setFinish($finish);
129
        }
130
131
        $metaElements = XmlUtil::getChildElements($action, 'meta');
132
        $metaAttributes = [];
133 View Code Duplication
        foreach ($metaElements as $meta) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
            $value = XmlUtil::getText($meta);
135
            $name = XmlUtil::getRequiredAttributeValue($meta, 'name');
136
137
            $metaAttributes[$name] = $value;
138
        }
139
        $this->setMetaAttributes($metaAttributes);
140
141
        // set up validators -- OPTIONAL
142
        $v = XMLUtil::getChildElement($action, 'validators');
143 View Code Duplication
        if (null !== $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
            $validators = XMLUtil::getChildElements($v, 'validator');
145
            foreach ($validators as $validator) {
146
                $validatorDescriptor = DescriptorFactory::getFactory()->createValidatorDescriptor($validator);
147
                $validatorDescriptor->setParent($this);
148
                $this->validators->attach($validatorDescriptor);
149
            }
150
        }
151
152
        // set up pre-functions -- OPTIONAL
153
        $pre = XMLUtil::getChildElement($action, 'pre-functions');
154 View Code Duplication
        if (null !== $pre) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
            $preFunctions = XMLUtil::getChildElements($pre, 'function');
156
            foreach ($preFunctions as $preFunction) {
157
                $functionDescriptor = DescriptorFactory::getFactory()->createFunctionDescriptor($preFunction);
158
                $functionDescriptor->setParent($this);
159
                $this->preFunctions->attach($functionDescriptor);
160
            }
161
        }
162
163
        // set up results - REQUIRED
164
        $resultsElement = XMLUtil::getChildElement($action, 'results');
165
        if (!$resultsElement instanceof DOMElement) {
166
            $errMsg = 'Отсутствует обязательный блок results';
167
            throw new InvalidWorkflowDescriptorException($errMsg);
168
        }
169
        $results = XMLUtil::getChildElements($resultsElement, 'result');
170
        foreach ($results as $result) {
171
            $conditionalResultDescriptor = new ConditionalResultDescriptor($result);
172
            $conditionalResultDescriptor->setParent($this);
173
            $this->conditionalResults->attach($conditionalResultDescriptor);
174
        }
175
176
        $unconditionalResult = XMLUtil::getChildElement($resultsElement, 'unconditional-result');
177
        if (null !== $unconditionalResult) {
178
            $unconditionalResult = DescriptorFactory::getFactory()->createResultDescriptor($unconditionalResult);
179
            $unconditionalResult->setParent($this);
180
            $this->setUnconditionalResult($unconditionalResult);
181
        }
182
183
184
        // set up post-functions - OPTIONAL
185
        $post = XMLUtil::getChildElement($action, 'post-functions');
186 View Code Duplication
        if (null !== $post) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
            $postFunctions = XMLUtil::getChildElements($post, 'function');
188
            foreach ($postFunctions as $postFunction) {
189
                $functionDescriptor = DescriptorFactory::getFactory()->createFunctionDescriptor($postFunction);
190
                $functionDescriptor->setParent($this);
191
                $this->postFunctions->attach($functionDescriptor);
192
            }
193
        }
194
195
        // set up restrict-to - OPTIONAL
196
        $restrictElement = XMLUtil::getChildElement($action, 'restrict-to');
197
        if (null !== $restrictElement) {
198
            $restriction = new RestrictionDescriptor($restrictElement);
199
200
            if (null !== $restriction->getConditionsDescriptor()) {
201
                $restriction->setParent($this);
202
                $this->setRestriction($restriction);
203
            }
204
        }
205
    }
206
207
    /**
208
     * @return boolean
209
     */
210
    public function getAutoExecute()
211
    {
212
        return $this->autoExecute;
213
    }
214
215
    /**
216
     * @param boolean $autoExecute
217
     *
218
     * @return $this
219
     */
220
    public function setAutoExecute($autoExecute)
221
    {
222
        $this->autoExecute = (boolean)$autoExecute;
223
224
        return $this;
225
    }
226
227
    /**
228
     * @return boolean
229
     */
230
    public function isCommon()
231
    {
232
        return $this->common;
233
    }
234
235
    /**
236
     * @param boolean $common
237
     *
238
     * @return $this
239
     */
240
    public function setCommon($common)
241
    {
242
        $this->common = (boolean)$common;
243
244
        return $this;
245
    }
246
247
    /**
248
     * @return string
249
     */
250
    public function getView()
251
    {
252
        return $this->view;
253
    }
254
255
    /**
256
     * @param string $view
257
     *
258
     * @return $this
259
     */
260
    public function setView($view)
261
    {
262
        $this->view = (string)$view;
263
264
        return $this;
265
    }
266
267
    /**
268
     * @return ValidatorDescriptor[]|SplObjectStorage
269
     */
270
    public function getValidators()
271
    {
272
        return $this->validators;
273
    }
274
275
    /**
276
     * @return ResultDescriptor
277
     */
278
    public function getUnconditionalResult()
279
    {
280
        return $this->unconditionalResult;
281
    }
282
283
    /**
284
     * @param ResultDescriptor $unconditionalResult
285
     *
286
     * @return $this
287
     */
288
    public function setUnconditionalResult(ResultDescriptor $unconditionalResult)
289
    {
290
        $this->unconditionalResult = $unconditionalResult;
291
292
        return $this;
293
    }
294
295
    /**
296
     * @return RestrictionDescriptor
297
     */
298
    public function getRestriction()
299
    {
300
        return $this->restriction;
301
    }
302
303
    /**
304
     * @param RestrictionDescriptor $restriction
305
     *
306
     * @return $this
307
     */
308
    public function setRestriction(RestrictionDescriptor $restriction)
309
    {
310
        $this->restriction = $restriction;
311
312
        return $this;
313
    }
314
315
    /**
316
     * @return FunctionDescriptor[]|SplObjectStorage
317
     */
318
    public function getPostFunctions()
319
    {
320
        return $this->postFunctions;
321
    }
322
323
    /**
324
     * @return FunctionDescriptor[]|SplObjectStorage
325
     */
326
    public function getPreFunctions()
327
    {
328
        return $this->preFunctions;
329
    }
330
331
    /**
332
     * @return array
333
     */
334
    public function getMetaAttributes()
335
    {
336
        return $this->metaAttributes;
337
    }
338
339
    /**
340
     * @param array $metaAttributes
341
     *
342
     * @return $this
343
     */
344
    public function setMetaAttributes(array $metaAttributes = [])
345
    {
346
        $this->metaAttributes = $metaAttributes;
347
348
        return $this;
349
    }
350
351
    /**
352
     * @return boolean
353
     */
354
    public function isFinish()
355
    {
356
        return $this->finish;
357
    }
358
359
    /**
360
     * @param boolean $finish
361
     *
362
     * @return $this
363
     */
364
    public function setFinish($finish)
365
    {
366
        $this->finish = (boolean)$finish;
367
368
        return $this;
369
    }
370
371
    /**
372
     * @return SplObjectStorage|ConditionalResultDescriptor[]
373
     */
374
    public function getConditionalResults()
375
    {
376
        return $this->conditionalResults;
377
    }
378
379
380
    /**
381
     * Валидация дескриптора
382
     *
383
     * @return void
384
     * @throws InvalidWorkflowDescriptorException
385
     */
386
    public function validate()
387
    {
388
        $preFunctions = $this->getPreFunctions();
389
        $postFunctions = $this->getPostFunctions();
390
        $validators = $this->getValidators();
391
        $conditionalResults = $this->getConditionalResults();
392
393
        ValidationHelper::validate($preFunctions);
394
        ValidationHelper::validate($postFunctions);
395
        ValidationHelper::validate($validators);
396
        ValidationHelper::validate($conditionalResults);
397
398
        $unconditionalResult = $this->getUnconditionalResult();
399
        if (null === $unconditionalResult && $conditionalResults->count() > 0) {
400
            $name = (string)$this->getName();
401
            $errMsg = sprintf('Действие %s имеет условные условия, но не имеет запасного безусловного', $name);
402
            throw new InvalidWorkflowDescriptorException($errMsg);
403
        }
404
405
        $restrictions = $this->getRestriction();
406
        if ($restrictions !== null) {
407
            $restrictions->validate();
408
        }
409
410
        if ($unconditionalResult !== null) {
411
            $unconditionalResult->validate();
412
        }
413
    }
414
415
    /**
416
     * Создает DOMElement - эквивалентный состоянию дескриптора
417
     *
418
     * @param DOMDocument $dom
419
     *
420
     * @return DOMElement
421
     * @throws InvalidDescriptorException
422
     * @throws InvalidWriteWorkflowException
423
     */
424
    public function writeXml(DOMDocument $dom = null)
425
    {
426
        if (null === $dom) {
427
            $errMsg = 'Не передан DOMDocument';
428
            throw new InvalidWriteWorkflowException($errMsg);
429
        }
430
        $descriptor = $dom->createElement('action');
431
432
        if (!$this->hasId()) {
433
            $errMsg = 'Отсутствует атрибут id';
434
            throw new InvalidDescriptorException($errMsg);
435
        }
436
        $id = $this->getId();
437
        $descriptor->setAttribute('id', $id);
438
439
        $name = (string)$this->getName();
440
        $name = trim($name);
441
        if (strlen($name) > 0) {
442
            $nameEncode = XmlUtil::encode($name);
443
            $descriptor->setAttribute('name', $nameEncode);
444
        }
445
446
        $view = (string)$this->getView();
447
        $view = trim($view);
448
        if (strlen($view) > 0) {
449
            $viewEncode = XmlUtil::encode($view);
450
            $descriptor->setAttribute('view', $viewEncode);
451
        }
452
453
        if ($this->isFinish()) {
454
            $descriptor->setAttribute('finish', 'true');
455
        }
456
457
        if ($this->getAutoExecute()) {
458
            $descriptor->setAttribute('auto', 'true');
459
        }
460
461
        $metaAttributes = $this->getMetaAttributes();
462
        $baseMeta = $dom->createElement('meta');
463 View Code Duplication
        foreach ($metaAttributes as $metaAttributeName => $metaAttributeValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
464
            $metaAttributeNameEncode = XmlUtil::encode($metaAttributeName);
465
            $metaAttributeValueEnEncode = XmlUtil::encode($metaAttributeValue);
466
467
            $metaElement = clone $baseMeta;
468
            $metaElement->setAttribute('name', $metaAttributeNameEncode);
469
            $metaValueElement = $dom->createTextNode($metaAttributeValueEnEncode);
470
            $metaElement->appendChild($metaValueElement);
471
472
            $descriptor->appendChild($metaElement);
473
        }
474
475
476
        $restrictions = $this->getRestriction();
477
        if ($restrictions !== null) {
478
            $restrictionsElement = $restrictions->writeXml($dom);
479
            if (null !== $restrictionsElement) {
480
                $descriptor->appendChild($restrictionsElement);
481
            }
482
        }
483
484
        $validators = $this->getValidators();
485 View Code Duplication
        if ($validators->count() > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
486
            $validatorsElement = $dom->createElement('validators');
487
            foreach ($validators as $validator) {
488
                $validatorElement = $validator->writeXml($dom);
489
                $validatorsElement->appendChild($validatorElement);
490
            }
491
            $descriptor->appendChild($validatorsElement);
492
        }
493
494
        $preFunctions = $this->getPreFunctions();
495 View Code Duplication
        if ($preFunctions->count() > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
496
            $preFunctionsElement = $dom->createElement('pre-functions');
497
            foreach ($preFunctions as $function) {
498
                $functionElement = $function->writeXml($dom);
499
                $preFunctionsElement->appendChild($functionElement);
500
            }
501
502
            $descriptor->appendChild($preFunctionsElement);
503
        }
504
505
        $resultsElement = $dom->createElement('results');
506
        $descriptor->appendChild($resultsElement);
507
508
        $conditionalResults = $this->getConditionalResults();
509
        foreach ($conditionalResults as $conditionalResult) {
510
            $conditionalResultElement = $conditionalResult->writeXml($dom);
511
            $resultsElement->appendChild($conditionalResultElement);
512
        }
513
514
        $unconditionalResult = $this->getUnconditionalResult();
515
        if ($unconditionalResult !== null) {
516
            $unconditionalResultElement = $unconditionalResult->writeXml($dom);
517
            $resultsElement->appendChild($unconditionalResultElement);
518
        }
519
520
        $postFunctions = $this->getPostFunctions();
521 View Code Duplication
        if ($postFunctions->count() > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
522
            $postFunctionsElement = $dom->createElement('post-functions');
523
            foreach ($postFunctions as $function) {
524
                $functionElement = $function->writeXml($dom);
525
                $postFunctionsElement->appendChild($functionElement);
526
            }
527
528
            $descriptor->appendChild($postFunctionsElement);
529
        }
530
531
532
        return $descriptor;
533
    }
534
}
535