Completed
Push — master ( 75d280...2d4629 )
by Ievgen
08:05
created

AbstractStep::skip()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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 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;
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 4
    public function functionName() : string
41
    {
42 4
        is_callable($this->function, false, $functionName);
43 4
        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 3
    protected function normalizeStepResult($result) : array
51
    {
52 3
        $stepResult = $result;
53
        
54 3
        if (is_a($result, Result::class)) {
55 1
            if ($result->isSuccess()) {
56
                $stepResult = [
57 1
                    'type' => RESPONSE_TYPE_OK,
58 1
                    'appendParams' => $result->params()
59
                ];
60
            } else {
61
                $stepResult = [
62
                    'type' => RESPONSE_TYPE_ERROR,
63
                    'appendError' => $result->errors()
64
                ];
65
            }
66
        }
67
68 3
        return $stepResult;
69
    }
70
71
    abstract public function __invoke(&$params, string $track);
72
}
73