Completed
Push — master ( fe9181...bc0bfa )
by ARCANEDEV
12s
created

Middleware::isDefaultLocaleHidden()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
ccs 2
cts 2
cp 1
crap 2
rs 10
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
16
{
17
    /* -----------------------------------------------------------------
18
     |  Properties
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
     * Middleware constructor.
41
     *
42
     * @param  \Arcanedev\Localization\Contracts\Localization  $localization
43
     */
44 15
    public function __construct(Localization $localization)
45
    {
46 15
        $this->localization = $localization;
47 15
    }
48
49
    /* -----------------------------------------------------------------
50
     |  Getters & Setters
51
     | -----------------------------------------------------------------
52
     */
53
    /**
54
     * Get the default locale.
55
     *
56
     * @return string
57
     */
58 15
    public function getDefaultLocale()
59
    {
60 15
        return $this->localization->getDefaultLocale();
61
    }
62
63
    /**
64
     * Get the current locale.
65
     *
66
     * @return string
67
     */
68 9
    public function getCurrentLocale()
69
    {
70 9
        return $this->localization->getCurrentLocale();
71
    }
72
73
    /**
74
     * Return an array of all supported Locales.
75
     *
76
     * @throws UndefinedSupportedLocalesException
77
     *
78
     * @return LocaleCollection
79
     */
80 9
    public function getSupportedLocales()
81
    {
82 9
        return $this->localization->getSupportedLocales();
83
    }
84
85
    /**
86
     * Hide the default locale in URL ??
87
     *
88
     * @return bool
89
     */
90 9
    protected function hideDefaultLocaleInURL()
91
    {
92 9
        return $this->localization->isDefaultLocaleHiddenInUrl();
93
    }
94
95
    /* -----------------------------------------------------------------
96
     |  Check Methods
97
     | -----------------------------------------------------------------
98
     */
99
    /**
100
     * Check is default locale hidden.
101
     *
102
     * @param  string|null  $locale
103
     *
104
     * @return bool
105
     */
106 6
    protected function isDefaultLocaleHidden($locale)
107
    {
108 6
        return $this->getDefaultLocale() === $locale && $this->hideDefaultLocaleInURL();
109
    }
110
111
    /**
112
     * Determine if the request has a URI that should not be localized.
113
     *
114
     * @param  \Illuminate\Http\Request  $request
115
     *
116
     * @return bool
117
     */
118 15
    protected function shouldIgnore($request)
119
    {
120 15
        foreach ($this->except as $except) {
121
            if ($except !== '/')
122
                $except = trim($except, '/');
123
124
            if ($request->is($except))
125
                return true;
126 5
        }
127
128 15
        return false;
129
    }
130
131
    /* -----------------------------------------------------------------
132
     |  Other Methods
133
     | -----------------------------------------------------------------
134
     */
135
    /**
136
     * Get the redirection response.
137
     *
138
     * @param  string  $locale
139
     *
140
     * @return \Illuminate\Http\RedirectResponse|null
141
     */
142 6
    protected function getLocalizedRedirect($locale)
143
    {
144 6
        $localizedUrl = $this->localization->getLocalizedURL($locale);
145
146 6
        if ( ! is_string($localizedUrl)) return null;
147
148 6
        return $this->makeRedirectResponse($localizedUrl);
149
    }
150
151
    /**
152
     * Make a redirect response.
153
     *
154
     * @param  string  $url
155
     * @param  int     $code
156
     *
157
     * @return \Illuminate\Http\RedirectResponse
158
     */
159 12
    protected function makeRedirectResponse($url, $code = 302)
160
    {
161 12
        return new RedirectResponse($url, $code, ['Vary' => 'Accept-Language']);
162
    }
163
}
164