Completed
Pull Request — master (#107)
by Glenn
33:11 queued 25:13
created

languageMiddleware   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 42
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 7 2
A isLanguageSupported() 0 4 1
A setSupportedLanguage() 0 7 2
1
<?php
2
3
namespace app\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\App;
8
use Illuminate\Support\Facades\Session;
9
use Illuminate\Support\Facades\Input;
10
11
class languageMiddleware
12
{
13
    /**
14
     * @var array
15
     */
16
    protected static $supportedLanguages = ['nl', 'en', 'fr',
17
    ];
18
    /**
19
     * Handle an incoming request.
20
     *
21
     * @param \Illuminate\Http\Request $request
22
     * @param \Closure                 $next
23
     *
24
     * @return mixed
25
     */
26
    public function handle(Request $request, Closure $next)
27
    {
28
        $language = (Input::get('lang')) ?: Session::get('lang');
29
        $this->setSupportedLanguage($language);
30
31
        return $next($request);
32
    }
33
    /**
34
     * @param string $lang
35
     *
36
     * @return bool
37
     */
38
    private function isLanguageSupported($lang)
39
    {
40
        return in_array($lang, self::$supportedLanguages);
41
    }
42
    /**
43
     * @param string $lang
44
     */
45
    private function setSupportedLanguage($lang)
46
    {
47
        if ($this->isLanguageSupported($lang)) {
48
            App::setLocale($lang);
49
            Session::put('lang', $lang);
50
        }
51
    }
52
}
53