ResponsibilityChain::append()   A
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 6
eloc 11
c 3
b 1
f 0
nc 9
nop 3
dl 0
loc 21
rs 9.2222
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: frowhy
5
 * Date: 2019-03-08
6
 * Time: 11:23.
7
 */
8
9
namespace Modules\Core\Supports;
10
11
use Closure;
12
use Exception;
13
14
class ResponsibilityChain
15
{
16
    private $isException = false;
17
    private $isContinue = false;
18
    private $result = null;
19
    private $lastResult = null;
20
21
    public function append(Closure $result, bool $isLastResult = false, bool $isContinue = false): self
22
    {
23
        if (!$this->isException || $this->isContinue) {
24
            $this->isContinue = $isContinue;
25
26
            try {
27
                $this->result = $result($this->result);
28
            } catch (Exception $exception) {
29
                $this->result = Handler::renderException($exception);
30
            }
31
32
            if ($isLastResult) {
33
                $this->lastResult = $this->result;
34
            }
35
36
            if (!$this->result->isContinue()) {
37
                $this->isException = true;
38
            }
39
        }
40
41
        return $this;
42
    }
43
44
    public function handle(): Response
45
    {
46
        if (!is_null($this->lastResult)) {
47
            return $this->lastResult;
48
        }
49
50
        return $this->result;
51
    }
52
}
53