Passed
Push — main ( 6f03e1...597168 )
by Thierry
13:43
created

GetAppLocale   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 23
rs 10
c 1
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 13 1
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Carbon\Carbon;
6
use Illuminate\Http\RedirectResponse;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Response;
9
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
10
use Closure;
11
12
use function session;
13
use function setlocale;
14
15
class GetAppLocale
16
{
17
    /**
18
     * Handle an incoming request.
19
     *
20
     * @param  Request  $request
21
     * @param  Closure(Request): (Response|RedirectResponse)  $next
22
     *
23
     * @return Response|RedirectResponse
24
     */
25
    public function handle(Request $request, Closure $next)
26
    {
27
        // Try to get the current locale from the session.
28
        $locale = session('jaxonCurrentLocale', LaravelLocalization::getCurrentLocale());
29
        LaravelLocalization::setLocale($locale);
0 ignored issues
show
Bug introduced by
It seems like $locale can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $locale of Mcamara\LaravelLocalizat...calization::setLocale() 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
        LaravelLocalization::setLocale(/** @scrutinizer ignore-type */ $locale);
Loading history...
30
31
        // Same as for SetAppLocale, excepted that the locale is read in the session.
32
        Carbon::setLocale($locale);
0 ignored issues
show
Bug introduced by
It seems like $locale can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $locale of Carbon\Carbon::setLocale() 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

32
        Carbon::setLocale(/** @scrutinizer ignore-type */ $locale);
Loading history...
33
        setlocale(LC_TIME, $locale);
0 ignored issues
show
Bug introduced by
It seems like $locale can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $locales of setlocale() does only seem to accept array|integer|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

33
        setlocale(LC_TIME, /** @scrutinizer ignore-type */ $locale);
Loading history...
34
        // For comparison of strings with accented characters in french
35
        setlocale(LC_COLLATE, LaravelLocalization::getCurrentLocaleRegional() . '.utf8');
36
37
        return $next($request);
38
    }
39
}
40