AbstractHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 4
Metric Value
wmc 6
eloc 16
c 6
b 0
f 4
dl 0
loc 54
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setNext() 0 4 1
A execute() 0 26 5
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
     * If the condition matches, code is executed;
16
     * if not, then it is passed along the chain to the next handler
17
     * -------------------------------------------------------------
18
     * Если условие совпадает, код выполняется;
19
     * Если нет, то он передается по цепочке следующему обработчику
20
     *
21
     * @param string $request
22
     * @param bool $allInChain
23
     */
24
    public function execute(string $request, bool $allInChain = false): void
25
    {
26
        $calledClassName = get_called_class();
27
28
        if ($allInChain) {
29
            printf("%s %s\n", $calledClassName, "has handle a request");
30
31
            if ($request === $calledClassName) {
32
                return;
33
            }
34
        } else {
35
            // In case of compliance, the code is executed
36
            // В случае соответствия код выполняется
37
            if ($request === $calledClassName) {
38
                printf("%s %s\n", $calledClassName, "has handle a request");
39
                return;
40
            }
41
        }
42
43
        if (!isset($this->nextHandler)) {
44
            throw new \InvalidArgumentException($request . " does not exist in the chain");
45
        }
46
47
        // Passed to the next handler
48
        // Передано следующему обработчику
49
        $this->nextHandler->execute($request, $allInChain);
50
    }
51
52
    /**
53
     * Adds the next handler to the chain
54
     * ----------------------------------
55
     * Добавляет следующий обработчик в цепочку
56
     *
57
     * @param AbstractHandler $handler
58
     * @return AbstractHandler
59
     */
60
    public function setNext(AbstractHandler $handler): AbstractHandler
61
    {
62
        $this->nextHandler = $handler;
63
        return $handler;
64
    }
65
}
66