Passed
Pull Request — master (#2)
by Dmitriy
10:29
created

MiddlewareDispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Middleware\Dispatcher;
6
7
use Psr\Http\Message\ResponseInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Psr\Http\Message\ServerRequestInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ServerRequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Psr\Http\Server\RequestHandlerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\RequestHandlerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
final class MiddlewareDispatcher
12
{
13
    /**
14
     * Contains a stack of middleware handler.
15
     * @var MiddlewareStackInterface stack of middleware
16
     */
17
    private MiddlewareStackInterface $stack;
18
19
    private MiddlewareFactoryInterface $middlewareFactory;
20
21
    /**
22
     * @var callable[]|string[]|array[]
23
     */
24
    private array $middlewareDefinitions = [];
25
26
    public function __construct(MiddlewareFactoryInterface $middlewareFactory, MiddlewareStackInterface $stack)
27
    {
28
        $this->middlewareFactory = $middlewareFactory;
29
        $this->stack = $stack;
30
    }
31
32
    public function dispatch(ServerRequestInterface $request, RequestHandlerInterface $fallbackHandler): ResponseInterface
33
    {
34
        if ($this->stack->isEmpty()) {
35
            $this->stack = $this->stack->build($this->buildMiddlewares(), $fallbackHandler);
36
        }
37
38
        return $this->stack->handle($request);
39
    }
40
41
    public function withMiddlewares(array $middlewareDefinitions): MiddlewareDispatcher
42
    {
43
        $clone = clone $this;
44
        $clone->middlewareDefinitions = $middlewareDefinitions;
45
        $clone->stack->reset();
46
47
        return $clone;
48
    }
49
50
    public function hasMiddlewares(): bool
51
    {
52
        return $this->middlewareDefinitions !== [];
53
    }
54
55
    private function buildMiddlewares(): array
56
    {
57
        $middlewares = [];
58
        foreach ($this->middlewareDefinitions as $middlewareDefinition) {
59
            $middlewares[] = $this->middlewareFactory->create($middlewareDefinition);
60
        }
61
62
        return $middlewares;
63
    }
64
}
65