|
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
|
7 |
|
public function __construct(callable $callable, string $name = null) |
|
24
|
|
|
{ |
|
25
|
7 |
|
$this->function = $callable; |
|
26
|
7 |
|
$this->name = $name; |
|
27
|
7 |
|
$this->skipped = false; |
|
28
|
7 |
|
} |
|
29
|
|
|
|
|
30
|
5 |
|
public function isSkipped() : bool |
|
31
|
|
|
{ |
|
32
|
5 |
|
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 functionName() : string |
|
41
|
|
|
{ |
|
42
|
3 |
|
is_callable($this->function, false, $functionName); |
|
43
|
3 |
|
return $this->name ?? $functionName; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Transform all results into valid resut array form |
|
48
|
|
|
* It could be Result object instance for nested steps |
|
49
|
|
|
*/ |
|
50
|
2 |
|
protected function normalizeStepResult($result) : array { |
|
51
|
2 |
|
$stepResult = $result; |
|
52
|
|
|
|
|
53
|
2 |
|
if (is_a($result, Result::class)) { |
|
54
|
|
|
if ($result->isSuccess()) { |
|
55
|
|
|
$stepResult = [ |
|
56
|
|
|
'type' => RESPONSE_TYPE_OK, |
|
57
|
|
|
'appendParams' => $result->params() |
|
58
|
|
|
]; |
|
59
|
|
|
} else { |
|
60
|
|
|
$stepResult = [ |
|
61
|
|
|
'type' => RESPONSE_TYPE_ERROR, |
|
62
|
|
|
'appendError' => $result->errors() |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
return $stepResult; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
abstract public function __invoke(&$params, string $track); |
|
71
|
|
|
} |
|
72
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.