TrailingSlashEnforce::checkQueryString()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 4
nc 5
nop 2
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() && config('cortex.foundation.route.trailing_slash')) {
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)) {
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): bool
41
    {
42
        return (! $queryString && ! ends_with($requestUri, '/')) || ($queryString && ! ends_with(mb_strstr($requestUri, '?', true), '/'));
0 ignored issues
show
Deprecated Code introduced by
The function ends_with() has been deprecated with message: Str::endsWith() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
43
    }
44
}
45