GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Push — master ( c5ade0...e039e4 )
by Gabriel
05:51
created

Dispatcher::get()   C

Complexity

Conditions 14
Paths 19

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 29
nc 19
nop 1
dl 0
loc 44
rs 5.0864
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Nip\Http\ServerMiddleware;
4
5
use Closure;
6
use Interop\Http\ServerMiddleware\DelegateInterface;
7
use Interop\Http\ServerMiddleware\MiddlewareInterface;
8
use InvalidArgumentException;
9
use LogicException;
10
use Nip\Container\ContainerInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
14
/**
15
 * Class Dispatcher
16
 * @package Nip\Http\ServerMiddleware
17
 *
18
 * @based on https://github.com/oscarotero/middleland/blob/master/src/Dispatcher.php
19
 */
20
class Dispatcher implements DispatcherInterface
21
{
22
23
    /**
24
     * @var MiddlewareInterface[]
25
     */
26
    private $middleware;
27
    /**
28
     * @var ContainerInterface|null
29
     */
30
    private $container;
31
32
33
    /**
34
     * @param MiddlewareInterface[] $middleware
35
     * @param ContainerInterface $container
36
     */
37
    public function __construct(array $middleware, ContainerInterface $container = null)
38
    {
39
        if (empty($middleware)) {
40
            throw new LogicException('Empty middleware queue');
41
        }
42
        $this->middleware = $middleware;
43
        $this->container = $container;
44
    }
45
46
    /**
47
     * Return a new dispatcher containing the given middleware.
48
     *
49
     * @param \Interop\Http\ServerMiddleware\MiddlewareInterface $middleware
50
     * @return DispatcherInterface
51
     */
52
    public function with(MiddlewareInterface $middleware): DispatcherInterface
53
    {
54
        return $this->withElement($middleware);
55
    }
56
57
    /**
58
     * Return a new dispatcher containing the given element.
59
     *
60
     * @param mixed $element
61
     * @return DispatcherInterface
62
     */
63
    public function withElement($element): DispatcherInterface
64
    {
65
        $this->middleware = array_merge($this->middleware, [$element]);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->middleware, array($element)) of type array<integer,*,{"0":"*"}> is incompatible with the declared type array<integer,object<Int...e\MiddlewareInterface>> of property $middleware.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
        return $this;
67
    }
68
69
    /**
70
     * Dispatch the request, return a response.
71
     *
72
     * @param ServerRequestInterface $request
73
     *
74
     * @return ResponseInterface
75
     */
76
    public function dispatch(ServerRequestInterface $request): ResponseInterface
77
    {
78
        reset($this->middleware);
79
        return $this->get($request)->process($request, $this->createDelegate());
80
    }
81
82
    /**
83
     * Create a delegate for the current stack
84
     *
85
     * @param DelegateInterface $delegate
86
     *
87
     * @return DelegateInterface
88
     */
89
    private function createDelegate(DelegateInterface $delegate = null): DelegateInterface
90
    {
91
        return new class($this, $delegate) implements DelegateInterface
92
        {
93
            private $dispatcher;
94
            private $delegate;
95
96
            /**
97
             * @param Dispatcher $dispatcher
98
             * @param DelegateInterface|null $delegate
99
             */
100
            public function __construct(Dispatcher $dispatcher, DelegateInterface $delegate = null)
101
            {
102
                $this->dispatcher = $dispatcher;
103
                $this->delegate = $delegate;
104
            }
105
106
            /**
107
             * {@inheritdoc}
108
             */
109
            public function process(ServerRequestInterface $request)
110
            {
111
                $frame = $this->dispatcher->next($request);
112
                if ($frame === false) {
113
                    if ($this->delegate !== null) {
114
                        return $this->delegate->process($request);
115
                    }
116
                    throw new LogicException('Middleware queue exhausted');
117
                }
118
                return $frame->process($request, $this);
119
            }
120
        };
121
    }
122
123
    /**
124
     * Return the next available middleware frame in the queue.
125
     *
126
     * @param ServerRequestInterface $request
127
     * @return false|MiddlewareInterface
128
     */
129
    public function next(ServerRequestInterface $request)
130
    {
131
        next($this->middleware);
132
        return $this->get($request);
133
    }
134
135
    /**
136
     * Return the next available middleware frame in the middleware.
137
     *
138
     * @param ServerRequestInterface $request
139
     *
140
     * @return MiddlewareInterface|false
141
     */
142
    private function get(ServerRequestInterface $request)
143
    {
144
        $frame = current($this->middleware);
145
        if ($frame === false) {
146
            return $frame;
147
        }
148
        if (is_array($frame)) {
149
            $conditions = $frame;
150
            $frame = array_pop($conditions);
151
            foreach ($conditions as $condition) {
152
                if ($condition === true) {
153
                    continue;
154
                }
155
                if ($condition === false) {
156
                    return $this->next($request);
157
                }
158
                if (is_string($condition)) {
159
                    $condition = new Matchers\Path($condition);
160
                } elseif (!($condition instanceof Matchers\MatcherInterface)) {
161
                    throw new InvalidArgumentException(
162
                        'Invalid matcher. Must be a boolean, string or an instance of MatcherInterface'
163
                    );
164
                }
165
                if (!$condition->match($request)) {
166
                    return $this->next($request);
167
                }
168
            }
169
        }
170
        if (is_string($frame)) {
171
            if ($this->container === null) {
172
                throw new InvalidArgumentException(sprintf('No valid middleware provided (%s)', $frame));
173
            }
174
            $frame = $this->container->get($frame);
175
        }
176
        if ($frame instanceof Closure) {
177
            return $this->createMiddlewareFromClosure($frame);
178
        }
179
        if ($frame instanceof MiddlewareInterface) {
180
            return $frame;
181
        }
182
        throw new InvalidArgumentException(
183
            sprintf('No valid middleware provided (%s)', is_object($frame) ? get_class($frame) : gettype($frame))
184
        );
185
    }
186
187
    /**
188
     * Create a middleware from a closure
189
     *
190
     * @param Closure $handler
191
     *
192
     * @return MiddlewareInterface
193
     */
194
    private function createMiddlewareFromClosure(Closure $handler): MiddlewareInterface
195
    {
196
        return new class($handler) implements MiddlewareInterface
197
        {
198
            private $handler;
199
200
            /**
201
             * @param Closure $handler
202
             */
203
            public function __construct(Closure $handler)
204
            {
205
                $this->handler = $handler;
206
            }
207
208
            /**
209
             * {@inheritdoc}
210
             */
211
            public function process(ServerRequestInterface $request, DelegateInterface $delegate)
212
            {
213
                $response = call_user_func($this->handler, $request, $delegate);
214
                if (!($response instanceof ResponseInterface)) {
215
                    throw new LogicException('The middleware must return a ResponseInterface');
216
                }
217
                return $response;
218
            }
219
        };
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface
226
    {
227
        reset($this->middleware);
228
        return $this->get($request)->process($request, $this->createDelegate($delegate));
229
    }
230
}
231