Completed
Push — master ( 28331e...d2c7df )
by ARCANEDEV
06:38 queued 05:27
created

Middleware   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
dl 0
loc 166
ccs 27
cts 33
cp 0.8182
rs 10
c 0
b 0
f 0
wmc 16
lcom 2
cbo 3

10 Methods

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