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 — dev ( fead3a...925885 )
by Андрей
07:22
created

Conditions::passesConditionsByDescriptor()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 8
cts 10
cp 0.8
rs 9.4285
cc 3
eloc 10
nc 3
nop 4
crap 3.072
1
<?php
2
/**
3
 * @link https://github.com/old-town/old-town-workflow
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace OldTown\Workflow\Engine;
7
8
use OldTown\PropertySet\PropertySetInterface;
9
use OldTown\Workflow\Exception\InternalWorkflowException;
10
use OldTown\Workflow\Exception\WorkflowException;
11
use OldTown\Workflow\Loader\ConditionDescriptor;
12
use OldTown\Workflow\Loader\ConditionsDescriptor;
13
use OldTown\Workflow\TransientVars\TransientVarsInterface;
14
use SplObjectStorage;
15
16
/**
17
 * Class Conditions
18
 *
19
 * @package OldTown\Workflow\Engine
20
 */
21
class Conditions extends AbstractEngine implements ConditionsInterface
22
{
23
    /**
24
     * @param ConditionsDescriptor $descriptor
25
     * @param TransientVarsInterface $transientVars
26
     * @param PropertySetInterface $ps
27
     * @param                      $currentStepId
28
     *
29
     * @return bool
30
     *
31
     * @throws InternalWorkflowException
32
     * @throws WorkflowException
33
     */
34 19
    public function passesConditionsByDescriptor(ConditionsDescriptor $descriptor = null, TransientVarsInterface $transientVars, PropertySetInterface $ps, $currentStepId)
35
    {
36 19
        if (null === $descriptor) {
37 17
            return true;
38
        }
39
40 2
        $type = $descriptor->getType();
41 2
        $conditions = $descriptor->getConditions();
42 2
        if (!$conditions instanceof SplObjectStorage) {
43
            $errMsg = 'Invalid conditions';
44
            throw new InternalWorkflowException($errMsg);
45
        }
46 3
        $passesConditions = $this->passesConditionsWithType($type, $conditions, $transientVars, $ps, $currentStepId);
47
48 2
        return $passesConditions;
49
    }
50
51
52
    /**
53
     * @param string $conditionType
54
     * @param SplObjectStorage $conditions
55
     * @param TransientVarsInterface $transientVars
56
     * @param PropertySetInterface $ps
57
     * @param integer $currentStepId
58
     *
59
     * @return bool
60
     *
61
     * @throws InternalWorkflowException
62
     * @throws InternalWorkflowException
63
     * @throws WorkflowException
64
     *
65
     */
66 7
    public function passesConditionsWithType($conditionType, SplObjectStorage $conditions = null, TransientVarsInterface $transientVars, PropertySetInterface $ps, $currentStepId)
67
    {
68 7
        if (null === $conditions) {
69
            return true;
70
        }
71
72 7
        if (0 === $conditions->count()) {
73
            return true;
74
        }
75
76 7
        $and = strtoupper($conditionType) === 'AND';
77 7
        $or = !$and;
78
79 7
        foreach ($conditions as $descriptor) {
80 7
            if ($descriptor instanceof ConditionsDescriptor) {
81 5
                $descriptorConditions = $descriptor->getConditions();
82 5
                if (!$descriptorConditions instanceof SplObjectStorage) {
83
                    $errMsg = 'Invalid conditions container';
84
                    throw new InternalWorkflowException($errMsg);
85
                }
86
87 5
                $result = $this->passesConditionsWithType($descriptor->getType(), $descriptorConditions, $transientVars, $ps, $currentStepId);
88 7
            } elseif ($descriptor instanceof ConditionDescriptor) {
89 7
                $result = $this->passesCondition($descriptor, $transientVars, $ps, $currentStepId);
90 7
            } else {
91
                $errMsg = 'Invalid condition descriptor';
92
                throw new InternalWorkflowException($errMsg);
93
            }
94
95 7
            if ($and && !$result) {
96
                return false;
97 7
            } elseif ($or && $result) {
98 5
                return true;
99
            }
100 6
        }
101
102 6
        if ($and) {
103 1
            return true;
104
        }
105
106 5
        return false;
107
    }
108
109
    /**
110
     * @param ConditionDescriptor $conditionDesc
111
     * @param TransientVarsInterface $transientVars
112
     * @param PropertySetInterface $ps
113
     * @param integer $currentStepId
114
     *
115
     * @return boolean
116
     *
117
     * @throws WorkflowException
118
     * @throws InternalWorkflowException
119
     */
120 7
    protected function passesCondition(ConditionDescriptor $conditionDesc, TransientVarsInterface $transientVars, PropertySetInterface $ps, $currentStepId)
121
    {
122 7
        $type = $conditionDesc->getType();
123
124 7
        $argsOriginal = $conditionDesc->getArgs();
125
126
127 7
        $args = $this->getWorkflowManager()->getEngineManager()->getArgsEngine()->prepareArgs($argsOriginal, $transientVars, $ps);
128
129 7
        if (-1 !== $currentStepId) {
130 2
            $stepId = array_key_exists('stepId', $args) ? (integer)$args['stepId'] : null;
131
132 2
            if (null !== $stepId && -1 === $stepId) {
133
                $args['stepId'] = $currentStepId;
134
            }
135 2
        }
136
137 7
        $condition = $this->getWorkflowManager()->getResolver()->getCondition($type, $args);
138
139 7
        $context = $this->getWorkflowManager()->getContext();
140 7
        if (null === $condition) {
141
            $context->setRollbackOnly();
142
            $errMsg = 'Огибка при загрузки условия';
143
            throw new WorkflowException($errMsg);
144
        }
145
146
        try {
147 7
            $passed = $condition->passesCondition($transientVars, $args, $ps);
148
149 7
            if ($conditionDesc->isNegate()) {
150
                $passed = !$passed;
151
            }
152 7
        } catch (\Exception $e) {
153
            $context->setRollbackOnly();
154
155
            $errMsg = sprintf(
156
                'Ошбика при выполнение условия %s',
157
                get_class($condition)
158
            );
159
160
            throw new WorkflowException($errMsg, $e->getCode(), $e);
161
        }
162
163 7
        return $passed;
164
    }
165
}
166