LocalizationMiddleware   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 123
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 17 1
A validateLanguage() 0 41 4
A findLanguage() 0 14 2
A getSupportedLanguages() 0 23 5
1
<?php
2
3
namespace App\Containers\Localization\Middlewares;
4
5
use App\Containers\Localization\Exceptions\UnsupportedLanguageException;
6
use App\Ship\Parents\Middlewares\Middleware;
7
use ArrayIterator;
8
use Closure;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\App;
11
use Illuminate\Support\Facades\Config;
12
use Illuminate\Support\Str;
13
14
/**
15
 * Class LocalizationMiddleware
16
 *
17
 * @author  Mahmoud Zalt  <[email protected]>
18
 */
19
class LocalizationMiddleware extends Middleware
20
{
21
22
    /**
23
     * @param \Illuminate\Http\Request $request
24
     * @param \Closure                 $next
25
     *
26
     * @return  mixed
27
     */
28
    public function handle(Request $request, Closure $next)
29
    {
30
        // find and validate the lang on that request
31
        $lang = $this->validateLanguage($this->findLanguage($request));
32
33
        // set the local language
34
        App::setLocale($lang);
35
36
        // get the response after the request is done
37
        $response = $next($request);
38
39
        // set Content Languages header in the response
40
        $response->headers->set('Content-Language', $lang);
41
42
        // return the response
43
        return $response;
44
    }
45
46
    /**
47
     * @param $request_languages
48
     *
49
     * @return string
50
     * @throws UnsupportedLanguageException
51
     */
52
    private function validateLanguage($request_languages)
53
    {
54
        /*
55
         * be sure to check $lang of the format "de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4"
56
         * this means:
57
         *  1) give me de-DE if it is available
58
         *  2) otherwise, give me de
59
         *  3) otherwise, give me en-US
60
         *  4) if all fails, give me en
61
        */
62
63
        // split it up by ","
64
        $languages = explode(',', $request_languages);
65
66
        // we need an ArrayIterator because we will be extending the FOREACH below dynamically!
67
        $language_iterator = new ArrayIterator($languages);
68
69
        $supported_languages = $this->getSupportedLanguages();
70
71
        foreach ($language_iterator as $language) {
72
            // split it up by ";"
73
            $locale = explode(';', $language);
74
75
            $current_locale = $locale[0];
76
77
            // now check, if this locale is "supported"
78
            if (array_search($current_locale, $supported_languages) !== false) {
79
                return $current_locale;
80
            }
81
82
            // now check, if the language to be checked is in the form of de-DE
83
            if (Str::contains($current_locale, '-')) {
84
                // extract the "main" part ("de") and append it to the end of the languages to be checked
85
                $base = explode('-', $current_locale);
86
                $language_iterator[] = $base[0];
87
            }
88
        }
89
90
        // we have not found any language that is supported
91
        throw new UnsupportedLanguageException();
92
    }
93
94
    /**
95
     * @param $request
96
     *
97
     * @return  string
98
     */
99
    private function findLanguage($request)
100
    {
101
        /*
102
         * read the accept-language from the request
103
         * if the header is missing, use the default local language
104
         */
105
        $language = Config::get('app.locale');
106
107
        if ($request->hasHeader('Accept-Language')) {
108
            $language = $request->header('Accept-Language');
109
        }
110
111
        return $language;
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    private function getSupportedLanguages()
118
    {
119
        $supported_locales = [];
120
121
        $locales = Config::get('localization-container.supported_languages');
122
123
        foreach ($locales as $key => $value) {
124
            // it is a "simple" language code (e.g., "en")!
125
            if (!is_array($value)) {
126
                $supported_locales[] = $value;
127
            }
128
129
            // it is a combined language-code (e.g., "en-US")
130
            if (is_array($value)) {
131
                foreach ($value as $k => $v) {
132
                    $supported_locales[] = $v;
133
                }
134
                $supported_locales[] = $key;
135
            }
136
        }
137
138
        return $supported_locales;
139
    }
140
141
}
142