MiddlewareAwareTrait   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 19
eloc 28
c 3
b 0
f 0
dl 0
loc 80
ccs 35
cts 35
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A lazyPrependMiddleware() 0 4 1
A middlewares() 0 7 2
A lazyMiddlewares() 0 7 2
A getMiddlewareStack() 0 3 1
A prependMiddleware() 0 4 1
A lazyMiddleware() 0 4 1
A middleware() 0 4 1
B resolveMiddleware() 0 15 8
A shiftMiddleware() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Route\Middleware;
6
7
use InvalidArgumentException;
8
use OutOfBoundsException;
9
use Psr\Container\ContainerInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
12
trait MiddlewareAwareTrait
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $middleware = [];
18
19 75
    public function getMiddlewareStack(): iterable
20
    {
21 75
        return $this->middleware;
22
    }
23
24 3
    public function lazyMiddleware(string $middleware): MiddlewareAwareInterface
25
    {
26 3
        $this->middleware[] = $middleware;
27 3
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type League\Route\Middleware\MiddlewareAwareTrait which is incompatible with the type-hinted return League\Route\Middleware\MiddlewareAwareInterface.
Loading history...
28
    }
29
30 3
    public function lazyMiddlewares(array $middlewares): MiddlewareAwareInterface
31
    {
32 3
        foreach ($middlewares as $middleware) {
33 3
            $this->lazyMiddleware($middleware);
34
        }
35
36 3
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type League\Route\Middleware\MiddlewareAwareTrait which is incompatible with the type-hinted return League\Route\Middleware\MiddlewareAwareInterface.
Loading history...
37
    }
38
39 3
    public function lazyPrependMiddleware(string $middleware): MiddlewareAwareInterface
40
    {
41 3
        array_unshift($this->middleware, $middleware);
42 3
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type League\Route\Middleware\MiddlewareAwareTrait which is incompatible with the type-hinted return League\Route\Middleware\MiddlewareAwareInterface.
Loading history...
43
    }
44
45 54
    public function middleware(MiddlewareInterface $middleware): MiddlewareAwareInterface
46
    {
47 54
        $this->middleware[] = $middleware;
48 54
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type League\Route\Middleware\MiddlewareAwareTrait which is incompatible with the type-hinted return League\Route\Middleware\MiddlewareAwareInterface.
Loading history...
49
    }
50
51 3
    public function middlewares(array $middlewares): MiddlewareAwareInterface
52
    {
53 3
        foreach ($middlewares as $middleware) {
54 3
            $this->middleware($middleware);
55
        }
56
57 3
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type League\Route\Middleware\MiddlewareAwareTrait which is incompatible with the type-hinted return League\Route\Middleware\MiddlewareAwareInterface.
Loading history...
58
    }
59
60 75
    public function prependMiddleware(MiddlewareInterface $middleware): MiddlewareAwareInterface
61
    {
62 75
        array_unshift($this->middleware, $middleware);
63 75
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type League\Route\Middleware\MiddlewareAwareTrait which is incompatible with the type-hinted return League\Route\Middleware\MiddlewareAwareInterface.
Loading history...
64
    }
65
66 78
    public function shiftMiddleware(): MiddlewareInterface
67
    {
68 78
        $middleware =  array_shift($this->middleware);
69
70 78
        if ($middleware === null) {
71 3
            throw new OutOfBoundsException('Reached end of middleware stack. Does your controller return a response?');
72
        }
73
74 78
        return $middleware;
75
    }
76
77 12
    protected function resolveMiddleware($middleware, ?ContainerInterface $container = null): MiddlewareInterface
78
    {
79 12
        if ($container === null && is_string($middleware) && class_exists($middleware)) {
80 6
            $middleware = new $middleware();
81
        }
82
83 12
        if ($container !== null && is_string($middleware) && $container->has($middleware)) {
84 3
            $middleware = $container->get($middleware);
85
        }
86
87 12
        if ($middleware instanceof MiddlewareInterface) {
88 9
            return $middleware;
89
        }
90
91 3
        throw new InvalidArgumentException(sprintf('Could not resolve middleware class: %s', $middleware));
92
    }
93
}
94