Completed
Push — master ( b47912...4eb3ae )
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 Illuminate\Http\RedirectResponse;
7
use Illuminate\Http\Request;
8
9
/**
10
 * Class     Middleware
11
 *
12
 * @package  Arcanedev\Localization\Bases
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
abstract class Middleware
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 10
    public function __construct(Localization $localization)
47
    {
48 10
        $this->localization = $localization;
49 10
        $this->except       = config('localization.ignored-uri', []);
50 10
    }
51
52
    /* -----------------------------------------------------------------
53
     |  Getters & Setters
54
     | -----------------------------------------------------------------
55
     */
56
    /**
57
     * Get the default locale.
58
     *
59
     * @return string
60
     */
61 10
    public function getDefaultLocale()
62
    {
63 10
        return $this->localization->getDefaultLocale();
64
    }
65
66
    /**
67
     * Get the current locale.
68
     *
69
     * @return string
70
     */
71 6
    public function getCurrentLocale()
72
    {
73 6
        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 6
    public function getSupportedLocales()
84
    {
85 6
        return $this->localization->getSupportedLocales();
86
    }
87
88
    /**
89
     * Hide the default locale in URL ??
90
     *
91
     * @return bool
92
     */
93 6
    protected function hideDefaultLocaleInURL()
94
    {
95 6
        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 4
    protected function isDefaultLocaleHidden($locale)
111
    {
112 4
        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 10
    protected function shouldIgnore(Request $request)
123
    {
124 10
        foreach ($this->except as $except) {
125
            if ($except !== '/')
126
                $except = trim($except, '/');
127
128
            if ($request->is($except))
129
                return true;
130
        }
131
132 10
        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 4
    protected function getLocalizedRedirect($locale)
148
    {
149 4
        return is_string($localizedUrl = $this->localization->getLocalizedURL($locale))
150 4
            ? $this->makeRedirectResponse($localizedUrl)
151 4
            : null;
152
    }
153
154
    /**
155
     * Make a redirect response.
156
     *
157
     * @param  string  $url
158
     * @param  int     $code
159
     *
160
     * @return \Illuminate\Http\RedirectResponse
161
     */
162 8
    protected function makeRedirectResponse($url, $code = 302)
163
    {
164 8
        return new RedirectResponse($url, $code, ['Vary' => 'Accept-Language']);
165
    }
166
}
167