LocalizationMiddleware   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 63
c 2
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 17 1
A validateLanguage() 0 10 2
A findLanguage() 0 5 2
A getSupportedLanguages() 0 4 1
1
<?php
2
3
namespace App\Containers\Localization\Middlewares;
4
5
use App;
6
use App\Ship\Parents\Middlewares\Middleware;
7
use Closure;
8
use Config;
9
use Illuminate\Http\Request;
10
11
/**
12
 * Class LocalizationMiddleware
13
 *
14
 * @author  Mahmoud Zalt  <[email protected]>
15
 */
16
class LocalizationMiddleware extends Middleware
17
{
18
19
    /**
20
     * @param \Illuminate\Http\Request $request
21
     * @param \Closure                 $next
22
     *
23
     * @return  mixed
24
     */
25
    public function handle(Request $request, Closure $next)
26
    {
27
        // find and validate the lang on that request
28
        $lang = $this->validateLanguage($this->findLanguage($request));
29
30
        // set the local language
31
        App::setLocale($lang);
32
33
        // get the response after the request is done
34
        $response = $next($request);
35
36
        // set Content Languages header in the response
37
        $response->headers->set('Content-Language', $lang);
38
39
        // return the response
40
        return $response;
41
    }
42
43
    /**
44
     * @param $lang
45
     *
46
     * @return string|Exception
47
     */
48
    private function validateLanguage($lang)
49
    {
50
        // check the languages defined is supported
51
        if (!array_key_exists($lang, $this->getSupportedLanguages())) {
52
            // respond with error
53
            $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...
54
        }
55
56
        return $lang;
57
    }
58
59
    /**
60
     * @param $request
61
     *
62
     * @return  string
63
     */
64
    private function findLanguage($request)
65
    {
66
        // read the language from the request header, if the header is missed, take the default local language
67
        return $request->header('Content-Language') ? : Config::get('app.locale');
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    private function getSupportedLanguages()
74
    {
75
        return Config::get('localization.supported_languages');
76
    }
77
78
}
79