Completed
Push — master ( 78748d...6b0950 )
by Ievgen
02:46
created

Railway::getStepClassName()   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 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace einfach\operation;
3
4
use function einfach\operation\response\isError;
5
use function einfach\operation\response\isOk;
6
use function einfach\operation\response\isValidResponse;
7
use const einfach\operation\response\RESPONSE_TYPE_ERROR;
8
use const einfach\operation\response\RESPONSE_TYPE_OK;
9
use einfach\operation\step\Step;
10
use einfach\operation\step\Failure;
11
use einfach\operation\step\AbstractStep;
12
use einfach\operation\step\Always;
13
use einfach\operation\step\TryCatch;
14
15
class Railway
16
{
17
    const TRACK_SUCCESS = 'success_track';
18
    const TRACK_FAILURE = 'failure_track';
19
20
    /**
21
     * @var \SplQueue
22
     */
23
    protected $stepsQueue;
24
25 1
    public function __construct()
26
    {
27 1
        $this->stepsQueue = new \SplQueue();
28 1
    }
29
30
    /**
31
     *
32
     * @param AbstractStep $stepObject
33
     * @param array $opt
34
     * @return $this
35
     */
36 1
    public function rawStep(AbstractStep $stepObject, $opt = []) : Railway
0 ignored issues
show
Unused Code introduced by
The parameter $opt is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    {
38 1
        $this->stepsQueue->enqueue($stepObject);
39 1
        return $this;
40
    }
41
42 1
    public function step(callable $callable, $opt = []) : Railway
43
    {
44 1
        $name = $opt['name'] ?? null;
45 1
        return $this->rawStep(new Step($callable, $name), $opt);
46
    }
47
48
    public function always(callable $callable, $opt = []) : Railway
49
    {
50
        $name = $opt['name'] ?? null;
51
        return $this->rawStep(new Always($callable, $name), $opt);
52
    }
53
54
    public function failure(callable $callable, $opt = []) : Railway
55
    {
56
        $name = $opt['name'] ?? null;
57
        return $this->rawStep(new Failure($callable, $name), $opt);
58
    }
59
60
    public function tryCatch(callable $callable, $opt = []) : Railway
61
    {
62
        $name = $opt['name'] ?? null;
63
        return $this->rawStep(new TryCatch($callable, $name), $opt);
64
    }
65
66
    /**
67
     * @param $params
68
     * @return Result
69
     * @throws \Exception
70
     */
71 1
    public function runWithParams($params)
72
    {
73
        // a bit hardcoded, but let it be :)
74 1
        $params['errors'] = [];
75 1
        $signaturesPipeline = [];
76
77 1
        $track = self::TRACK_SUCCESS;
78 1
        foreach ($this->stepsQueue as $step) {
79
            /** @var $step AbstractStep */
80 1
            $track = $this->performStep($step, $params, $track, $signaturesPipeline);
81
        }
82 1
        return new Result($params, $track, $signaturesPipeline);
83
    }
84
85
    /**
86
     * @param $params
87
     * @param $step
88
     * @return string
89
     * @throws \Exception
90
     */
91 1
    protected function performStep($step, &$params, $track, &$signaturesPipeline)
92
    {
93 1
        $newTrack = $track;
94 1
        $stepResult = $step($params, $track);
95
96 1
        if (!$step->isSkipped()) {
97 1
            if (isValidResponse($stepResult)) {
98 1
                $type = $stepResult['type'];
99 1
                if (isOk($type)) {
100 1
                    $newTrack = self::TRACK_SUCCESS;
101 1
                    $appendParams = $stepResult['appendParams'] ?? [];
102 1
                    $params = array_merge($params, $appendParams);
103
                } elseif (isError($type)) {
104
                    $newTrack = self::TRACK_FAILURE;
105
                    $appendError = [$stepResult['appendError']] ?? [];
106
                    // TODO: Fix errors merge
107
                    print_r($params['errors']);
108
                    print_r($appendError);
109
                    $params['errors'] = $params['errors'] + $appendError;
110
                }
111
112 1
                $signaturesPipeline[] = $step->signature();
113
            } else {
114
                $actualResult = var_export($stepResult, true);
115
                throw new \Exception("Step returned incorrectly formatted result: {$actualResult}");
116
            }
117
        }
118
119 1
        return $newTrack;
120
    }
121
}
122