Completed
Pull Request — master (#152)
by ARCANEDEV
02:23
created

Middleware::getIgnoredRedirection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 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
     * Get the ignored URI/Route.
100
     *
101
     * @return array
102
     */
103 30
    protected function getIgnoredRedirection(): array
104
    {
105 30
        return config('localization.ignored-redirection', []);
106
    }
107
108
    /* -----------------------------------------------------------------
109
     |  Check Methods
110
     | -----------------------------------------------------------------
111
     */
112
113
    /**
114
     * Check is default locale hidden.
115
     *
116
     * @param  string|null  $locale
117
     *
118
     * @return bool
119
     */
120 12
    protected function isDefaultLocaleHidden($locale)
121
    {
122 12
        return $this->getDefaultLocale() === $locale && $this->hideDefaultLocaleInURL();
123
    }
124
125
    /**
126
     * Determine if the request has a URI that should not be localized.
127
     *
128
     * @param  \Illuminate\Http\Request  $request
129
     *
130
     * @return bool
131
     */
132 30
    protected function shouldIgnore(Request $request)
133
    {
134 30
        foreach ($this->except as $except) {
135
            if ($except !== '/') {
136
                $except = trim($except, '/');
137
            }
138
139
            if ($request->is($except)) {
140
                return true;
141
            }
142
143
            if ($request->routeIs($except)) {
144
                return true;
145
            }
146
        }
147
148 30
        return false;
149
    }
150
151
    /* -----------------------------------------------------------------
152
     |  Other Methods
153
     | -----------------------------------------------------------------
154
     */
155
156
    /**
157
     * Get the redirection response.
158
     *
159
     * @param  string  $locale
160
     *
161
     * @return \Illuminate\Http\RedirectResponse|null
162
     */
163 12
    protected function getLocalizedRedirect($locale)
164
    {
165 12
        return is_string($localizedUrl = $this->localization->getLocalizedURL($locale))
166 12
            ? $this->makeRedirectResponse($localizedUrl)
167 12
            : null;
168
    }
169
170
    /**
171
     * Make a redirect response.
172
     *
173
     * @param  string    $url
174
     * @param  int|null  $code
175
     *
176
     * @return \Illuminate\Http\RedirectResponse
177
     */
178 24
    protected function makeRedirectResponse($url, $code = null)
179
    {
180 24
        return new RedirectResponse($url, $code ?? config('localization.redirection-code', 302), ['Vary' => 'Accept-Language']);
181
    }
182
}
183