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 einfach\operation\Result; |
8
|
|
|
|
9
|
|
|
abstract class AbstractStep |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var callable |
13
|
|
|
*/ |
14
|
|
|
public $function; |
15
|
|
|
protected $name; |
16
|
|
|
/** |
17
|
|
|
* Indicates was this step performed or not |
18
|
|
|
* |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
protected $skipped; |
22
|
|
|
|
23
|
8 |
|
public function __construct(callable $callable, string $name = null) |
24
|
|
|
{ |
25
|
8 |
|
$this->function = $callable; |
26
|
8 |
|
$this->name = $name; |
27
|
8 |
|
$this->skipped = false; |
28
|
8 |
|
} |
29
|
|
|
|
30
|
6 |
|
public function isSkipped() : bool |
31
|
|
|
{ |
32
|
6 |
|
return true == $this->skipped; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
|
public function skip() : bool |
36
|
|
|
{ |
37
|
3 |
|
return $this->skipped = true; |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
public function functionSignature() : string |
41
|
|
|
{ |
42
|
3 |
|
is_callable($this->function, false, $functionName); |
43
|
3 |
|
return $functionName; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Step name, respecting custom name |
48
|
|
|
*/ |
49
|
3 |
|
public function name() : string |
50
|
|
|
{ |
51
|
3 |
|
return $this->name ?? $this->functionSignature(); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
public function signature($template = "%-10s | %s") : string |
55
|
|
|
{ |
56
|
1 |
|
$className = (new \ReflectionClass($this))->getShortName(); |
57
|
1 |
|
return sprintf($template, $className, $this->name()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Transform all results into valid resut array form |
62
|
|
|
* It could be Result object instance for nested steps |
63
|
|
|
*/ |
64
|
3 |
|
protected function normalizeStepResult($result) : array |
65
|
|
|
{ |
66
|
3 |
|
$stepResult = $result; |
67
|
|
|
|
68
|
3 |
|
if (is_a($result, Result::class)) { |
69
|
1 |
|
if ($result->isSuccess()) { |
70
|
|
|
$stepResult = [ |
71
|
1 |
|
'type' => RESPONSE_TYPE_OK, |
72
|
1 |
|
'appendParams' => $result->params() |
73
|
|
|
]; |
74
|
|
|
} else { |
75
|
|
|
$stepResult = [ |
76
|
|
|
'type' => RESPONSE_TYPE_ERROR, |
77
|
|
|
'appendError' => $result->errors() |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
return $stepResult; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
abstract public function __invoke(&$params, string $track); |
86
|
|
|
} |
87
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.