Passed
Push — master ( 173e5c...d2d036 )
by Divine Niiquaye
02:24
created

UriRedirectMiddleware::setPermanentRedirection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Flight Routing.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Flight\Routing\Middlewares;
19
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Server\MiddlewareInterface;
23
use Psr\Http\Server\RequestHandlerInterface;
24
25
class UriRedirectMiddleware implements MiddlewareInterface
26
{
27
    /** @var array */
28
    protected $redirects = [];
29
30
    /** @var bool */
31
    private $permanent;
32
33
    /** @var bool */
34
    private $query;
35
36
    /**
37
     * @param array<string,string> $redirects [from => to]
38
     * @param array<string,bool> $options
39
     */
40
    public function __construct(array $redirects = [], bool $query = true, bool $permanent = true)
41
    {
42
        if (!empty($redirects)) {
43
            $this->redirects = $redirects;
44
        }
45
46
        $this->query = $query;
47
        $this->permanent = $permanent;
48
    }
49
50
    /**
51
     * Whether return a permanent redirect.
52
     *
53
     * @param bool $permanent
54
     *
55
     * @return UriRedirectMiddleware
56
     */
57
    public function setPermanentRedirection(bool $permanent = true): self
58
    {
59
        $this->permanent = $permanent;
60
61
        return $this;
62
    }
63
64
    /**
65
     * Whether include the query to search the url.
66
     *
67
     * @param bool $query
68
     *
69
     * @return UriRedirectMiddleware
70
     */
71
    public function allowQueries(bool $query = true): self
72
    {
73
        $this->query = $query;
74
75
        return $this;
76
    }
77
78
    /**
79
     * Process a request and return a response.
80
     * {@inheritdoc}
81
     */
82
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
83
    {
84
        /** @var ResponseInterface $response */
85
        $response = $handler->handle($request);
86
87
        $uri = $request->getUri()->getPath();
88
89
        if ($this->query && ($query = $request->getUri()->getQuery()) !== '') {
90
            $uri .= '?' . $query;
91
        }
92
93
        if (!isset($this->redirects[$uri])) {
94
            return $response;
95
        }
96
97
        return $response
98
            ->withStatus($this->determineResponseCode($request))
99
            ->withAddedHeader('Location', $this->redirects[$uri]);
100
    }
101
102
    /**
103
     * Determine the response code according with the method and the permanent config.
104
     *
105
     * @param ServerRequestInterface $request
106
     *
107
     * @return int
108
     */
109
    private function determineResponseCode(ServerRequestInterface $request): int
110
    {
111
        if (\in_array($request->getMethod(), ['GET', 'HEAD', 'CONNECT', 'TRACE', 'OPTIONS'])) {
112
            return $this->permanent ? 301 : 302;
113
        }
114
115
        return $this->permanent ? 308 : 307;
116
    }
117
}
118