Completed
Push — master ( dc322e...438d0e )
by Mahmoud
03:27
created

Localization::handle()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 2
1
<?php
2
3
namespace App\Ship\Features\Middlewares\Http;
4
5
use Closure;
6
use Illuminate\Foundation\Application;
7
8
/**
9
 * Class Localization
10
 *
11
 * @author  Mahmoud Zalt  <[email protected]>
12
 */
13
class Localization
14
{
15
16
    /**
17
     * Localization constructor.
18
     *
19
     * @param \Illuminate\Foundation\Application $app
20
     */
21
    public function __construct(Application $app)
22
    {
23
        $this->app = $app;
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
    }
25
26
27
    /**
28
     * Handle an incoming request.
29
     *
30
     * @param  \Illuminate\Http\Request $request
31
     * @param  \Closure                 $next
32
     *
33
     * @return mixed
34
     */
35
    public function handle($request, Closure $next)
36
    {
37
        // read the language from the request header
38
        $locale = $request->header('Content-Language');
39
40
        // if the header is missed
41
        if(!$locale){
42
            // take the default local language
43
            $locale = $this->app->config->get('app.locale');
44
        }
45
46
        // check the languages defined is supported
47
        if (!array_key_exists($locale, $this->app->config->get('hello.supported_languages'))) {
48
            // respond with error
49
            return abort(403, 'Language not supported.');
50
        }
51
52
        // set the local language
53
        $this->app->setLocale($locale);
54
55
        // get the response after the request is done
56
        $response = $next($request);
57
58
        // set Content Languages header in the response
59
        $response->headers->set('Content-Language', $locale);
60
61
        // return the response
62
        return $response;
63
    }
64
}
65