|
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
|
7 |
|
$type = $descriptor->getType(); |
|
41
|
7 |
|
$conditions = $descriptor->getConditions(); |
|
42
|
7 |
|
if (!$conditions instanceof SplObjectStorage) { |
|
43
|
|
|
$errMsg = 'Invalid conditions'; |
|
44
|
|
|
throw new InternalWorkflowException($errMsg); |
|
45
|
|
|
} |
|
46
|
7 |
|
$passesConditions = $this->passesConditionsWithType($type, $conditions, $transientVars, $ps, $currentStepId); |
|
47
|
|
|
|
|
48
|
7 |
|
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
|
13 |
|
public function passesConditionsWithType($conditionType, SplObjectStorage $conditions = null, TransientVarsInterface $transientVars, PropertySetInterface $ps, $currentStepId) |
|
67
|
|
|
{ |
|
68
|
13 |
|
if (null === $conditions) { |
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
13 |
|
if (0 === $conditions->count()) { |
|
73
|
1 |
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
12 |
|
$and = strtoupper($conditionType) === 'AND'; |
|
77
|
12 |
|
$or = !$and; |
|
78
|
|
|
|
|
79
|
12 |
|
foreach ($conditions as $descriptor) { |
|
80
|
12 |
|
if ($descriptor instanceof ConditionsDescriptor) { |
|
81
|
6 |
|
$descriptorConditions = $descriptor->getConditions(); |
|
82
|
6 |
|
if (!$descriptorConditions instanceof SplObjectStorage) { |
|
83
|
|
|
$errMsg = 'Invalid conditions container'; |
|
84
|
|
|
throw new InternalWorkflowException($errMsg); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
6 |
|
$result = $this->passesConditionsWithType($descriptor->getType(), $descriptorConditions, $transientVars, $ps, $currentStepId); |
|
88
|
12 |
|
} elseif ($descriptor instanceof ConditionDescriptor) { |
|
89
|
12 |
|
$result = $this->passesCondition($descriptor, $transientVars, $ps, $currentStepId); |
|
90
|
11 |
|
} else { |
|
91
|
|
|
$errMsg = 'Invalid condition descriptor'; |
|
92
|
|
|
throw new InternalWorkflowException($errMsg); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
11 |
|
if ($and && !$result) { |
|
96
|
1 |
|
return false; |
|
97
|
11 |
|
} elseif ($or && $result) { |
|
98
|
5 |
|
return true; |
|
99
|
|
|
} |
|
100
|
10 |
|
} |
|
101
|
|
|
|
|
102
|
9 |
|
if ($and) { |
|
103
|
3 |
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
6 |
|
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
|
12 |
|
protected function passesCondition(ConditionDescriptor $conditionDesc, TransientVarsInterface $transientVars, PropertySetInterface $ps, $currentStepId) |
|
121
|
|
|
{ |
|
122
|
12 |
|
$type = $conditionDesc->getType(); |
|
123
|
|
|
|
|
124
|
12 |
|
$argsOriginal = $conditionDesc->getArgs(); |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
12 |
|
$args = $this->getWorkflowManager()->getEngineManager()->getArgsEngine()->prepareArgs($argsOriginal, $transientVars, $ps); |
|
128
|
|
|
|
|
129
|
12 |
|
if (-1 !== $currentStepId) { |
|
130
|
7 |
|
$stepId = array_key_exists('stepId', $args) ? (integer)$args['stepId'] : null; |
|
131
|
|
|
|
|
132
|
7 |
|
if (null !== $stepId && -1 === $stepId) { |
|
133
|
1 |
|
$args['stepId'] = $currentStepId; |
|
134
|
1 |
|
} |
|
135
|
7 |
|
} |
|
136
|
|
|
|
|
137
|
12 |
|
$condition = $this->getWorkflowManager()->getResolver()->getCondition($type, $args); |
|
138
|
|
|
|
|
139
|
11 |
|
$context = $this->getWorkflowManager()->getContext(); |
|
140
|
11 |
|
if (null === $condition) { |
|
141
|
|
|
$context->setRollbackOnly(); |
|
142
|
|
|
$errMsg = 'Огибка при загрузки условия'; |
|
143
|
|
|
throw new WorkflowException($errMsg); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
try { |
|
147
|
11 |
|
$passed = $condition->passesCondition($transientVars, $args, $ps); |
|
148
|
|
|
|
|
149
|
11 |
|
if ($conditionDesc->isNegate()) { |
|
150
|
|
|
$passed = !$passed; |
|
151
|
|
|
} |
|
152
|
11 |
|
} 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
|
11 |
|
return $passed; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|