Completed
Push — master ( cd4a8d...bb1000 )
by Tim
02:29
created

languageMiddleware::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 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
/**
12
 * Class languageMiddleware
13
 * @package app\Http\Middleware
14
 */
15
class languageMiddleware
16
{
17
    /**
18
     * @var array
19
     */
20
    protected static $supportedLanguages = ['nl', 'en', 'fr',
21
    ];
22
    /**
23
     * Handle an incoming request.
24
     *
25
     * @param \Illuminate\Http\Request $request
26
     * @param \Closure                 $next
27
     *
28
     * @return mixed
29
     */
30
    public function handle(Request $request, Closure $next)
31
    {
32
        $language = (Input::get('lang')) ?: Session::get('lang');
33
        $this->setSupportedLanguage($language);
34
35
        return $next($request);
36
    }
37
    /**
38
     * @param string $lang
39
     *
40
     * @return bool
41
     */
42
    private function isLanguageSupported($lang)
43
    {
44
        return in_array($lang, self::$supportedLanguages);
45
    }
46
    /**
47
     * @param string $lang
48
     */
49
    private function setSupportedLanguage($lang)
50
    {
51
        if ($this->isLanguageSupported($lang)) {
52
            App::setLocale($lang);
53
            Session::put('lang', $lang);
54
        }
55
    }
56
}
57