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

ResultDescriptor::init()   F

Complexity

Conditions 14
Paths 1536

Size

Total Lines 74
Code Lines 45

Duplication

Lines 24
Ratio 32.43 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 24
loc 74
rs 2.2878
cc 14
eloc 45
nc 1536
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
 * @method ActionDescriptor getParent()
21
 */
22
class ResultDescriptor extends AbstractDescriptor implements ValidateDescriptorInterface, WriteXmlInterface
23
{
24
    use Traits\IdTrait;
25
26
    /**
27
     * Предыдущий статус
28
     *
29
     * @var string
30
     */
31
    protected $oldStatus;
32
33
    /**
34
     * Статус
35
     *
36
     * @var string
37
     */
38
    protected $status;
39
40
    /**
41
     * Срок
42
     *
43
     * @var string
44
     */
45
    protected $dueDate;
46
47
    /**
48
     * @var integer|null
49
     */
50
    protected $join;
51
52
    /**
53
     * @var integer|null
54
     */
55
    protected $split;
56
57
    /**
58
     * @var integer
59
     */
60
    protected $step;
61
62
    /**
63
     * @var bool
64
     */
65
    protected $hasStep = false;
66
67
    /**
68
     * @var string
69
     */
70
    protected $owner;
71
72
    /**
73
     * @var string
74
     */
75
    protected $displayName;
76
77
    /**
78
     * @var ValidatorDescriptor[]|SplObjectStorage
79
     */
80
    protected $validators;
81
82
    /**
83
     * @var FunctionDescriptor[]|SplObjectStorage
84
     */
85
    protected $preFunctions;
86
87
    /**
88
     * @var FunctionDescriptor[]|SplObjectStorage
89
     */
90
    protected $postFunctions;
91
92
    /**
93
     * Если флаг установлен в true, то не запускаем инициализацию дескриптора для элемента
94
     *
95
     * @var bool
96
     */
97
    protected $flagNotExecuteInit = false;
98
99
    /**
100
     * @param $element
101
     */
102
    public function __construct(DOMElement $element = null)
103
    {
104
        $validators = new SplObjectStorage();
105
        $this->setValidators($validators);
106
107
        $this->preFunctions = new SplObjectStorage();
108
        $this->postFunctions = new SplObjectStorage();
109
110
        parent::__construct($element);
111
112
        if (null !== $element && !$this->flagNotExecuteInit) {
113
            $this->init($element);
114
        }
115
    }
116
117
    /**
118
     * @param DOMElement $result
119
     *
120
     * @return void
121
     */
122
    protected function init(DOMElement $result)
123
    {
124
        $oldStatus = XmlUtil::getRequiredAttributeValue($result, 'old-status');
125
        $this->setOldStatus($oldStatus);
126
        if ($result->hasAttribute('status')) {
127
            $status = XmlUtil::getRequiredAttributeValue($result, 'status');
128
            $this->setStatus($status);
129
        }
130
131
        $this->parseId($result, false);
132
133
        if ($result->hasAttribute('due-date')) {
134
            $this->dueDate = XmlUtil::getRequiredAttributeValue($result, 'due-date');
135
        }
136
137
        if ($result->hasAttribute('split')) {
138
            $split = XmlUtil::getRequiredAttributeValue($result, 'split');
139
            $this->setSplit($split);
140
        }
141
142
143
        if ($result->hasAttribute('join')) {
144
            $join = XmlUtil::getRequiredAttributeValue($result, 'join');
145
            $this->setJoin($join);
146
        }
147
148
        if ($result->hasAttribute('step')) {
149
            $step = XmlUtil::getRequiredAttributeValue($result, 'step');
150
            $this->setStep($step);
151
        }
152
153
        if ($result->hasAttribute('owner')) {
154
            $owner = XmlUtil::getRequiredAttributeValue($result, 'owner');
155
            $this->setOwner($owner);
156
        }
157
158
        if ($result->hasAttribute('display-name')) {
159
            $displayName = XmlUtil::getRequiredAttributeValue($result, 'display-name');
160
            $this->setDisplayName($displayName);
161
        }
162
163
        // set up validators -- OPTIONAL
164
        $v = XMLUtil::getChildElement($result, 'validators');
165 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...
166
            $validators = XMLUtil::getChildElements($v, 'validator');
167
            foreach ($validators as $validator) {
168
                $validatorDescriptor = DescriptorFactory::getFactory()->createValidatorDescriptor($validator);
169
                $validatorDescriptor->setParent($this);
170
                $this->validators->attach($validatorDescriptor);
171
            }
172
        }
173
174
        // set up pre-functions -- OPTIONAL
175
        $pre = XMLUtil::getChildElement($result, 'pre-functions');
176 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...
177
            $preFunctions = XMLUtil::getChildElements($pre, 'function');
178
            foreach ($preFunctions as $preFunction) {
179
                $functionDescriptor = DescriptorFactory::getFactory()->createFunctionDescriptor($preFunction);
180
                $functionDescriptor->setParent($this);
181
                $this->preFunctions->attach($functionDescriptor);
182
            }
183
        }
