Failed Conditions
Push — master ( 22f12f...42602c )
by Florent
03:49
created

Pipe::dispatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\IssuerDiscoveryBundle\Middleware;
15
16
use Psr\Http\Server\RequestHandlerInterface;
17
use Psr\Http\Server\MiddlewareInterface;
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
21
class Pipe implements MiddlewareInterface
22
{
23
    /**
24
     * @var MiddlewareInterface[]
25
     */
26
    private $middlewares;
27
28
    /**
29
     * Dispatcher constructor.
30
     *
31
     * @param MiddlewareInterface[] $middlewares
32
     */
33
    public function __construct(array $middlewares = [])
34
    {
35
        $this->middlewares = $middlewares;
36
    }
37
38
    /**
39
     * Appends new middleware for this message bus. Should only be used at configuration time.
40
     *
41
     * @param MiddlewareInterface $middleware
42
     */
43
    public function appendMiddleware(MiddlewareInterface $middleware)
44
    {
45
        $this->middlewares[] = $middleware;
46
    }
47
48
    /**
49
     * Prepends new middleware for this message bus. Should only be used at configuration time.
50
     *
51
     * @param MiddlewareInterface $middleware
52
     */
53
    public function prependMiddleware(MiddlewareInterface $middleware)
54
    {
55
        array_unshift($this->middlewares, $middleware);
56
    }
57
58
    /**
59
     * @param MiddlewareInterface $middleware
60
     */
61
    public function addMiddlewareAfterFirstOne(MiddlewareInterface $middleware)
62
    {
63
        $count = count($this->middlewares);
64
        $temp = array_slice($this->middlewares, 1, $count);
65
        array_unshift($temp, $middleware);
66
        array_unshift($temp, $this->middlewares[0]);
67
        $this->middlewares = $temp;
68
    }
69
70
    /**
71
     * @param MiddlewareInterface $middleware
72
     */
73
    public function addMiddlewareBeforeLastOne(MiddlewareInterface $middleware)
74
    {
75
        $count = count($this->middlewares);
76
        $temp = array_slice($this->middlewares, 0, $count - 1);
77
        $temp[] = $middleware;
78
        $temp[] = $this->middlewares[$count - 1];
79
        $this->middlewares = $temp;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
86
    {
87
        $this->middlewares[] = new RequestHandler(function (ServerRequestInterface $request) use ($handler) {
88
            return $handler->handle($request);
89
        });
90
91
        $response = $this->dispatch($request);
92
93
        array_pop($this->middlewares);
94
95
        return $response;
96
    }
97
98
    /**
99
     * Dispatches the middleware and returns the resulting `ResponseInterface`.
100
     *
101
     * @param ServerRequestInterface $request
102
     *
103
     * @throws \LogicException on unexpected result from any middleware on the middlewares
104
     *
105
     * @return ResponseInterface
106
     */
107
    public function dispatch(ServerRequestInterface $request)
108
    {
109
        $resolved = $this->resolve(0);
110
111
        return $resolved->handle($request);
112
    }
113
114
    /**
115
     * @param int $index Middleware index
116
     *
117
     * @return RequestHandlerInterface
118
     */
119
    private function resolve(int $index): RequestHandlerInterface
120
    {
121
        if (isset($this->middlewares[$index])) {
122
            $middleware = $this->middlewares[$index];
123
124
            return new RequestHandler(function (ServerRequestInterface $request) use ($middleware, $index) {
125
                return $middleware->process($request, $this->resolve($index + 1));
126
            });
127
        }
128
129
        return new RequestHandler(function () {
130
            throw new \LogicException('Unresolved request: middleware exhausted with no result.');
131
        });
132
    }
133
}
134