Middleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 13
dl 0
loc 29
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 27 4
1
<?php
2
3
namespace Presspack\Framework\Support\Localization;
4
5
use Closure;
6
use Illuminate\Support\Facades\App;
7
use Presspack\Framework\Support\Facades\Localize as LocalizeFacade;
8
9
class Middleware
10
{
11
    public function handle($request, Closure $next, $segment = 0)
12
    {
13
        // Ignores all non GET requests:
14
        if ('GET' !== $request->method()) {
15
            return $next($request);
16
        }
17
18
        $currentUrl = $request->getUri();
19
        $uriLocale = LocalizeFacade::getLocaleFromUrl($currentUrl, $segment);
0 ignored issues
show
Bug introduced by
The method getLocaleFromUrl() does not exist on Presspack\Framework\Support\Facades\Localize. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
        /** @scrutinizer ignore-call */ 
20
        $uriLocale = LocalizeFacade::getLocaleFromUrl($currentUrl, $segment);
Loading history...
20
        $defaultLocale = config('presspack.default_locale');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        $defaultLocale = /** @scrutinizer ignore-call */ config('presspack.default_locale');
Loading history...
21
        // If a locale was set in the url:
22
        if ($uriLocale) {
23
            // Set app locale
24
            App::setLocale($uriLocale);
25
26
            return $next($request);
27
        }
28
29
        // If no locale was set in the url, check the browser's locale:
30
        $browserLocale = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
31
        if (LocalizeFacade::isValidLocale($browserLocale)) {
0 ignored issues
show
Bug introduced by
The method isValidLocale() does not exist on Presspack\Framework\Support\Facades\Localize. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        if (LocalizeFacade::/** @scrutinizer ignore-call */ isValidLocale($browserLocale)) {
Loading history...
32
            return redirect()->to(LocalizeFacade::url($currentUrl, $browserLocale, $segment));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
            return /** @scrutinizer ignore-call */ redirect()->to(LocalizeFacade::url($currentUrl, $browserLocale, $segment));
Loading history...
Bug introduced by
The method url() does not exist on Presspack\Framework\Support\Facades\Localize. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
            return redirect()->to(LocalizeFacade::/** @scrutinizer ignore-call */ url($currentUrl, $browserLocale, $segment));
Loading history...
33
        }
34
35
        // If not, redirect to the default locale:
36
37
        return redirect()->to(LocalizeFacade::url($currentUrl, $defaultLocale, $segment));
38
    }
39
}
40