Completed
Push — develop ( 63954c...f2c9f6 )
by Abdelrahman
02:09
created

TrailingSlashEnforce   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 14 6
A checkQueryString() 0 4 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\Http\Middleware;
6
7
use Closure;
8
9
class TrailingSlashEnforce
10
{
11
    /**
12
     * Handle an incoming request.
13
     *
14
     * @param \Illuminate\Http\Request $request
15
     * @param \Closure                 $next
16
     *
17
     * @return mixed
18
     */
19
    public function handle($request, Closure $next)
20
    {
21
        if (! $request->ajax()) {
22
            $requestUri = $request->getRequestUri();
23
            $queryString = $request->getQueryString();
24
            $untrimmedPath = trim($request->getPathInfo(), '/').'/';
25
26
            if ($request->method() === 'GET' && mb_strrchr($requestUri, '.') === false && $this->checkQueryString($requestUri, $queryString)) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 143 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
27
                return redirect()->to($untrimmedPath.(! empty($queryString) ? '?'.$queryString : ''), 301);
28
            }
29
        }
30
31
        return $next($request);
32
    }
33
34
    /**
35
     * @param $requestUri
36
     * @param $queryString
37
     *
38
     * @return bool
39
     */
40
    protected function checkQueryString($requestUri, $queryString)
41
    {
42
        return (! $queryString && ! ends_with($requestUri, '/')) || ($queryString && ! ends_with(mb_strstr($requestUri, '?', true), '/'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
43
    }
44
}
45