184
185
        // set up post-functions - OPTIONAL
186
        $post = XMLUtil::getChildElement($result, 'post-functions');
187 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...
188
            $postFunctions = XMLUtil::getChildElements($post, 'function');
189
            foreach ($postFunctions as $postFunction) {
190
                $functionDescriptor = DescriptorFactory::getFactory()->createFunctionDescriptor($postFunction);
191
                $functionDescriptor->setParent($this);
192
                $this->postFunctions->attach($functionDescriptor);
193
            }
194
        }
195
    }
196
197
    /**
198
     * Возвращает предыдущий статус
199
     *
200
     * @return string
201
     */
202
    public function getOldStatus()
203
    {
204
        return $this->oldStatus;
205
    }
206
207
    /**
208
     * Устанавливает значение предыдующего статуса
209
     *
210
     * @param string $oldStatus
211
     *
212
     * @return $this
213
     */
214
    public function setOldStatus($oldStatus)
215
    {
216
        $this->oldStatus = $oldStatus;
217
218
        return $this;
219
    }
220
221
    /**
222
     * Возвращает статус
223
     *
224
     * @return string
225
     */
226
    public function getStatus()
227
    {
228
        return $this->status;
229
    }
230
231
    /**
232
     * Устанавливает статус
233
     *
234
     * @param string $status
235
     *
236
     * @return $this
237
     */
238
    public function setStatus($status)
239
    {
240
        $this->status = (string)$status;
241
242
        return $this;
243
    }
244
245
    /**
246
     * Срок
247
     *
248
     * @return string
249
     */
250
    public function getDueDate()
251
    {
252
        return $this->dueDate;
253
    }
254
255
    /**
256
     * @return int|null
257
     */
258
    public function getJoin()
259
    {
260
        return $this->join;
261
    }
262
263
    /**
264
     * @param int $join
265
     *
266
     * @return $this
267
     */
268
    public function setJoin($join)
269
    {
270
        $this->join = (integer)$join;
271
272
        return $this;
273
    }
274
275
    /**
276
     * @return int|null
277
     */
278
    public function getSplit()
279
    {
280
        return $this->split;
281
    }
282
283
    /**
284
     * @param int|null $split
285
     *
286
     * @return $this
287
     */
288
    public function setSplit($split)
289
    {
290
        $this->split = (integer)$split;
291
292
        return $this;
293
    }
294
295
    /**
296
     * @return int
297
     */
298
    public function getStep()
299
    {
300
        return $this->step;
301
    }
302
303
    /**
304
     * @param int $step
305
     *
306
     * @return $this
307
     */
308
    public function setStep($step)
309
    {
310
        $this->step = (integer)$step;
311
        $this->hasStep = true;
312
313
        return $this;
314
    }
315
316
    /**
317
     * @return string
318
     */
319
    public function getOwner()
320
    {
321
        return $this->owner;
322
    }
323
324
    /**
325
     * @param string $owner
326
     *
327
     * @return $this
328
     */
329
    public function setOwner($owner)
330
    {
331
        $this->owner = (string)$owner;
332
333
        return $this;
334
    }
335
336
    /**
337
     * @return string
338
     */
339
    public function getDisplayName()
340
    {
341
        return $this->displayName;
342
    }
