Completed
Push — master ( ea048a...f42f25 )
by Mahmoud
03:42
created

Localization::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Containers\Localization\Middlewares;
4
5
use Closure;
6
use App;
7
use Config;
8
9
/**
10
 * Class Localization
11
 *
12
 * @author  Mahmoud Zalt  <[email protected]>
13
 */
14
class Localization
15
{
16
    /**
17
     * Handle an incoming request.
18
     *
19
     * @param  \Illuminate\Http\Request $request
20
     * @param  \Closure                 $next
21
     *
22
     * @return mixed
23
     */
24
    public function handle($request, Closure $next)
25
    {
26
        // find and validate the lang on that request
27
        $lang = $this->validateLanguage($this->findLanguage($request));
28
29
        // set the local language
30
        App::setLocale($lang);
31
32
        // get the response after the request is done
33
        $response = $next($request);
34
35
        // set Content Languages header in the response
36
        $response->headers->set('Content-Language', $lang);
37
38
        // return the response
39
        return $response;
40
    }
41
42
    /**
43
     * @param $lang
44
     *
45
     * @return string|Exception
46
     */
47
    private function validateLanguage($lang)
48
    {
49
        // check the languages defined is supported
50
        if (!array_key_exists($lang, $this->getSupportedLanguages())) {
51
            // respond with error
52
            $lang = abort(403, 'Language not supported.');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $lang is correct as abort(403, 'Language not supported.') (which targets abort()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
53
        }
54
55
        return $lang;
56
    }
57
58
    /**
59
     * @param $request
60
     *
61
     * @return  string
62
     */
63
    private function findLanguage($request)
64
    {
65
        // read the language from the request header, if the header is missed, take the default local language
66
        return $request->header('Content-Language') ? : Config::get('app.locale');
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    private function getSupportedLanguages()
73
    {
74
        return Config::get('localization.supported_languages');
75
    }
76
77
}
78