1
|
|
|
<?php |
2
|
|
|
namespace einfach\operation; |
3
|
|
|
|
4
|
|
|
use function einfach\operation\response\isError; |
5
|
|
|
use function einfach\operation\response\isOk; |
6
|
|
|
use function einfach\operation\response\isValidResponse; |
7
|
|
|
use const einfach\operation\response\RESPONSE_TYPE_ERROR; |
8
|
|
|
use const einfach\operation\response\RESPONSE_TYPE_OK; |
9
|
|
|
use einfach\operation\step\Step; |
10
|
|
|
use einfach\operation\step\Failure; |
11
|
|
|
use einfach\operation\step\AbstractStep; |
12
|
|
|
use einfach\operation\step\Always; |
13
|
|
|
use einfach\operation\step\TryCatch; |
14
|
|
|
|
15
|
|
|
class Railway |
16
|
|
|
{ |
17
|
|
|
const TRACK_SUCCESS = 'success_track'; |
18
|
|
|
const TRACK_FAILURE = 'failure_track'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \SplQueue |
22
|
|
|
*/ |
23
|
|
|
public $stepsQueue; |
24
|
|
|
/** |
25
|
|
|
* Accumulator of passed steps, while running railway |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $signaturesPipeline = []; |
30
|
|
|
|
31
|
6 |
|
public function __construct() |
32
|
|
|
{ |
33
|
6 |
|
$this->stepsQueue = []; |
|
|
|
|
34
|
6 |
|
} |
35
|
|
|
|
36
|
|
|
protected function findTargetStepIndex($stepName) |
37
|
|
|
{ |
38
|
|
|
$steps = array_column($this->stepsQueue, 'step'); |
39
|
|
|
$names = array_map(function ($step) { |
40
|
|
|
return $step->name(); |
41
|
|
|
}, $steps); |
42
|
|
|
|
43
|
|
|
$targetIndex = array_search($stepName, $names); |
44
|
|
|
return $targetIndex; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
protected function insertStepBefore(string $stepName, AbstractStep $stepObject, array $opt) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$targetIndex = $this->findTargetStepIndex($stepName); |
50
|
|
|
$step = [ |
51
|
|
|
'step' => $stepObject, |
52
|
|
|
'opt' => $opt |
53
|
|
|
]; |
54
|
|
|
array_splice($this->stepsQueue, $targetIndex, 0, [$step]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
protected function insertStepAfter(string $stepName, AbstractStep $stepObject, array $opt) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$targetIndex = $this->findTargetStepIndex($stepName); |
60
|
|
|
$step = [ |
61
|
|
|
'step' => $stepObject, |
62
|
|
|
'opt' => $opt |
63
|
|
|
]; |
64
|
|
|
array_splice($this->stepsQueue, $targetIndex + 1, 0, [$step]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
protected function replaceStep(string $stepName, AbstractStep $stepObject, array $opt) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$targetIndex = $this->findTargetStepIndex($stepName); |
70
|
|
|
$step = [ |
71
|
|
|
'step' => $stepObject, |
72
|
|
|
'opt' => $opt |
73
|
|
|
]; |
74
|
|
|
array_splice($this->stepsQueue, $targetIndex, 1, [$step]); |
75
|
|
|
} |
76
|
|
|
|
77
|
6 |
|
protected function addStep(AbstractStep $stepObject, array $opt) |
78
|
|
|
{ |
79
|
6 |
|
$this->stepsQueue[] = [ |
80
|
6 |
|
'step' => $stepObject, |
81
|
6 |
|
'opt' => $opt |
82
|
|
|
]; |
83
|
6 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* @param AbstractStep $stepObject |
88
|
|
|
* @param array $opt |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
6 |
|
public function rawStep(AbstractStep $stepObject, $opt = []) : Railway |
92
|
|
|
{ |
93
|
6 |
|
$before = $opt['before'] ?? null; |
94
|
6 |
|
$after = $opt['after'] ?? null; |
95
|
6 |
|
$replace = $opt['replace'] ?? null; |
96
|
|
|
|
97
|
6 |
|
if ($before) { |
98
|
|
|
$this->insertStepBefore($before, $stepObject, $opt); |
99
|
6 |
|
} elseif ($after) { |
100
|
|
|
$this->insertStepAfter($after, $stepObject, $opt); |
101
|
6 |
|
} elseif ($replace) { |
102
|
|
|
$this->replaceStep($replace, $stepObject, $opt); |
103
|
|
|
} else { |
104
|
6 |
|
$this->addStep($stepObject, $opt); |
105
|
|
|
} |
106
|
|
|
|
107
|
6 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
6 |
|
public function step(callable $callable, $opt = []) : Railway |
111
|
|
|
{ |
112
|
6 |
|
$name = $opt['name'] ?? null; |
113
|
6 |
|
return $this->rawStep(new Step($callable, $name), $opt); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function always(callable $callable, $opt = []) : Railway |
117
|
|
|
{ |
118
|
|
|
$name = $opt['name'] ?? null; |
119
|
|
|
return $this->rawStep(new Always($callable, $name), $opt); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function failure(callable $callable, $opt = []) : Railway |
123
|
|
|
{ |
124
|
|
|
$name = $opt['name'] ?? null; |
125
|
|
|
return $this->rawStep(new Failure($callable, $name), $opt); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function tryCatch(callable $callable, $opt = []) : Railway |
129
|
|
|
{ |
130
|
|
|
$name = $opt['name'] ?? null; |
131
|
|
|
return $this->rawStep(new TryCatch($callable, $name), $opt); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function removeStep(string $stepName){ |
135
|
|
|
$targetIndex = $this->findTargetStepIndex($stepName); |
136
|
|
|
unset($this->stepsQueue[$targetIndex]); |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param $params |
142
|
|
|
* @return Result |
143
|
|
|
* @throws \Exception |
144
|
|
|
*/ |
145
|
6 |
|
public function runWithParams($params) |
146
|
|
|
{ |
147
|
|
|
// a bit hardcoded, but let it be :) |
148
|
6 |
|
$params['__errors'] = []; |
149
|
|
|
|
150
|
6 |
|
$track = self::TRACK_SUCCESS; |
151
|
6 |
|
foreach ($this->stepsQueue as $item) { |
152
|
6 |
|
$step = $item['step']; |
153
|
6 |
|
$opt = $item['opt']; |
154
|
|
|
/** |
155
|
|
|
* @var $step AbstractStep |
156
|
|
|
*/ |
157
|
6 |
|
$track = $this->performStep($step, $params, $opt, $track); |
158
|
|
|
} |
159
|
5 |
|
return new Result($params, $track, $this->signaturesPipeline); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @throws \Exception |
164
|
|
|
*/ |
165
|
6 |
|
protected function performStep($step, &$params, $opt, $track) |
166
|
|
|
{ |
167
|
6 |
|
$nextTrack = $track; |
168
|
6 |
|
$stepResult = $step($params, $track, $opt); |
169
|
|
|
|
170
|
5 |
|
if (!$step->isSkipped()) { |
171
|
5 |
|
if (isValidResponse($stepResult)) { |
172
|
5 |
|
if (isOk($stepResult)) { |
173
|
4 |
|
$nextTrack = self::TRACK_SUCCESS; |
174
|
1 |
|
} elseif (isError($stepResult)) { |
175
|
1 |
|
$nextTrack = self::TRACK_FAILURE; |
176
|
|
|
} |
177
|
5 |
|
$params = $stepResult['params']; |
178
|
5 |
|
$this->signaturesPipeline[] = $step->signature(); |
179
|
|
|
} else { |
180
|
|
|
$actualResult = var_export($stepResult, true); |
181
|
|
|
throw new \Exception("Step returned incorrectly formatted result: {$actualResult}"); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
5 |
|
return $nextTrack; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..