LocaleMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 11
dl 0
loc 47
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 37 5
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Carbon\Carbon;
6
use Closure;
7
8
/**
9
 * Class LocaleMiddleware.
10
 */
11
class LocaleMiddleware
12
{
13
    /**
14
     * Handle an incoming request.
15
     *
16
     * @param \Illuminate\Http\Request $request
17
     * @param \Closure                 $next
18
     *
19
     * @return mixed
20
     */
21
    public function handle($request, Closure $next)
22
    {
23
        /*
24
         * Locale is enabled and allowed to be changed
25
         */
26
        if (config('locale.status')) {
27
            if (session()->has('locale') && in_array(session()->get('locale'), array_keys(config('locale.languages')))) {
28
29
                /*
30
                 * Set the Laravel locale
31
                 */
32
                app()->setLocale(session()->get('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

32
                app()->/** @scrutinizer ignore-call */ setLocale(session()->get('locale'));
Loading history...
33
34
                /*
35
                 * setLocale for php. Enables ->formatLocalized() with localized values for dates
36
                 */
37
                setlocale(LC_TIME, config('locale.languages')[session()->get('locale')][1]);
38
39
                /*
40
                 * setLocale to use Carbon source locales. Enables diffForHumans() localized
41
                 */
42
                Carbon::setLocale(config('locale.languages')[session()->get('locale')][0]);
43
44
                /*
45
                 * Set the session variable for whether or not the app is using RTL support
46
                 * for the current language being selected
47
                 * For use in the blade directive in BladeServiceProvider
48
                 */
49
                if (config('locale.languages')[session()->get('locale')][2]) {
50
                    session(['lang-rtl' => true]);
51
                } else {
52
                    session()->forget('lang-rtl');
53
                }
54
            }
55
        }
56
57
        return $next($request);
58
    }
59
}
60