Completed
Pull Request — master (#100)
by ARCANEDEV
22:26 queued 05:58
created

Middleware::getLocalizedRedirect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
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 Arcanedev\Support\Bases\Middleware as BaseMiddleware;
7
use Illuminate\Http\RedirectResponse;
8
9
/**
10
 * Class     Middleware
11
 *
12
 * @package  Arcanedev\Localization\Bases
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
abstract class Middleware extends BaseMiddleware
0 ignored issues
show
Deprecated Code introduced by
The class Arcanedev\Support\Bases\Middleware has been deprecated with message: Use `Arcanedev\Support\Http\Middleware` instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
16
{
17
    /* -----------------------------------------------------------------
18
     |  Properties
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * The localization instance.
24
     *
25
     * @var \Arcanedev\Localization\Contracts\Localization
26
     */
27
    protected $localization;
28
29
    /**
30
     * The URIs that should not be localized.
31
     *
32
     * @var array
33
     */
34
    protected $except = [];
35
36
    /* -----------------------------------------------------------------
37
     |  Constructor
38
     | -----------------------------------------------------------------
39
     */
40
41
    /**
42
     * Middleware constructor.
43
     *
44
     * @param  \Arcanedev\Localization\Contracts\Localization  $localization
45
     */
46 15
    public function __construct(Localization $localization)
47
    {
48 15
        $this->localization = $localization;
49 15
        $this->except       = config('localization.ignored-uri', []);
50 15
    }
51
52
    /* -----------------------------------------------------------------
53
     |  Getters & Setters
54
     | -----------------------------------------------------------------
55
     */
56
    /**
57
     * Get the default locale.
58
     *
59
     * @return string
60
     */
61 15
    public function getDefaultLocale()
62
    {
63 15
        return $this->localization->getDefaultLocale();
64
    }
65
66
    /**
67
     * Get the current locale.
68
     *
69
     * @return string
70
     */
71 9
    public function getCurrentLocale()
72
    {
73 9
        return $this->localization->getCurrentLocale();
74
    }
75
76
    /**
77
     * Return an array of all supported Locales.
78
     *
79
     * @throws UndefinedSupportedLocalesException
80
     *
81
     * @return LocaleCollection
82
     */
83 9
    public function getSupportedLocales()
84
    {
85 9
        return $this->localization->getSupportedLocales();
86
    }
87
88
    /**
89
     * Hide the default locale in URL ??
90
     *
91
     * @return bool
92
     */
93 9
    protected function hideDefaultLocaleInURL()
94
    {
95 9
        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 6
    protected function isDefaultLocaleHidden($locale)
111
    {
112 6
        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 15
    protected function shouldIgnore($request)
123
    {
124 15
        foreach ($this->except as $except) {
125
            if ($except !== '/')
126
                $except = trim($except, '/');
127
128
            if ($request->is($except))
129
                return true;
130
        }
131
132 15
        return false;
133
    }
134
135
    /* -----------------------------------------------------------------
136
     |  Other Methods
137
     | -----------------------------------------------------------------
138
     */
139
140
    /**
141
     * Get the redirection response.
142
     *
143
     * @param  string  $locale
144
     *
145
     * @return \Illuminate\Http\RedirectResponse|null
146
     */
147 6
    protected function getLocalizedRedirect($locale)
148
    {
149 6
        $localizedUrl = $this->localization->getLocalizedURL($locale);
150
151 6
        if ( ! is_string($localizedUrl)) return null;
152
153 6
        return $this->makeRedirectResponse($localizedUrl);
154
    }
155
156
    /**
157
     * Make a redirect response.
158
     *
159
     * @param  string  $url
160
     * @param  int     $code
161
     *
162
     * @return \Illuminate\Http\RedirectResponse
163
     */
164 12
    protected function makeRedirectResponse($url, $code = 302)
165
    {
166 12
        return new RedirectResponse($url, $code, ['Vary' => 'Accept-Language']);
167
    }
168
}
169