Failed Conditions
Push — master ( 7c3864...930f9b )
by Florent
14:15
created

src/Component/Core/Middleware/Pipe.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Component\Core\Middleware;
15
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Psr\Http\Server\MiddlewareInterface;
19
use Psr\Http\Server\RequestHandlerInterface;
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
    public function appendMiddleware(MiddlewareInterface $middleware)
42
    {
43
        $this->middlewares[] = $middleware;
44
    }
45
46
    /**
47
     * Prepends new middleware for this message bus. Should only be used at configuration time.
48
     */
49
    public function prependMiddleware(MiddlewareInterface $middleware)
50
    {
51
        \array_unshift($this->middlewares, $middleware);
52
    }
53
54
    public function addMiddlewareAfterFirstOne(MiddlewareInterface $middleware)
55
    {
56
        $count = \count($this->middlewares);
57
        $temp = \array_slice($this->middlewares, 1, $count);
58
        \array_unshift($temp, $middleware);
59
        \array_unshift($temp, $this->middlewares[0]);
60
        $this->middlewares = $temp;
61
    }
62
63
    public function addMiddlewareBeforeLastOne(MiddlewareInterface $middleware)
64
    {
65
        $count = \count($this->middlewares);
66
        $temp = \array_slice($this->middlewares, 0, $count - 1);
67
        $temp[] = $middleware;
68
        $temp[] = $this->middlewares[$count - 1];
69
        $this->middlewares = $temp;
70
    }
71
72
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
73
    {
74
        /*$this->middlewares[] = new RequestHandler(function (ServerRequestInterface $request) use ($handler) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75
            return $handler->handle($request);
76
        });*/
77
78
        $response = $this->dispatch($request);
79
80
        //\array_pop($this->middlewares);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
82
        return $response;
83
    }
84
85
    /**
86
     * Dispatches the middleware and returns the resulting `ResponseInterface`.
87
     */
88
    public function dispatch(ServerRequestInterface $request): ResponseInterface
89
    {
90
        $resolved = $this->resolve(0);
91
92
        return $resolved->handle($request);
93
    }
94
95
    private function resolve(int $index): RequestHandlerInterface
96
    {
97
        if (isset($this->middlewares[$index])) {
98
            $middleware = $this->middlewares[$index];
99
100
            return new RequestHandler(function (ServerRequestInterface $request) use ($middleware, $index) {
101
                return $middleware->process($request, $this->resolve($index + 1));
102
            });
103
        }
104
105
        return new RequestHandler(function () {
106
            throw new \LogicException('Unresolved request: middleware exhausted with no result.');
107
        });
108
    }
109
}
110