Completed
Push — master ( f0de6b...b60992 )
by Marcel
02:00
created

MiddlewareStack   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 43
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A compose() 0 9 2
A __construct() 0 4 1
A getSpec() 0 3 1
A __invoke() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UMA\JsonRpc\Internal;
6
7
use stdClass;
8
use UMA\JsonRpc;
9
10
final class MiddlewareStack implements JsonRpc\Procedure
11
{
12
    /**
13
     * @var JsonRpc\Middleware
14
     */
15
    private $middleware;
16
17
    /**
18
     * @var JsonRpc\Procedure
19
     */
20
    private $next;
21
22 15
    public static function compose(JsonRpc\Procedure $bottom, JsonRpc\Middleware ...$middlewares): JsonRpc\Procedure
23
    {
24 15
        $stack = $bottom;
25
26 15
        foreach ($middlewares as $middleware) {
27 2
            $stack = new self($middleware, $stack);
28
        }
29
30 15
        return $stack;
31
    }
32
33 2
    private function __construct(JsonRpc\Middleware $middleware, JsonRpc\Procedure $next)
34
    {
35 2
        $this->middleware = $middleware;
36 2
        $this->next = $next;
37 2
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 2
    public function __invoke(JsonRpc\Request $request): JsonRpc\Response
43
    {
44 2
        return ($this->middleware)($request, $this->next);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public function getSpec(): ?stdClass
51
    {
52 1
        return null;
53
    }
54
}
55