1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace einfach\operation\step; |
4
|
|
|
|
5
|
|
|
use const einfach\operation\response\RESPONSE_TYPE_ERROR; |
6
|
|
|
use const einfach\operation\response\RESPONSE_TYPE_OK; |
7
|
|
|
use function einfach\operation\response\isValidResponse; |
8
|
|
|
use function einfach\operation\response\isOk; |
9
|
|
|
use function einfach\operation\response\isError; |
10
|
|
|
use einfach\operation\Result; |
11
|
|
|
|
12
|
|
|
abstract class AbstractStep |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var callable |
16
|
|
|
*/ |
17
|
|
|
public $function; |
18
|
|
|
protected $name; |
19
|
|
|
/** |
20
|
|
|
* Indicates was this step performed or not |
21
|
|
|
* |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $skipped; |
25
|
|
|
|
26
|
26 |
|
public function __construct(callable $callable, string $name = null) |
27
|
|
|
{ |
28
|
26 |
|
$this->function = $callable; |
29
|
26 |
|
$this->name = $name; |
30
|
26 |
|
$this->skipped = false; |
31
|
26 |
|
} |
32
|
|
|
|
33
|
24 |
|
public function isSkipped() : bool |
34
|
|
|
{ |
35
|
24 |
|
return true == $this->skipped; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
7 |
|
public function skip() : bool |
39
|
|
|
{ |
40
|
7 |
|
return $this->skipped = true; |
41
|
|
|
} |
42
|
|
|
|
43
|
7 |
|
public function functionSignature() : string |
44
|
|
|
{ |
45
|
7 |
|
is_callable($this->function, false, $functionName); |
46
|
7 |
|
return $functionName; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Step name, respecting custom name |
51
|
|
|
*/ |
52
|
8 |
|
public function name() : string |
53
|
|
|
{ |
54
|
8 |
|
return $this->name ?? $this->functionSignature(); |
55
|
|
|
} |
56
|
|
|
|
57
|
5 |
|
public function signature($template = "%-10s | %s") : string |
58
|
|
|
{ |
59
|
5 |
|
$className = (new \ReflectionClass($this))->getShortName(); |
60
|
5 |
|
return sprintf($template, $className, $this->name()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Transform all results into valid resut array form |
65
|
|
|
* It could be Result object instance for nested steps |
66
|
|
|
*/ |
67
|
17 |
|
protected function normalizeStepResponse($result) : array |
68
|
|
|
{ |
69
|
17 |
|
$stepResult = $result; |
70
|
|
|
|
71
|
17 |
|
if (is_a($result, Result::class)) { |
72
|
|
|
$stepResult = [ |
73
|
5 |
|
'params' => $result->params(), |
74
|
5 |
|
'type' => ($result->isSuccess()) ? RESPONSE_TYPE_OK : RESPONSE_TYPE_ERROR |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
17 |
|
if (!isValidResponse($stepResult)) { |
79
|
|
|
$actualResult = var_export($stepResult, true); |
80
|
|
|
throw new \Exception("Step '{$this->name()}' returned incorrectly formatted result. \ |
81
|
|
|
Maybe you forgot to return `ok(\$params)` or `error(\$params)`. \ |
82
|
|
|
Current return: {$actualResult}"); |
83
|
|
|
} |
84
|
|
|
|
85
|
17 |
|
if (isOk($stepResult)) { |
86
|
9 |
|
$appendParams = $stepResult['appendParams'] ?? []; |
87
|
9 |
|
$stepResult['params'] = $stepResult['params'] ?? []; |
88
|
9 |
|
$stepResult['params'] = array_merge($stepResult['params'], $appendParams); |
89
|
9 |
|
unset($stepResult['appendParams']); |
90
|
8 |
|
} elseif (isError($stepResult)) { |
91
|
8 |
|
$appendError = $stepResult['appendError'] ?? []; |
92
|
8 |
|
$stepResult['params']['__errors'] = $stepResult['params']['__errors'] ?? []; |
93
|
8 |
|
if ($appendError) { |
94
|
4 |
|
$stepResult['params']['__errors'] = $stepResult['params']['__errors'] + $appendError; |
95
|
|
|
} |
96
|
8 |
|
unset($stepResult['appendError']); |
97
|
|
|
} |
98
|
|
|
|
99
|
17 |
|
return $stepResult; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
abstract public function __invoke(&$params, string $track); |
103
|
|
|
} |
104
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.