MiddlewareAwareTrait::resolveMiddleware()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 6
cts 8
cp 0.75
rs 8.4444
c 0
b 0
f 0
cc 8
nc 8
nop 2
crap 9
1
<?php declare(strict_types=1);
2
3
namespace League\Route\Middleware;
4
5
use InvalidArgumentException;
6
use OutOfBoundsException;
7
use Psr\Container\ContainerInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
10
trait MiddlewareAwareTrait
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $middleware = [];
16
17
    /**
18
     * {@inheritdoc}
19
     */
20 27
    public function middleware(MiddlewareInterface $middleware): MiddlewareAwareInterface
21
    {
22 27
        $this->middleware[] = $middleware;
23
24 27
        return $this;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 3
    public function middlewares(array $middlewares): MiddlewareAwareInterface
31
    {
32 3
        foreach ($middlewares as $middleware) {
33 3
            $this->middleware($middleware);
34
        }
35
36 3
        return $this;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 48
    public function prependMiddleware(MiddlewareInterface $middleware): MiddlewareAwareInterface
43
    {
44 48
        array_unshift($this->middleware, $middleware);
45
46 48
        return $this;
47
    }
48
49
    /**
50
     * Add a middleware as a class name to the stack
51
     *
52
     * @param string $middleware
53
     *
54
     * @return static
55
     */
56 3
    public function lazyMiddleware(string $middleware): MiddlewareAwareInterface
57
    {
58 3
        $this->middleware[] = $middleware;
59
60 3
        return $this;
61
    }
62
63
    /**
64
     * Add multiple middlewares as class names to the stack
65
     *
66
     * @param string[] $middlewares
67
     *
68
     * @return static
69
     */
70
    public function lazyMiddlewares(array $middlewares): MiddlewareAwareInterface
71
    {
72
        foreach ($middlewares as $middleware) {
73
            $this->lazyMiddleware($middleware);
74
        }
75
76
        return $this;
77
    }
78
79
    /**
80
     * Prepend a middleware as a class name to the stack
81
     *
82
     * @param string $middleware
83
     *
84
     * @return static
85
     */
86
    public function lazyPrependMiddleware(string $middleware): MiddlewareAwareInterface
87
    {
88
        array_unshift($this->middleware, $middleware);
89
90
        return $this;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 48
    public function shiftMiddleware(): MiddlewareInterface
97
    {
98 48
        $middleware =  array_shift($this->middleware);
99
100 48
        if ($middleware === null) {
101
            throw new OutOfBoundsException('Reached end of middleware stack. Does your controller return a response?');
102
        }
103
104 48
        return $middleware;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 51
    public function getMiddlewareStack(): iterable
111
    {
112 51
        return $this->middleware;
113
    }
114
115
    /**
116
     * Resolve a middleware implementation, optionally from a container
117
     *
118
     * @param MiddlewareInterface|string $middleware
119
     * @param ContainerInterface|null    $container
120
     *
121
     * @return MiddlewareInterface
122
     */
123 3
    protected function resolveMiddleware($middleware, ?ContainerInterface $container = null): MiddlewareInterface
124
    {
125 3
        if ($container === null && is_string($middleware) && class_exists($middleware)) {
126 3
            $middleware = new $middleware;
127
        }
128
129 3
        if ($container !== null && is_string($middleware) && $container->has($middleware)) {
130
            $middleware = $container->get($middleware);
131
        }
132
133 3
        if ($middleware instanceof MiddlewareInterface) {
134 3
            return $middleware;
135
        }
136
137
        throw new InvalidArgumentException(sprintf('Could not resolve middleware class: %s', $middleware));
138
    }
139
}
140