These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Arcanedev\MissingUrlsRedirector; |
||
6 | |||
7 | use Arcanedev\MissingUrlsRedirector\Entities\Redirection; |
||
8 | use Arcanedev\MissingUrlsRedirector\Contracts\{RedirectorManager, RedirectorProvider}; |
||
9 | use Arcanedev\MissingUrlsRedirector\Entities\RedirectionCollection; |
||
10 | use Arcanedev\MissingUrlsRedirector\Events\RedirectionNotFound; |
||
11 | use Arcanedev\MissingUrlsRedirector\Helpers\RouteMaker; |
||
12 | use Arcanedev\MissingUrlsRedirector\RedirectorProviders\ConfigProvider; |
||
13 | use Illuminate\Http\{Request, Response}; |
||
14 | use Illuminate\Support\Arr; |
||
15 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
16 | |||
17 | /** |
||
18 | * Class MissingUrlsRedirector |
||
19 | * |
||
20 | * @package Arcanedev\MissingUrlsRedirector |
||
21 | * @author ARCANEDEV <[email protected]> |
||
22 | */ |
||
23 | class MissingUrlsRedirector implements RedirectorManager |
||
24 | { |
||
25 | /* ----------------------------------------------------------------- |
||
26 | | Properties |
||
27 | | ----------------------------------------------------------------- |
||
28 | */ |
||
29 | |||
30 | /** @var \Arcanedev\MissingUrlsRedirector\Contracts\RedirectorProvider|mixed */ |
||
31 | protected $redirector; |
||
32 | |||
33 | /** |
||
34 | * @var \Arcanedev\MissingUrlsRedirector\Entities\RedirectionCollection |
||
35 | */ |
||
36 | protected $redirections; |
||
37 | |||
38 | /* ----------------------------------------------------------------- |
||
39 | | Constructor |
||
40 | | ----------------------------------------------------------------- |
||
41 | */ |
||
42 | |||
43 | /** |
||
44 | * RedirectionManager constructor. |
||
45 | * |
||
46 | * @param \Arcanedev\MissingUrlsRedirector\Contracts\RedirectorProvider $redirector |
||
47 | */ |
||
48 | public function __construct(RedirectorProvider $redirector) |
||
49 | { |
||
50 | $this->setRedirector($redirector); |
||
51 | $this->setRedirections(new RedirectionCollection); |
||
52 | } |
||
53 | |||
54 | /* ----------------------------------------------------------------- |
||
55 | | Getters & Setters |
||
56 | | ----------------------------------------------------------------- |
||
57 | */ |
||
58 | |||
59 | /** |
||
60 | * Get the redirections. |
||
61 | * |
||
62 | * @return \Arcanedev\MissingUrlsRedirector\Entities\RedirectionCollection |
||
63 | */ |
||
64 | public function getRedirections(): RedirectionCollection |
||
65 | { |
||
66 | return $this->redirections; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Set the redirections. |
||
71 | * |
||
72 | * @param \Arcanedev\MissingUrlsRedirector\Entities\RedirectionCollection $redirections |
||
73 | * |
||
74 | * @return $this |
||
75 | */ |
||
76 | public function setRedirections(RedirectionCollection $redirections): self |
||
77 | { |
||
78 | $this->redirections = $redirections; |
||
79 | |||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Get the redirected status codes. |
||
85 | * |
||
86 | * @return array|null |
||
87 | */ |
||
88 | public function getRedirectedStatusCodes(): ?array |
||
89 | { |
||
90 | return $this->getRedirector()->statusCodes(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Get the redirector. |
||
95 | * |
||
96 | * @param \Arcanedev\MissingUrlsRedirector\Contracts\RedirectorProvider|mixed $redirector |
||
97 | * |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function setRedirector(RedirectorProvider $redirector): self |
||
101 | { |
||
102 | $this->redirector = $redirector; |
||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Get the redirector. |
||
109 | * |
||
110 | * @return \Arcanedev\MissingUrlsRedirector\Contracts\RedirectorProvider|mixed |
||
111 | */ |
||
112 | protected function getRedirector(): RedirectorProvider |
||
113 | { |
||
114 | return $this->redirector ?? new ConfigProvider; |
||
0 ignored issues
–
show
|
|||
115 | } |
||
116 | |||
117 | /* ----------------------------------------------------------------- |
||
118 | | Main Methods |
||
119 | | ----------------------------------------------------------------- |
||
120 | */ |
||
121 | |||
122 | /** |
||
123 | * Register a redirection. |
||
124 | * |
||
125 | * @param iterable|string $from |
||
126 | * @param string $to |
||
127 | * @param int $status |
||
128 | * |
||
129 | * @return $this |
||
130 | */ |
||
131 | public function register($from, string $to, int $status = Response::HTTP_MOVED_PERMANENTLY): self |
||
132 | { |
||
133 | if (is_string($from)) { |
||
134 | $this->getRedirections()->addOne($from, $to, $status); |
||
135 | } |
||
136 | |||
137 | if (is_iterable($from)) { |
||
138 | $this->getRedirections()->addMany($from, $to, $status); |
||
139 | } |
||
140 | |||
141 | return $this; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Register a redirection (entity). |
||
146 | * |
||
147 | * @param \Arcanedev\MissingUrlsRedirector\Entities\Redirection $redirection |
||
148 | * |
||
149 | * @return $this |
||
150 | */ |
||
151 | public function registerRedirection(Redirection $redirection) |
||
152 | { |
||
153 | $this->getRedirections()->addRedirection($redirection); |
||
154 | |||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Get the redirection for the given request. |
||
160 | * |
||
161 | * @param \Illuminate\Http\Request $request |
||
162 | * |
||
163 | * @return \Illuminate\Http\Response|null |
||
164 | */ |
||
165 | public function getRedirectionFor(Request $request) |
||
166 | { |
||
167 | $this->loadRedirections( |
||
168 | $this->getRedirector()->redirectionsFor($request) |
||
169 | ); |
||
170 | |||
171 | $routes = RouteMaker::makeCollection($this->getRedirections()); |
||
172 | |||
173 | try { |
||
174 | return $routes->match($request)->run(); |
||
175 | } |
||
176 | catch (NotFoundHttpException $e) { |
||
177 | event(new RedirectionNotFound($request)); |
||
178 | } |
||
179 | |||
180 | return null; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Load the redirections collection. |
||
185 | * |
||
186 | * @param \Arcanedev\MissingUrlsRedirector\Entities\Redirection[]|array $redirections |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | private function loadRedirections(array $redirections): self |
||
191 | { |
||
192 | foreach ($redirections as $from => $redirection) { |
||
193 | if ($redirection instanceof Redirection) |
||
194 | $this->registerRedirection($redirection); |
||
195 | else |
||
196 | $this->register($from, ...Arr::wrap($redirection)); |
||
197 | } |
||
198 | |||
199 | return $this; |
||
200 | } |
||
201 | } |
||
202 |
This check looks for function calls that miss required arguments.