Chain   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 30
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 7 2
A __construct() 0 3 1
1
<?php
2
3
namespace alkemann\h2l\util;
4
5
use alkemann\h2l\exceptions;
6
use alkemann\h2l\Request;
7
use alkemann\h2l\Response;
8
9
/**
10
 * Class Chain
11
 *
12
 * Non-generic implementation, only meant for the Request to Response generation chain
13
 *
14
 * @package alkemann\h2l\util
15
 */
16
class Chain
17
{
18
    /**
19
     * @var array<callable>
20
     */
21
    private array $chain;
22
23
    /**
24
     * Constructor
25
     *
26
     * @param array<callable> $chained_callable
27
     */
28
    public function __construct(array $chained_callable = [])
29
    {
30
        $this->chain = $chained_callable;
31
    }
32
33
    /**
34
     * Take the first callable in the chain, remove it from que and call it, returning it's result
35
     *
36
     * @param Request $request
37
     * @return null|Response
38
     */
39
    public function next(Request $request): ?Response
40
    {
41
        if (empty($this->chain)) {
42
            throw new exceptions\EmptyChainError();
43
        }
44
        $next = array_shift($this->chain);
45
        return $next($request, $this);
46
    }
47
}
48