343
344
    /**
345
     * @param string $displayName
346
     *
347
     * @return $this
348
     */
349
    public function setDisplayName($displayName)
350
    {
351
        /** @var ActionDescriptor $parent */
352
        $parent = $this->getParent();
353
        if ($parent instanceof ActionDescriptor) {
354
            $parentName = $parent->getName();
355
            if ($displayName === $parentName) {
356
                $this->displayName = null;
357
                return $this;
358
            }
359
        }
360
        $this->displayName = $displayName;
361
362
        return $this;
363
    }
364
365
    /**
366
     * @return ValidatorDescriptor[]|SplObjectStorage
367
     */
368
    public function getValidators()
369
    {
370
        return $this->validators;
371
    }
372
373
    /**
374
     * @param ValidatorDescriptor[]|SplObjectStorage $validators
375
     *
376
     * @return $this
377
     */
378
    public function setValidators(SplObjectStorage $validators)
379
    {
380
        $this->validators = $validators;
381
382
        return $this;
383
    }
384
385
    /**
386
     * @return FunctionDescriptor[]|SplObjectStorage
387
     */
388
    public function getPreFunctions()
389
    {
390
        return $this->preFunctions;
391
    }
392
393
    /**
394
     * @return FunctionDescriptor[]|SplObjectStorage
395
     */
396
    public function getPostFunctions()
397
    {
398
        return $this->postFunctions;
399
    }
400
401
    /**
402
     * Вывод информации о функциях пост обработки
403
     *
404
     * @param DOMDocument $dom
405
     * @return DOMElement|null
406
     * @throws InvalidWriteWorkflowException
407
     */
408 View Code Duplication
    protected function printPostFunctions(DOMDocument $dom)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
409
    {
410
        $postFunctions = $this->getPostFunctions();
411
        if ($postFunctions->count() > 0) {
412
            $postFunctionsElements = $dom->createElement('post-functions');
413
            foreach ($postFunctions as $function) {
414
                try {
415
                    $functionElement = $function->writeXml($dom);
416
                } catch (\Exception $e) {
417
                    $errMsg  = 'Ошибка сохранения workflow. Ошибка в post-function';
418
                    throw new InvalidWriteWorkflowException($errMsg, $e->getCode(), $e);
419
                }
420
421
                $postFunctionsElements->appendChild($functionElement);
422
            }
423
424
            return $postFunctionsElements;
425
        }
426
427
        return null;
428
    }
429
430
    /**
431
     * Вывод информации о функциях пред обработки
432
     *
433
     * @param DOMDocument $dom
434
     * @return DOMElement|null
435
     *
436
     * @throws InvalidWriteWorkflowException
437
     */
438 View Code Duplication
    protected function printPreFunctions(DOMDocument $dom)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
439
    {
440
        $preFunctions = $this->getPreFunctions();
441
        if ($preFunctions->count() > 0) {
442
            $preFunctionsElements = $dom->createElement('pre-functions');
443
            foreach ($preFunctions as $function) {
444
                try {
445
                    $functionElement = $function->writeXml($dom);
446
                } catch (\Exception $e) {
447
                    $errMsg  = 'Ошибка сохранения workflow. Ошибка в pre-function';
448
                    throw new InvalidWriteWorkflowException($errMsg, $e->getCode(), $e);
449
                }
450
                $preFunctionsElements->appendChild($functionElement);
451
            }
452
453
            return $preFunctionsElements;
454
        }
455
456
        return null;
457
    }
458
459
460
    /**
461
     * Создает DOMElement - эквивалентный состоянию дескриптора
462
     *
463
     * @param DOMDocument $dom
464
     *
465
     * @return DOMElement
466
     * @throws InvalidDescriptorException
467
     * @throws InvalidWriteWorkflowException
468
     */
469
    public function writeXml(DOMDocument $dom = null)
