Completed
Push — master ( bc0bfa...b3fff2 )
by ARCANEDEV
10s
created

Middleware   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 150
ccs 24
cts 28
cp 0.8571
rs 10
wmc 14
lcom 2
cbo 2

9 Methods

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