TrailingSlashEnforce   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 14 7
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() && 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