TranslatorMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 31
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 20 5
1
<?php
2
3
/**
4
 * Translator Middleware
5
 */
6
namespace Hokan22\LaravelTranslator\Middleware;
7
8
use Hokan22\LaravelTranslator\TranslatorFacade;
9
use Closure;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Session;
12
13
/**
14
 * Class LocaleHandler
15
 *
16
 * @category TranslatorMiddleware
17
 * @package  Hokan22\LaravelTranslator\Middleware
18
 * @author   Alexander Viertel <[email protected]>
19
 * @license  http://opensource.org/licenses/MIT MIT
20
 * @link     https://github.com/Hokan22/laravel-translator
21
 */
22
class TranslatorMiddleware
23
{
24
    /**
25
     * Handle an incoming request
26
     *
27
     * @param Request $request
28
     * @param Closure $next
29
     * @return mixed
30
     *
31
     * @todo Validate Browser locale string (https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4)
32
     */
33
    public function handle(Request $request, Closure $next) {
34
        if (Session::has('locale') || auth()->check()) {
35
            $locale = Session::has('locale') ? session()->get('locale') : auth()->user()->language;
36
37
            $locale = TranslatorFacade::validateLocale($locale);
38
39
            if (Session::has('locale') == false) {
40
                Session::put('locale', $locale);
41
                Session::save();
42
            }
43
44
            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

44
            app()->/** @scrutinizer ignore-call */ setLocale($locale);
Loading history...
45
        } else {
46
            Session::put('locale', TranslatorFacade::getConfigValue('default_locale'));
47
            Session::save();
48
        }
49
50
        TranslatorFacade::setLocale(session()->get('locale'));
51
52
        return $next($request);
53
    }
54
}