Middleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseHttpLocale() 0 25 2
A handle() 0 7 2
1
<?php
2
3
namespace Orkhanahmadov\LaravelAcceptLanguageMiddleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Collection;
8
9
class Middleware
10
{
11
    /**
12
     * Handle an incoming request.
13
     *
14
     * @param  Request  $request
15
     * @param  \Closure  $next
16
     * @return mixed
17
     */
18
    public function handle($request, Closure $next)
19
    {
20
        if ($locale = $this->parseHttpLocale($request)) {
21
            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

21
            app()->/** @scrutinizer ignore-call */ setLocale($locale);
Loading history...
22
        }
23
24
        return $next($request);
25
    }
26
27
    private function parseHttpLocale(Request $request): string
28
    {
29
        $list = explode(',', $request->server('HTTP_ACCEPT_LANGUAGE', ''));
0 ignored issues
show
Bug introduced by
It seems like $request->server('HTTP_ACCEPT_LANGUAGE', '') can also be of type array and null; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

29
        $list = explode(',', /** @scrutinizer ignore-type */ $request->server('HTTP_ACCEPT_LANGUAGE', ''));
Loading history...
30
31
        $locales = Collection::make($list)
0 ignored issues
show
Bug introduced by
$list of type string[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::make(). ( Ignorable by Annotation )

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

31
        $locales = Collection::make(/** @scrutinizer ignore-type */ $list)
Loading history...
32
            ->map(function ($locale) {
33
                $parts = explode(';', $locale);
34
35
                $mapping['locale'] = trim($parts[0]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$mapping was never initialized. Although not strictly required by PHP, it is generally a good practice to add $mapping = array(); before regardless.
Loading history...
36
37
                if (isset($parts[1])) {
38
                    $factorParts = explode('=', $parts[1]);
39
40
                    $mapping['factor'] = $factorParts[1];
41
                } else {
42
                    $mapping['factor'] = 1;
43
                }
44
45
                return $mapping;
46
            })
47
            ->sortByDesc(function ($locale) {
48
                return $locale['factor'];
49
            });
50
51
        return $locales->first()['locale'];
52
    }
53
}
54