Chain::next()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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