AbstractChain::runValidate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 3.072
rs 10
1
<?php
2
3
namespace DrLenux\Chain;
4
5
/**
6
 * Class AbstractChain
7
 * @package DrLenux\Chain
8
 */
9
abstract class AbstractChain
10
{
11
    /**
12
     * @var AbstractChain
13
     */
14
    private $next;
15
16
    /**
17
     * @var array
18
     */
19
    private $request = [];
20
21
    /**
22
     * @var Response|null
23
     */
24
    private $response = null;
25
26
    /**
27
     * @param AbstractChain $next
28
     * @return AbstractChain
29
     */
30 4
    public function setNext(AbstractChain $next): AbstractChain
31
    {
32 4
        $this->next = $next;
33 4
        return $next;
34
    }
35
36
    /**
37
     * @param array $request
38
     * @param Response|null $response
39
     * @return Response|null
40
     */
41 4
    public function handler(array $request, ?Response $response = null): ?Response
42
    {
43 4
        $this->request = $request;
44 4
        $this->response = $response;
45
46 4
        if ($response !== null) {
47 3
            if ($response->getNextStage() !== static::getStage()) {
48 2
                return $this->nextRun($request, $response);
49
            }
50
        }
51
52 4
        if (!$this->runValidate()) {
53
            return null;
54
        }
55
56 4
        $responseRun = $this->run();
57
58 4
        if ($responseRun !== null) {
59 4
            if ($responseRun->getNextStage() === null) {
60 4
                return $responseRun;
61
            }
62 3
            return $this->nextRun($request, $responseRun);
63
        } else {
64 3
            return $this->nextRun($request, $response);
65
        }
66
    }
67
68
    /**
69
     * @param $request
70
     * @param $response
71
     * @return Response
72
     */
73 3
    private function nextRun($request, $response): ?Response
74
    {
75 3
        if ($this->next !== null) {
76 3
            return $this->next->handler($request, $response);
77
        } else {
78
            return $response;
79
        }
80
    }
81
82
    /**
83
     * @return bool
84
     */
85 4
    private function runValidate(): bool
86
    {
87 4
        foreach ($this->validate() as $key) {
88 4
            if (empty($this->request[$key])) {
89
                return false;
90
            }
91
        }
92 4
        return true;
93
    }
94
95
    /**
96
     * @return array
97
     */
98 3
    protected function validate(): array
99
    {
100 3
        return [];
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    abstract static public function getStage(): string;
107
108
    /**
109
     * @return Response|null
110
     */
111
    abstract protected function run(): ?Response;
112
113
    /**
114
     * @return array
115
     */
116 4
    public function getRequest(): array
117
    {
118 4
        return $this->request;
119
    }
120
121
    /**
122
     * @param $name
123
     * @return mixed|null
124
     */
125 4
    public function __get($name)
126
    {
127 4
        return $this->getRequest()[$name] ?? null;
128
    }
129
130
    /**
131
     * @return Response|null
132
     */
133 3
    public function getResponse(): ?Response
134
    {
135 3
        return $this->response;
136
    }
137
}