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\RegisterDescriptor; |
12
|
|
|
use OldTown\Workflow\Spi\WorkflowEntryInterface; |
13
|
|
|
use OldTown\Workflow\TransientVars\TransientVarsInterface; |
14
|
|
|
use Traversable; |
15
|
|
|
use OldTown\Workflow\Exception\InvalidArgumentException; |
16
|
|
|
use SplObjectStorage; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Data |
21
|
|
|
* |
22
|
|
|
* @package OldTown\Workflow\Engine |
23
|
|
|
*/ |
24
|
|
|
class Data extends AbstractEngine implements DataInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param WorkflowEntryInterface $entry |
28
|
|
|
* @param TransientVarsInterface $transientVars |
29
|
|
|
* @param array|Traversable|RegisterDescriptor[]|SplObjectStorage $registersStorage |
30
|
|
|
* @param integer $actionId |
31
|
|
|
* @param array|Traversable $currentSteps |
32
|
|
|
* @param PropertySetInterface $ps |
33
|
|
|
* |
34
|
|
|
* |
35
|
|
|
* @return TransientVarsInterface |
36
|
|
|
* |
37
|
|
|
* @throws InvalidArgumentException |
38
|
|
|
* @throws WorkflowException |
39
|
|
|
* @throws InternalWorkflowException |
40
|
|
|
*/ |
41
|
19 |
|
public function populateTransientMap(WorkflowEntryInterface $entry, TransientVarsInterface $transientVars, $registersStorage, $actionId = null, $currentSteps, PropertySetInterface $ps) |
42
|
|
|
{ |
43
|
19 |
View Code Duplication |
if (!is_array($currentSteps) && !$currentSteps instanceof Traversable) { |
|
|
|
|
44
|
|
|
$errMsg = 'Current steps not valid'; |
45
|
|
|
throw new InvalidArgumentException($errMsg); |
46
|
|
|
} |
47
|
|
|
|
48
|
19 |
|
$workflowManager = $this->getWorkflowManager(); |
49
|
19 |
|
$context = $workflowManager->getContext(); |
50
|
19 |
|
$configuration = $workflowManager->getConfiguration(); |
51
|
19 |
|
$engineManagers = $this->getWorkflowManager()->getEngineManager(); |
52
|
|
|
|
53
|
19 |
|
$registers = $engineManagers->getDataEngine()->convertDataInArray($registersStorage); |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** @var RegisterDescriptor[] $registers */ |
57
|
|
|
|
58
|
19 |
|
$transientVars['context'] = $context; |
59
|
19 |
|
$transientVars['entry'] = $entry; |
60
|
19 |
|
$transientVars['entryId'] = $entry->getId(); |
61
|
19 |
|
$transientVars['store'] = $configuration->getWorkflowStore(); |
62
|
19 |
|
$transientVars['configuration'] = $configuration; |
63
|
19 |
|
$transientVars['descriptor'] = $configuration->getWorkflow($entry->getWorkflowName()); |
64
|
|
|
|
65
|
19 |
|
if (null !== $actionId) { |
66
|
19 |
|
$transientVars['actionId'] = $actionId; |
67
|
19 |
|
} |
68
|
|
|
|
69
|
19 |
|
$transientVars['currentSteps'] = $currentSteps; |
70
|
|
|
|
71
|
|
|
|
72
|
19 |
|
foreach ($registers as $register) { |
73
|
3 |
|
$args = $register->getArgs(); |
74
|
3 |
|
$type = $register->getType(); |
75
|
|
|
|
76
|
|
|
try { |
77
|
3 |
|
$r = $workflowManager->getResolver()->getRegister($type, $args); |
78
|
3 |
|
} catch (\Exception $e) { |
79
|
|
|
$errMsg = 'Ошибка при инициализации register'; |
80
|
|
|
$context->setRollbackOnly(); |
81
|
|
|
throw new WorkflowException($errMsg, $e->getCode(), $e); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
$variableName = $register->getVariableName(); |
85
|
|
|
try { |
86
|
3 |
|
$value = $r->registerVariable($context, $entry, $args, $ps); |
87
|
|
|
|
88
|
3 |
|
$transientVars[$variableName] = $value; |
89
|
3 |
|
} catch (\Exception $e) { |
90
|
|
|
$context->setRollbackOnly(); |
91
|
|
|
|
92
|
|
|
$errMsg = sprintf( |
93
|
|
|
'При получение значения переменной %s из registry %s произошла ошибка', |
94
|
|
|
$variableName, |
95
|
|
|
get_class($r) |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
throw new WorkflowException($errMsg, $e->getCode(), $e); |
99
|
|
|
} |
100
|
19 |
|
} |
101
|
|
|
|
102
|
19 |
|
return $transientVars; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Преобразование данных в массив |
107
|
|
|
* |
108
|
|
|
* @param $data |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
* |
112
|
|
|
* @throws InvalidArgumentException |
113
|
|
|
*/ |
114
|
19 |
|
public function convertDataInArray($data) |
115
|
|
|
{ |
116
|
19 |
|
$result = []; |
117
|
19 |
|
if ($data instanceof Traversable) { |
118
|
19 |
|
foreach ($data as $k => $v) { |
119
|
7 |
|
$result[$k] = $v; |
120
|
19 |
|
} |
121
|
19 |
|
} elseif (is_array($data)) { |
122
|
|
|
$result = $data; |
123
|
|
|
} else { |
124
|
|
|
$errMsg = 'Data must be an array or an interface to implement Traversable'; |
125
|
|
|
throw new InvalidArgumentException($errMsg); |
126
|
|
|
} |
127
|
|
|
|
128
|
19 |
|
return $result; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.