| Total Complexity | 7 |
| Total Lines | 52 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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 | 15 | public function __construct(MiddlewareFactoryInterface $middlewareFactory, MiddlewareStackInterface $stack) |
|
| 30 | 15 | } |
|
| 31 | |||
| 32 | 12 | public function dispatch(ServerRequestInterface $request, RequestHandlerInterface $fallbackHandler): ResponseInterface |
|
| 33 | { |
||
| 34 | 12 | if ($this->stack->isEmpty()) { |
|
| 35 | 12 | $this->stack = $this->stack->build($this->buildMiddlewares(), $fallbackHandler); |
|
| 36 | } |
||
| 37 | |||
| 38 | 12 | return $this->stack->handle($request); |
|
| 39 | } |
||
| 40 | |||
| 41 | 12 | public function withMiddlewares(array $middlewareDefinitions): MiddlewareDispatcher |
|
| 42 | { |
||
| 43 | 12 | $clone = clone $this; |
|
| 44 | 12 | $clone->middlewareDefinitions = $middlewareDefinitions; |
|
| 45 | 12 | $clone->stack->reset(); |
|
| 46 | |||
| 47 | 12 | return $clone; |
|
| 48 | } |
||
| 49 | |||
| 50 | 6 | public function hasMiddlewares(): bool |
|
| 53 | } |
||
| 54 | |||
| 55 | 12 | private function buildMiddlewares(): array |
|
| 63 | } |
||
| 64 | } |
||
| 65 |