Completed
Pull Request — master (#107)
by Tim
24:03 queued 15:37
created

languageMiddleware::isLanguageSupported()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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