MiddlewareManager::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PTS\PSR15\MiddlewareManager;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Server\MiddlewareInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
10
class MiddlewareManager implements RequestHandlerInterface
11
{
12
    /** @var array */
13
    protected $next = [];
14
    /** @var PathResolver|null */
15
    protected $pathResolver;
16
17
    /**
18
     * @param ServerRequestInterface $request
19
     *
20
     * @return ResponseInterface
21
     * @throws \Throwable
22
     *
23
     * @deprecated
24
     */
25
    public function __invoke(ServerRequestInterface $request): ResponseInterface
26
    {
27
        return $this->handle($request);
28
    }
29
30
    public function get(int $index = 0): ?array
31
    {
32
        return $this->next[$index] ?? null;
33
    }
34
35
    public function setPathResolver(PathResolver $pathResolver): void
36
    {
37
        $this->pathResolver = $pathResolver;
38
    }
39
40
    /**
41
     * @param MiddlewareInterface $middleware
42
     *
43
     * @return $this
44
     *
45
     * @deprecated
46
     */
47
    public function push(MiddlewareInterface $middleware): self
48
    {
49
        return $this->use($middleware);
50
    }
51
52
    /**
53
     * @param MiddlewareInterface $middleware
54
     * @param string|null $path
55
     *
56
     * @return $this
57
     */
58
    public function use(MiddlewareInterface $middleware, string $path = null): self
59
    {
60
        $this->next[] = [
61
            $middleware,
62
            $path
63
        ];
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param ServerRequestInterface $request
70
     *
71
     * @return ResponseInterface
72
     * @throws \Throwable
73
     */
74
    public function handle(ServerRequestInterface $request): ResponseInterface
75
    {
76
        $runner = $this->createRunner();
77
        return $runner->handle($request);
78
    }
79
80
    /**
81
     * @return Runner
82
     */
83
    protected function createRunner(): Runner
84
    {
85
        $runner = new Runner($this);
86
        $runner->setPathResolver($this->pathResolver);
87
88
        return $runner;
89
    }
90
}
91