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

TrailingSlashEnforce::checkQueryString()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 2
nc 5
nop 2
dl 0
loc 4
rs 9.2
c 0
b 0
f 0
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