UrlAliasMiddleware   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 69
c 3
b 0
f 1
dl 0
loc 190
rs 10
wmc 28

8 Methods

Rating   Name   Duplication   Size   Complexity  
C handle() 0 61 15
A makeNewRequest() 0 17 1
A getByPath() 0 5 1
A customize() 0 16 1
A isAvailableLocalizationPath() 0 7 2
A isAvailableCheckPath() 0 7 2
A isAvailableMethod() 0 7 3
A isTypeRedirect() 0 11 3
1
<?php
2
3
namespace Fomvasss\UrlAliases\Middleware;
4
5
use Closure;
6
use Fomvasss\UrlAliases\UrlAliasLocalization;
7
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class UrlAliasMiddleware
10
{
11
    /** @var */
12
    protected $config;
13
14
    /** @var bool */
15
    protected $useLocalization = false;
16
17
    /**
18
     * Handle an incoming request.
19
     *
20
     * @param  \Illuminate\Http\Request  $request
21
     * @param  \Closure  $next
22
     * @return mixed
23
     */
24
    public function handle($request, Closure $next)
25
    {
26
        $this->app = app();
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        $this->app = /** @scrutinizer ignore-call */ app();
Loading history...
Bug Best Practice introduced by
The property app does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
        $this->config = $this->app['config'];
28
29
        if ($this->isAvailableMethod($request) && $this->isAvailableCheckPath($request)) {
30
31
            $path = $request->path();
32
33
            // Check localization support
34
            if ($this->useLocalization = $this->config->get('url-aliases.use_localization') && $this->isAvailableLocalizationPath($request)) {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $this->useLocalization =...lizationPath($request)), Probably Intended Meaning: ($this->useLocalization ...alizationPath($request)
Loading history...
35
                $localization = $this->app->make(UrlAliasLocalization::class);
36
37
                $localizationResult = $localization->prepareLocalizePath($request->path());
38
39
                if (isset($localizationResult['redirect'])) {
40
                    $params = count($request->all()) ? '?' . http_build_query($request->all()) : '';
41
42
                    // Hide default locale in URL
43
                    return redirect()->to($localizationResult['redirect'] . $params, 302);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
                    return /** @scrutinizer ignore-call */ redirect()->to($localizationResult['redirect'] . $params, 302);
Loading history...
44
                } elseif (isset($localizationResult['path'])) {
45
                    $path = $localizationResult['path'];
46
                }
47
            }
48
49
            $urlModels = $this->getByPath($path);
50
51
            // If visited source - system path
52
            if ($urlModel = $urlModels->where('source', $path)->where('type', null)->first()) {
53
54
                $redirectStatus = $this->config->get('url-aliases.redirect_for_system_path', 301) == 301 ? 301 : 302;
55
56
                // Redirect to alias path
57
                $params = count($request->all()) ? '?' . http_build_query($request->all()) : '';
58
59
                if ($this->useLocalization) {
60
                    return redirect()->to(url($urlModel->localeAlias) . '/' . $params, $redirectStatus);
0 ignored issues
show
Bug introduced by
The function url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
                    return redirect()->to(/** @scrutinizer ignore-call */ url($urlModel->localeAlias) . '/' . $params, $redirectStatus);
Loading history...
61
                }
62
63
                return redirect()->to(url($urlModel->alias) . '/' . $params, $redirectStatus);
64
65
            // If visited alias
66
            } elseif ($urlModel = $urlModels->where('alias', $path)->where('locale', $this->app->getLocale())->where('type', null)->first()) {
67
                
68
                $newRequest = $this->makeNewRequest($request, $urlModel);
69
70
                return $next($newRequest);
71
72
            // If custom redirection
73
            } elseif ($urlModel = $urlModels->where('alias', $path)->where('type', '<>', null)->first()) {
74
75
                return redirect(url($urlModel->source), $urlModel->type);
76
                
77
            // Check if isset facet in current url and find aliased path without facet
78
            } elseif ($customReturn = $this->customize($request, $next, $path)) {
79
80
                return $customReturn;
81
            }
82
        }
83
        
84
        return $next($request);
85
    }
86
87
    /**
88
     * Remake request.
89
     * 
90
     * @param Request $request
91
     * @param $urlModel
92
     * @return Request
93
     */
94
    protected function makeNewRequest(Request $request, $urlModel, $getParams = [])
95
    {
96
        $newRequest = $request;
97
        $newRequest->server->set('REQUEST_URI', $urlModel->source);
98
        $newRequest->initialize(
99
            $request->query->all(),
100
            $request->request->all(),
101
            $request->attributes->all(),
102
            $request->cookies->all(),
103
            $request->files->all(),
104
            $newRequest->server->all() + ['ALIAS_REQUEST_URI' => $request->path(), 'ALIAS_ID' => $urlModel->id, 'ALIAS_LOCALE_BOUND' => $urlModel->locale_bound],
105
            $request->getContent()
106
        );
107
108
        $newRequest->merge($getParams);
109
110
        return $newRequest;
111
    }
112
113
    /**
114
     * @param $request
115
     * @param \Closure $next
116
     * @return mixed
117
     */
118
    public function customize($request, Closure $next, $path)
119
    {
120
        $newRequest = $request;
121
        $newRequest->server->set('REQUEST_URI', $path);
122
        $newRequest->initialize(
123
            $request->query->all(),
124
            $request->request->all(),
125
            $request->attributes->all(),
126
            $request->cookies->all(),
127
            $request->files->all(),
128
            $newRequest->server->all(),
129
            $request->getContent()
130
        );
131
        
132
       //...
133
        return $next($request);
134
    }
135
136
    /**
137
     * TODO: IS DEPRECATED
138
     * If type redirect - redirect.
139
     * @param $urlModel
140
     * @return bool|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Illuminate\Routing\Redirector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
141
     */
142
    protected function isTypeRedirect($urlModel)
143
    {
144
        if (in_array($urlModel->type, [301, 302])) {
145
            if ($this->useLocalization) {
146
                return redirect(url($urlModel->localeSource), $urlModel->type);
0 ignored issues
show
Bug introduced by
The function url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
                return redirect(/** @scrutinizer ignore-call */ url($urlModel->localeSource), $urlModel->type);
Loading history...
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
                return /** @scrutinizer ignore-call */ redirect(url($urlModel->localeSource), $urlModel->type);
Loading history...
147
            }
148
149
            return redirect()->to(url($urlModel->source), $urlModel->type);
150
        }
151
152
        return false;
153
    }
154
155
    /**
156
     * @param Request $request
157
     * @return bool
158
     */
159
    protected function isAvailableCheckPath(Request $request)
160
    {
161
        if ($request->is(...$this->config->get('url-aliases.ignored_paths', []))) {
162
            return false;
163
        }
164
        
165
        return true;
166
    }
167
168
    protected function isAvailableLocalizationPath(Request $request)
169
    {
170
        if ($request->is(...$this->config->get('url-aliases-laravellocalization.urlsIgnored', []))) {
171
            return false;
172
        }
173
174
        return true;
175
    }
176
177
    /**
178
     * @param Request $request
179
     * @return bool
180
     */
181
    protected function isAvailableMethod(Request $request)
182
    {
183
        if (in_array($request->getMethod(), $this->config->get('url-aliases.available_methods', [])) || empty($this->config->get('url-aliases.available_methods', []))) {
184
            return true;
185
        }
186
        
187
        return false;
188
    }
189
190
    /**
191
     * @param $path
192
     * @return mixed
193
     */
194
    protected function getByPath($path)
195
    {
196
        $model = $this->config->get('url-aliases.model', \Fomvasss\UrlAliases\Models\UrlAlias::class);
197
        
198
        return $model::byPath($path)->get();
199
    }
200
}
201