Locale   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 27 5
1
<?php
2
3
namespace PWWEB\Localisation\Middleware;
4
5
use Closure;
6
use Illuminate\Contracts\Session\Session;
7
use Illuminate\Http\Request;
8
use PWWEB\Localisation\Repositories\LanguageRepository;
9
10
class Locale
11
{
12
    /**
13
     * Constant for the session key for locale.
14
     *
15
     * @const string
16
     */
17
    const SESSION_KEY = 'locale';
18
19
    /**
20
     * Repository of languages to be used throughout the controller.
21
     *
22
     * @var \PWWEB\Localisation\Repositories\LanguageRepository
23
     */
24
    private $languageRepository;
25
26
    /**
27
     * Constructor for the middleware ensuring dependencies are injected.
28
     *
29
     * @param \PWWEB\Localisation\Repositories\LanguageRepository $languageRepo Repository of Languages
30
     */
31
    public function __construct(LanguageRepository $languageRepo)
32
    {
33
        $this->languageRepository = $languageRepo;
34
    }
35
36
    /**
37
     * Locale middleware handler.
38
     *
39
     * @param \Illuminate\Http\Request $request current request
40
     * @param Closure                 $next    Next
41
     *
42
     * @return mixed
43
     */
44
    public function handle(Request $request, Closure $next)
45
    {
46
        $session = $request->getSession();
47
        if (null !== $session) {
48
            if (false === $session->has(self::SESSION_KEY)) {
49
                $session->put(self::SESSION_KEY, $request->getPreferredLanguage($this->languageRepository->getAllActive()->pluck('locale')->toArray()));
50
            }
51
52
            if (true === $request->has('lang')) {
53
                $lang = $request->get('lang');
54
                $check = $this->languageRepository->isLangActive($lang);
0 ignored issues
show
Bug introduced by
It seems like $lang can also be of type null; however, parameter $lang of PWWEB\Localisation\Repos...ository::isLangActive() 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

54
                $check = $this->languageRepository->isLangActive(/** @scrutinizer ignore-type */ $lang);
Loading history...
55
56
                if (false === is_null($check)) {
57
                    $session->put(self::SESSION_KEY, $check->locale);
58
                }
59
            }
60
61
            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

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