Passed
Push — master ( 2e0c74...259d67 )
by Korotkov
08:40 queued 07:19
created

AbstractHandler::setNext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 2
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @author  : Jagepard <[email protected]>
5
 * @license https://mit-license.org/ MIT
6
 */
7
8
namespace Behavioral\ChainOfResponsibility;
9
10
abstract class AbstractHandler implements ChainInterface
11
{
12
    protected AbstractHandler $nextHandler;
13
14
    /**
15
     * @param string $request
16
     */
17
    public function execute(string $request): void
18
    {
19
        if ($request === get_called_class()) {
20
            printf("%s %s".PHP_EOL, get_called_class(), "has handle an error");
21
            return;
22
        }
23
24
        if (!isset($this->nextHandler)) {
25
            throw new \InvalidArgumentException($request . " does not exist in the chain");
26
        }
27
28
        $this->nextHandler->execute($request);
29
    }
30
31
    /**
32
     * @param AbstractHandler $handler
33
     * @return AbstractHandler
34
     */
35
    public function setNext(AbstractHandler $handler): AbstractHandler
36
    {
37
        $this->nextHandler = $handler;
38
        return $handler;
39
    }
40
}
41