Passed
Push — master ( 71075d...a0e443 )
by Korotkov
03:25 queued 01:41
created

AbstractHandler::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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 string $name;
13
    protected AbstractHandler $nextHandler;
14
15
    /**
16
     * If the condition matches, code is executed;
17
     * if not, then it is passed along the chain to the next handler
18
     *
19
     * @param string $request
20
     */
21
    public function execute(string $request): void
22
    {
23
        // In case of compliance, the code is executed
24
        if ($request === $this->getName()) {
25
            printf("%s %s\n", get_called_class(), "has handle a request");
26
            return;
27
        }
28
29
        if (!isset($this->nextHandler)) {
30
            throw new \InvalidArgumentException($request . " does not exist in the chain");
31
        }
32
33
        // Passed to the next handler
34
        $this->nextHandler->execute($request);
35
    }
36
37
    public function executeAllInChainBeforeRequest(string $request): void
38
    {
39
        printf("%s %s\n", get_called_class(), "has handle a request");
40
41
        if ($request === $this->getName()) {
42
            return;
43
        }
44
45
        if (!isset($this->nextHandler)) {
46
            throw new \InvalidArgumentException($request . " does not exist in the chain");
47
        }
48
49
        // Passed to the next handler
50
        $this->nextHandler->executeAllInChainBeforeRequest($request);
51
    }
52
53
    /**
54
     * Adds the next handler to the chain
55
     *
56
     * @param AbstractHandler $handler
57
     * @return AbstractHandler
58
     */
59
    public function setNext(AbstractHandler $handler): AbstractHandler
60
    {
61
        $this->nextHandler = $handler;
62
        return $handler;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getName(): string
69
    {
70
        return $this->name;
71
    }
72
}
73