Completed
Push — master ( 09c4bf...21aaac )
by
unknown
08:14
created

SetLocale::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace BBSLab\NovaTranslation\Http\Middleware;
4
5
use BBSLab\NovaTranslation\Models\Locale;
6
use Illuminate\Support\Facades\Session;
7
8
class SetLocale
9
{
10
    /**
11
     * Handle the incoming request.
12
     *
13
     * @param  \Illuminate\Http\Request  $request
14
     * @param  \Closure  $next
15
     * @return \Illuminate\Http\Response
16
     */
17
    public function handle($request, $next)
18
    {
19
        if (Session::has('nova_locale')) {
20
            $locale = Session::get('nova_locale');
21
        } else {
22
            $browserLocale = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
23
            $databaseLocale = Locale::query()->select('id')->where('iso', '=', $browserLocale)->first();
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
24
            if (! empty($databaseLocale)) {
25
                $locale = $browserLocale;
26
            } else {
27
                $locale = config('app.locale');
28
            }
29
            Session::put('nova_locale', $locale);
30
        }
31
32
        app()->setLocale($locale);
33
34
        return $next($request);
35
    }
36
}
37