Completed
Push — master ( 1b2d0a...89ea1a )
by Oscar
03:00
created

TrailingSlash::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Middleware to add or remove the trailing slash.
11
 */
12
class TrailingSlash
13
{
14
    use Utils\RedirectTrait;
15
16
    /**
17
     * @var bool Add or remove the slash
18
     */
19
    private $addSlash;
20
21
    /**
22
     * Configure whether add or remove the slash.
23
     *
24
     * @param bool $addSlash
25
     */
26
    public function __construct($addSlash = false)
27
    {
28
        $this->addSlash = (boolean) $addSlash;
29
    }
30
31
    /**
32
     * Execute the middleware.
33
     *
34
     * @param RequestInterface  $request
35
     * @param ResponseInterface $response
36
     * @param callable          $next
37
     *
38
     * @return ResponseInterface
39
     */
40
    public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
41
    {
42
        $uri = $request->getUri();
43
        $path = $uri->getPath();
44
45
        //Add/remove slash
46
        if ($this->addSlash) {
47
            if (strlen($path) > 1 && substr($path, -1) !== '/' && !pathinfo($path, PATHINFO_EXTENSION)) {
48
                $path .= '/';
49
            }
50 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
            if (strlen($path) > 1 && substr($path, -1) === '/') {
52
                $path = substr($path, 0, -1);
53
            }
54
        }
55
56
        //Ensure the path has one "/"
57
        if ($path === '') {
58
            $path = '/';
59
        }
60
61
        //redirect
62
        if ($this->redirectStatus !== false && ($uri->getPath() !== $path)) {
63
            return $this->getRedirectResponse($request, $uri->withPath($path), $response);
0 ignored issues
show
Compatibility introduced by
$request of type object<Psr\Http\Message\RequestInterface> is not a sub-type of object<Psr\Http\Message\ServerRequestInterface>. It seems like you assume a child interface of the interface Psr\Http\Message\RequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
64
        }
65
66
        return $next($request->withUri($uri->withPath($path)), $response);
67
    }
68
}
69