Passed
Push — master ( c8fc29...ea1130 )
by F
03:55
created

Locale::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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
use PWWEB\Localisation\Repositories\LanguageRepository;
10
11
class Locale
12
{
13
    /**
14
     * Constant for the session key for locale.
15
     *
16
     * @const string
17
     */
18
    const SESSION_KEY = 'locale';
19
20
    /**
21
     * Repository of languages to be used throughout the controller.
22
     *
23
     * @var \PWWEB\Localisation\Repositories\LanguageRepository
24
     */
25
    private $languageRepository;
26
27
    public function __construct(LanguageRepository $languageRepo)
28
    {
29
        $this->languageRepository = $languageRepo;
30
    }
31
32
    /**
33
     * Locale middleware handler.
34
     *
35
     * @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...
36
     * @param Closure                 $next    Next
37
     *
38
     * @return mixed
39
     */
40
    public function handle(Request $request, Closure $next)
41
    {
42
        $session = $request->getSession();
43
44
        if (null !== $session) {
45
            if (true === $session->has(self::SESSION_KEY)) {
46
                $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...
47
            }
48
49
            if (true === $request->has('lang')) {
50
                $lang = $request->get('lang');
51
                $locales = (array) $this->languageRepository->getLocales();
52
53
                if (true === in_array($lang, $locales)) {
54
                    $session->put(self::SESSION_KEY, $lang);
55
                }
56
            }
57
58
            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

58
            app()->/** @scrutinizer ignore-call */ setLocale($session->get(self::SESSION_KEY));
Loading history...
59
60
            return $next($request);
61
        }
62
63
        $lang = $request->server('HTTP_ACCEPT_LANGUAGE');
64
65
        app()->setLocale($lang);
66
67
        return $next($request);
68
    }
69
}
70