Completed
Pull Request — master (#105)
by ARCANEDEV
10:46
created

Middleware   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 85.19%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 154
ccs 23
cts 27
cp 0.8519
rs 10
c 1
b 0
f 0
wmc 14
lcom 2
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getDefaultLocale() 0 4 1
A getCurrentLocale() 0 4 1
A getSupportedLocales() 0 4 1
A hideDefaultLocaleInURL() 0 4 1
A isDefaultLocaleHidden() 0 4 2
A shouldIgnore() 0 12 4
A getLocalizedRedirect() 0 8 2
A makeRedirectResponse() 0 4 1
1
<?php namespace Arcanedev\Localization\Middleware;
2
3
use Arcanedev\Localization\Contracts\Localization;
4
use Arcanedev\Localization\Entities\LocaleCollection;
5
use Arcanedev\Localization\Exceptions\UndefinedSupportedLocalesException;
6
use Illuminate\Http\RedirectResponse;
7
8
/**
9
 * Class     Middleware
10
 *
11
 * @package  Arcanedev\Localization\Bases
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
abstract class Middleware
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * The localization instance.
23
     *
24
     * @var \Arcanedev\Localization\Contracts\Localization
25
     */
26
    protected $localization;
27
28
    /**
29
     * The URIs that should not be localized.
30
     *
31
     * @var array
32
     */
33
    protected $except = [];
34
35
    /* -----------------------------------------------------------------
36
     |  Constructor
37
     | -----------------------------------------------------------------
38
     */
39
40
    /**
41
     * Middleware constructor.
42
     *
43
     * @param  \Arcanedev\Localization\Contracts\Localization  $localization
44
     */
45 10
    public function __construct(Localization $localization)
46
    {
47 10
        $this->localization = $localization;
48 10
        $this->except       = config('localization.ignored-uri', []);
49 10
    }
50
51
    /* -----------------------------------------------------------------
52
     |  Getters & Setters
53
     | -----------------------------------------------------------------
54
     */
55
    /**
56
     * Get the default locale.
57
     *
58
     * @return string
59
     */
60 10
    public function getDefaultLocale()
61
    {
62 10
        return $this->localization->getDefaultLocale();
63
    }
64
65
    /**
66
     * Get the current locale.
67
     *
68
     * @return string
69
     */
70 6
    public function getCurrentLocale()
71
    {
72 6
        return $this->localization->getCurrentLocale();
73
    }
74
75
    /**
76
     * Return an array of all supported Locales.
77
     *
78
     * @throws UndefinedSupportedLocalesException
79
     *
80
     * @return LocaleCollection
81
     */
82 6
    public function getSupportedLocales()
83
    {
84 6
        return $this->localization->getSupportedLocales();
85
    }
86
87
    /**
88
     * Hide the default locale in URL ??
89
     *
90
     * @return bool
91
     */
92 6
    protected function hideDefaultLocaleInURL()
93
    {
94 6
        return $this->localization->isDefaultLocaleHiddenInUrl();
95
    }
96
97
    /* -----------------------------------------------------------------
98
     |  Check Methods
99
     | -----------------------------------------------------------------
100
     */
101
102
    /**
103
     * Check is default locale hidden.
104
     *
105
     * @param  string|null  $locale
106
     *
107
     * @return bool
108
     */
109 4
    protected function isDefaultLocaleHidden($locale)
110
    {
111 4
        return $this->getDefaultLocale() === $locale && $this->hideDefaultLocaleInURL();
112
    }
113
114
    /**
115
     * Determine if the request has a URI that should not be localized.
116
     *
117
     * @param  \Illuminate\Http\Request  $request
118
     *
119
     * @return bool
120
     */
121 10
    protected function shouldIgnore($request)
122
    {
123 10
        foreach ($this->except as $except) {
124
            if ($except !== '/')
125
                $except = trim($except, '/');
126
127
            if ($request->is($except))
128
                return true;
129
        }
130
131 10
        return false;
132
    }
133
134
    /* -----------------------------------------------------------------
135
     |  Other Methods
136
     | -----------------------------------------------------------------
137
     */
138
139
    /**
140
     * Get the redirection response.
141
     *
142
     * @param  string  $locale
143
     *
144
     * @return \Illuminate\Http\RedirectResponse|null
145
     */
146 4
    protected function getLocalizedRedirect($locale)
147
    {
148 4
        $localizedUrl = $this->localization->getLocalizedURL($locale);
149
150 4
        if ( ! is_string($localizedUrl)) return null;
151
152 4
        return $this->makeRedirectResponse($localizedUrl);
153
    }
154
155
    /**
156
     * Make a redirect response.
157
     *
158
     * @param  string  $url
159
     * @param  int     $code
160
     *
161
     * @return \Illuminate\Http\RedirectResponse
162
     */
163 8
    protected function makeRedirectResponse($url, $code = 302)
164
    {
165 8
        return new RedirectResponse($url, $code, ['Vary' => 'Accept-Language']);
166
    }
167
}
168