Passed
Push — master ( 26152a...8fe73e )
by F
04:04
created

Locale::handle()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 7
nop 2
dl 0
loc 28
rs 9.4888
c 0
b 0
f 0
1
<?php
2
3
namespace PWWEB\Localisation\Middleware;
4
5
use Closure;
6
use Illuminate\Contracts\Session\Session;
7
use Illuminate\Http\Request;
8
9
class Locale
10
{
11
    /**
12
     * Constant for the session key for locale.
13
     *
14
     * @const string
15
     */
16
    const SESSION_KEY = 'locale';
17
18
    /**
19
     * Locale middleware handler.
20
     *
21
     * @param Illuminate\Http\Request $request current request
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\Middl...Illuminate\Http\Request was not found. Did you mean Illuminate\Http\Request? If so, make sure to prefix the type with \.
Loading history...
22
     * @param Closure                 $next    Next
23
     *
24
     * @return mixed
25
     */
26
    public function handle(Request $request, Closure $next)
27
    {
28
        $session = $request->getSession();
29
30
        if (null !== $session) {
31
            if (true === $session->has(self::SESSION_KEY)) {
32
                $session->put(self::SESSION_KEY, $request->getPreferredLanguage(self::LOCALES));
0 ignored issues
show
Bug introduced by
The constant PWWEB\Localisation\Middleware\Locale::LOCALES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
33
            }
34
35
            if (true === $request->has('lang')) {
36
                $lang = $request->get('lang');
37
                $locales = (array) Language::getLocales();
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\Middleware\Language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
39
                if (true === in_array($lang, $locales)) {
40
                    $session->put(self::SESSION_KEY, $lang);
41
                }
42
            }
43
44
            app()->setLocale($session->get(self::SESSION_KEY));
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($session->get(self::SESSION_KEY));
Loading history...
45
46
            return $next($request);
47
        }
48
49
        $lang = $request->server('HTTP_ACCEPT_LANGUAGE');
50
51
        app()->setLocale($lang);
52
53
        return $next($request);
54
    }
55
}
56