|
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
|
|
|
/** |
|
17
|
|
|
* Interface WorkflowDescriptor |
|
18
|
|
|
* |
|
19
|
|
|
* @package OldTown\Workflow\Loader |
|
20
|
|
|
*/ |
|
21
|
|
|
class JoinDescriptor extends AbstractDescriptor implements ValidateDescriptorInterface, WriteXmlInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use Traits\IdTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ConditionsDescriptor[]|SplObjectStorage |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $conditions; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ResultDescriptor |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $result; |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param $element |
|
38
|
|
|
*/ |
|
39
|
20 |
View Code Duplication |
public function __construct(DOMElement $element = null) |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
20 |
|
$this->conditions = new SplObjectStorage(); |
|
42
|
|
|
|
|
43
|
20 |
|
parent::__construct($element); |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
20 |
|
if (null !== $element) { |
|
47
|
17 |
|
$this->init($element); |
|
48
|
17 |
|
} |
|
49
|
20 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param DOMElement $join |
|
53
|
|
|
* |
|
54
|
|
|
* @return void |
|
55
|
|
|
*/ |
|
56
|
17 |
|
protected function init(DOMElement $join) |
|
57
|
|
|
{ |
|
58
|
17 |
|
$this->parseId($join); |
|
59
|
|
|
|
|
60
|
17 |
|
$conditionNodes = XmlUtil::getChildElements($join, 'conditions'); |
|
61
|
17 |
|
foreach ($conditionNodes as $condition) { |
|
62
|
14 |
|
$conditionDescriptor = DescriptorFactory::getFactory()->createConditionsDescriptor($condition); |
|
63
|
14 |
|
$conditionDescriptor->setParent($conditionDescriptor); |
|
64
|
14 |
|
$this->conditions->attach($conditionDescriptor); |
|
65
|
17 |
|
} |
|
66
|
|
|
|
|
67
|
17 |
|
$resultElement = XMLUtil::getChildElement($join, 'unconditional-result'); |
|
68
|
17 |
|
if (null !== $resultElement) { |
|
69
|
16 |
|
$result = new ResultDescriptor($resultElement); |
|
70
|
16 |
|
$result->setParent($this); |
|
71
|
16 |
|
$this->setResult($result); |
|
72
|
16 |
|
} |
|
73
|
17 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return ConditionsDescriptor[]|SplObjectStorage |
|
77
|
|
|
*/ |
|
78
|
15 |
|
public function getConditions() |
|
79
|
|
|
{ |
|
80
|
15 |
|
return $this->conditions; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return ResultDescriptor |
|
85
|
|
|
*/ |
|
86
|
15 |
|
public function getResult() |
|
87
|
|
|
{ |
|
88
|
15 |
|
return $this->result; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param ResultDescriptor $result |
|
93
|
|
|
* |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
16 |
|
public function setResult(ResultDescriptor $result) |
|
97
|
|
|
{ |
|
98
|
16 |
|
$this->result = $result; |
|
99
|
|
|
|
|
100
|
16 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Валидация дескриптора |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
* @throws InvalidWorkflowDescriptorException |
|
109
|
|
|
*/ |
|
110
|
13 |
View Code Duplication |
public function validate() |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
13 |
|
$conditions = $this->getConditions(); |
|
113
|
13 |
|
ValidationHelper::validate($conditions); |
|
114
|
|
|
|
|
115
|
13 |
|
$result = $this->getResult(); |
|
116
|
13 |
|
if (null === $result) { |
|
117
|
1 |
|
$errMsg = 'Join должен иметь реузультат'; |
|
118
|
1 |
|
throw new InvalidWorkflowDescriptorException($errMsg); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
12 |
|
$result->validate(); |
|
122
|
12 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Создает DOMElement - эквивалентный состоянию дескриптора |
|
126
|
|
|
* |
|
127
|
|
|
* @param DOMDocument $dom |
|
128
|
|
|
* |
|
129
|
|
|
* @return DOMElement|null |
|
130
|
|
|
* @throws InvalidDescriptorException |
|
131
|
|
|
* @throws InvalidWriteWorkflowException |
|
132
|
|
|
*/ |
|
133
|
15 |
|
public function writeXml(DOMDocument $dom = null) |
|
134
|
|
|
{ |
|
135
|
15 |
|
if (null === $dom) { |
|
136
|
1 |
|
$errMsg = 'Не передан DOMDocument'; |
|
137
|
1 |
|
throw new InvalidWriteWorkflowException($errMsg); |
|
138
|
|
|
} |
|
139
|
14 |
|
$descriptor = $dom->createElement('join'); |
|
140
|
|
|
|
|
141
|
14 |
|
if (!$this->hasId()) { |
|
142
|
1 |
|
$errMsg = 'Отсутствует атрибут id'; |
|
143
|
1 |
|
throw new InvalidDescriptorException($errMsg); |
|
144
|
|
|
} |
|
145
|
13 |
|
$id = $this->getId(); |
|
146
|
13 |
|
$descriptor->setAttribute('id', $id); |
|
147
|
|
|
|
|
148
|
13 |
|
$conditions = $this->getConditions(); |
|
149
|
13 |
|
foreach ($conditions as $condition) { |
|
150
|
12 |
|
$conditionElement = $condition->writeXml($dom); |
|
151
|
12 |
|
if (null !== $conditionElement) { |
|
152
|
12 |
|
$descriptor->appendChild($conditionElement); |
|
153
|
12 |
|
} |
|
154
|
13 |
|
} |
|
155
|
|
|
|
|
156
|
13 |
|
$result = $this->getResult(); |
|
157
|
|
|
|
|
158
|
13 |
|
if (null !== $result) { |
|
159
|
13 |
|
$resultElement = $result->writeXml($dom); |
|
160
|
13 |
|
$descriptor->appendChild($resultElement); |
|
161
|
13 |
|
} |
|
162
|
|
|
|
|
163
|
13 |
|
return $descriptor; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
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.