Completed
Push — master ( 0e291a...511168 )
by Ievgen
05:31
created

AbstractStep::functionName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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;
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...
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