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

AbstractHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 4
eloc 10
c 3
b 0
f 1
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setNext() 0 4 1
A execute() 0 12 3
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