Passed
Pull Request — master (#224)
by
unknown
13:06
created

MiddlewareDispatcher.php$0 ➔ handle()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yiisoft\Yii\Web;
4
5
use Psr\Container\ContainerInterface;
6
use Psr\Http\Message\ResponseFactoryInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Yiisoft\Yii\Web\Middleware\Callback;
12
use Yiisoft\Yii\Web\RequestHandler\MiddlewareHandler;
13
use Yiisoft\Yii\Web\RequestHandler\NotFoundHandler;
14
15
/**
16
 * MiddlewareDispatcher
17
 */
18
final class MiddlewareDispatcher implements MiddlewareInterface
19
{
20
    /**
21
     * @var MiddlewareInterface[]
22
     */
23
    private array $middlewares = [];
24
25
    private RequestHandlerInterface $nextHandler;
26
    private ContainerInterface $container;
27
28
    /**
29
     * Contains a chain of middleware wrapped in handlers.
30
     * Each handler points to the handler of middleware that will be processed next.
31
     * @var RequestHandlerInterface|null stack of middleware
32
     */
33
    private ?RequestHandlerInterface $stack = null;
34 3
35
    public function __construct(
36
        ContainerInterface $container,
37
        RequestHandlerInterface $nextHandler = null
38
    ) {
39 3
        $this->container = $container;
40 1
        $this->nextHandler = $nextHandler ?? new NotFoundHandler($container->get(ResponseFactoryInterface::class));
41
    }
42
43 3
    /**
44
     * @param callable|MiddlewareInterface $middleware
45 3
     * @return self
46 3
     */
47
    public function addMiddleware($middleware): self
48
    {
49 3
        if (is_callable($middleware)) {
50
            $middleware = new Callback($middleware, $this->container);
0 ignored issues
show
Bug introduced by
It seems like $middleware can also be of type Psr\Http\Server\MiddlewareInterface; however, parameter $callback of Yiisoft\Yii\Web\Middleware\Callback::__construct() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            $middleware = new Callback(/** @scrutinizer ignore-type */ $middleware, $this->container);
Loading history...
51 3
        }
52
53
        if (!$middleware instanceof MiddlewareInterface) {
54
            throw new \InvalidArgumentException('Middleware should be either callable or MiddlewareInterface instance. ' . get_class($middleware) . ' given.');
0 ignored issues
show
Bug introduced by
$middleware of type callable is incompatible with the type object expected by parameter $object of get_class(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
            throw new \InvalidArgumentException('Middleware should be either callable or MiddlewareInterface instance. ' . get_class(/** @scrutinizer ignore-type */ $middleware) . ' given.');
Loading history...
55
        }
56
57
        $this->middlewares[] = $middleware;
58
59 3
        return $this;
60
    }
61 3
62
    public function dispatch(ServerRequestInterface $request): ResponseInterface
63 3
    {
64 3
        return $this->process($request, $this->nextHandler);
65
    }
66 1
67
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
68
    {
69
        if ($this->stack === null) {
70 1
            foreach ($this->middlewares as $middleware) {
71
                $handler = new MiddlewareHandler($middleware, $handler);
72 1
            }
73
            $this->stack = $handler;
74
        }
75
76 1
        return $this->stack->handle($request);
0 ignored issues
show
Bug introduced by
The method handle() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        return $this->stack->/** @scrutinizer ignore-call */ handle($request);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
    }
78
}
79