SetLocale   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 20 3
1
<?php
2
3
namespace BBSLab\NovaTranslation\Http\Middleware;
4
5
use BBSLab\NovaTranslation\Models\Locale;
6
use BBSLab\NovaTranslation\NovaTranslation;
7
use Illuminate\Support\Facades\Session;
8
9
class SetLocale
10
{
11
    /**
12
     * Handle the incoming request.
13
     *
14
     * @param  \Illuminate\Http\Request  $request
15
     * @param  \Closure  $next
16
     * @return \Illuminate\Http\Response
17
     */
18
    public function handle($request, $next)
19
    {
20
        if (Session::has(NovaTranslation::localeSessionKey())) {
21
            app()->setLocale(
22
                Session::get(NovaTranslation::localeSessionKey())
23
            );
24
        } else {
25
            $browserLocale = Locale::havingIso(
26
                // Take first 2 (as described flags in config)
27
                substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2)
28
            );
29
30
            $locale = $browserLocale ? $browserLocale->iso : config('app.locale');
31
            Session::put(NovaTranslation::localeSessionKey(), $locale);
32
33
            app()->setLocale($locale);
34
        }
35
36
        return $next($request);
37
    }
38
}
39