AbstractStep   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
dl 0
loc 94
rs 10
c 0
b 0
f 0
ccs 36
cts 39
cp 0.9231
wmc 13
lcom 2
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A isSkipped() 0 4 1
A skip() 0 4 1
A functionSignature() 0 5 1
A name() 0 4 1
A signature() 0 5 1
C normalizeStepResponse() 0 34 7
__invoke() 0 1 ?
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
    protected $opt;
26
27 26
    public function __construct(callable $callable, string $name = null, array $opt = [])
28
    {
29 26
        $this->function = $callable;
30 26
        $this->name = $name;
31 26
        $this->skipped = false;
32 26
        $this->opt = $opt;
33 26
    }
34
35 24
    public function isSkipped() : bool
36
    {
37 24
        return true == $this->skipped;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
38
    }
39
40 7
    public function skip() : bool
41
    {
42 7
        return $this->skipped = true;
43
    }
44
45 7
    public function functionSignature() : string
46
    {
47 7
        is_callable($this->function, false, $functionName);
48 7
        return $functionName;
49
    }
50
51
    /**
52
     * Step name, respecting custom name
53
     */
54 8
    public function name() : string
55
    {
56 8
        return $this->name ?? $this->functionSignature();
57
    }
58
59 5
    public function signature($template = "%-10s | %s") : string
60
    {
61 5
        $className = (new \ReflectionClass($this))->getShortName();
62 5
        return sprintf($template, $className, $this->name());
63
    }
64
65
    /**
66
     * Transform all results into valid resut array form
67
     * It could be Result object instance for nested steps
68
     */
69 17
    protected function normalizeStepResponse($result) : array
70
    {
71 17
        $stepResult = $result;
72
        
73 17
        if (is_a($result, Result::class)) {
74
             $stepResult = [
75 5
                    'params' => $result->params(),
76 5
                    'type' => ($result->isSuccess()) ? RESPONSE_TYPE_OK : RESPONSE_TYPE_ERROR
77
                ];
78
        }
79
80 17
        if (!isValidResponse($stepResult)) {
81
            $actualResult = var_export($stepResult, true);
82
            throw new \Exception("Step '{$this->name()}' returned incorrectly formatted result. \
83
            Maybe you forgot to return `ok(\$params)` or `error(\$params)`. \
84
            Current return: {$actualResult}");
85
        }
86
87 17
        if (isOk($stepResult)) {
88 9
            $appendParams = $stepResult['appendParams'] ?? [];
89 9
            $stepResult['params'] = $stepResult['params'] ?? [];
90 9
            $stepResult['params'] = array_merge($stepResult['params'], $appendParams);
91 9
            unset($stepResult['appendParams']);
92 8
        } elseif (isError($stepResult)) {
93 8
            $appendError = $stepResult['appendError'] ?? [];
94 8
            $stepResult['params']['__errors'] = $stepResult['params']['__errors'] ?? [];
95 8
            if ($appendError) {
96 4
                $stepResult['params']['__errors'] = $stepResult['params']['__errors'] + $appendError;
97
            }
98 8
            unset($stepResult['appendError']);
99
        }
100
101 17
        return $stepResult;
102
    }
103
104
    abstract public function __invoke(&$params, string $track);
105
}
106