470
    {
471
        if (null === $dom) {
472
            $errMsg = 'Не передан DOMDocument';
473
            throw new InvalidWriteWorkflowException($errMsg);
474
        }
475
        $descriptor = $dom->createElement('unconditional-result');
476
477
        if ($this->hasId()) {
478
            $id = $this->getId();
479
            $descriptor->setAttribute('id', $id);
480
        }
481
482
        $dueDate = $this->getDueDate();
483 View Code Duplication
        if (null !== $dueDate && is_string($dueDate) && strlen($dueDate) > 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...
484
            $descriptor->setAttribute('due-date', $dueDate);
485
        }
486
487
        $oldStatus = $this->getOldStatus();
488
        if (null === $oldStatus) {
489
            $errMsg = 'Некорректное значение для атрибута old-status';
490
            throw new InvalidDescriptorException($errMsg);
491
        }
492
        $descriptor->setAttribute('old-status', $oldStatus);
493
494
495
496
        $join = $this->getJoin();
497
        $split = $this->getSplit();
498 View Code Duplication
        if (null !== $join && 0 !== $join) {
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...
499
            $descriptor->setAttribute('join', $join);
500
        } elseif (null !== $split && 0 !== $split) {
501
            $descriptor->setAttribute('split', $split);
502
        } else {
503
            $status = $this->getStatus();
504
            if (null === $status) {
505
                $errMsg = 'Некорректное значение для атрибута status';
506
                throw new InvalidDescriptorException($errMsg);
507
            }
508
            $descriptor->setAttribute('status', $status);
509
510
            $step = $this->getStep();
511
            if (null === $step) {
512
                $errMsg = 'Некорректное значение для атрибута step';
513
                throw new InvalidDescriptorException($errMsg);
514
            }
515
            $descriptor->setAttribute('step', $step);
516
517
            $owner = $this->getOwner();
518
            if (null !== $owner && is_string($owner) && strlen($owner) > 0) {
519
                $descriptor->setAttribute('owner', $owner);
520
            }
521
522
            $displayName = $this->getDisplayName();
523
            if (null !== $displayName && is_string($displayName) && strlen($displayName) > 0) {
524
                $descriptor->setAttribute('display-name', $displayName);
525
            }
526
        }
527
528
        $validators = $this->getValidators();
529 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...
530
            $validatorsDescriptor = $dom->createElement('validators');
531
            $descriptor->appendChild($validatorsDescriptor);
532
533
            foreach ($validators as $validator) {
534
                $validatorElement = $validator->writeXml($dom);
535
                $validatorsDescriptor->appendChild($validatorElement);
536
            }
537
        }
538
539
        $preFunctionsElement = $this->printPreFunctions($dom);
540
        if (null !== $preFunctionsElement) {
541
            $descriptor->appendChild($preFunctionsElement);
542
        }
543
544
        $postFunctionsElement = $this->printPostFunctions($dom);
545
        if (null !== $postFunctionsElement) {
546
            $descriptor->appendChild($postFunctionsElement);
547
        }
548
549
        return $descriptor;
550
    }
551
552
553
    /**
554
     * Валидация дескриптора
555
     *
556
     * @return void
557
     * @throws InvalidWorkflowDescriptorException
558
     */
559
    public function validate()
560
    {
561
        $preFunctions = $this->getPreFunctions();
562
        $postFunctions = $this->getPostFunctions();
563
        $validators = $this->getValidators();
564
565
        ValidationHelper::validate($preFunctions);
566
        ValidationHelper::validate($postFunctions);
567
        ValidationHelper::validate($validators);
568
569
        $split = $this->getSplit();
570
        $join = $this->getJoin();
571
572
        $parent = $this->getParent();
573
        if ((null === $split) && (null === $join) && !($parent instanceof ActionDescriptor && ($parent->isFinish()))) {
574
            $errMsg = '';
575
            $id = (integer)$this->getId();
576
            if (is_numeric($id)) {
577
                $errMsg .= sprintf('#%s:', $id);
578
            }
579
            $errMsg .= 'Если не указано значение атрибутов split или join, необходимо указать';
580
581
            if (!$this->hasStep) {
582
                $errMsg .= ' id следующего шага';
583
                throw new InvalidWorkflowDescriptorException($errMsg);
584
            }
585
586
            $status = $this->getStatus();
587
            if (!$status) {
588
                $errMsg .= ' статус';
589
                throw new InvalidWorkflowDescriptorException($errMsg);
590
            }
591
        }
592
    }
593
}
594