AcceptLanguage::handle()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 3
1
<?php
2
3
namespace Locale\Http\Middleware;
4
5
use Closure;
6
use Locale\Models\Locale;
7
8
class AcceptLanguage
9
{
10
    /**
11
     * Set the locale to use in the API.
12
     *
13
     * @param  \Illuminate\Http\Request  $request
14
     * @param  \Closure  $next
15
     * @param  string|null  $guard
16
     * @return mixed
17
     */
18
    public function handle($request, Closure $next, $guard = null)
0 ignored issues
show
Unused Code introduced by
The parameter $guard is not used and could be removed. ( Ignorable by Annotation )

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

18
    public function handle($request, Closure $next, /** @scrutinizer ignore-unused */ $guard = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
    {
20
        $locale = $this->parseLocale($request->header("Accept-Language"));
21
22
        if ($locale !== null && !app()->isLocale($locale)) {
0 ignored issues
show
introduced by
The method isLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

22
        if ($locale !== null && !app()->/** @scrutinizer ignore-call */ isLocale($locale)) {
Loading history...
23
            app()->setLocale($locale);
0 ignored issues
show
introduced by
The method setLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

23
            app()->/** @scrutinizer ignore-call */ setLocale($locale);
Loading history...
24
        }
25
26
        return $next($request);
27
    }
28
29
    /**
30
     * @since 1.0.0
31
     * @param string $locale
32
     * @return string
33
     */
34
    protected function parseLocale($locale)
35
    {
36
        $locale = explode(",", preg_replace("/\;q=\d.\d/", "", $locale));
37
38
        return collect($locale)->first(function ($locale) {
39
            return $this->isLocaleSupported($locale);
40
        });
41
    }
42
43
    /**
44
     * @since 1.0.0
45
     * @param string $locale
46
     * @return bool
47
     */
48
    protected function isLocaleSupported($locale)
49
    {
50
        return Locale::whereKey($locale)->count() > 0;
51
    }
52
}
53