These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php namespace Arcanedev\LaravelSeo\Redirectors; |
||
2 | |||
3 | use Arcanedev\LaravelSeo\Contracts\Redirector; |
||
4 | use Illuminate\Http\Request; |
||
5 | use Illuminate\Support\Arr; |
||
6 | use Symfony\Component\HttpFoundation\Response; |
||
7 | use Illuminate\Contracts\Routing\Registrar as Router; |
||
8 | |||
9 | /** |
||
10 | * Class AbstractRedirector |
||
11 | * |
||
12 | * @package Arcanedev\LaravelSeo\Redirectors |
||
13 | * @author ARCANEDEV <[email protected]> |
||
14 | */ |
||
15 | abstract class AbstractRedirector implements Redirector |
||
16 | { |
||
17 | /* ----------------------------------------------------------------- |
||
18 | | Properties |
||
19 | | ----------------------------------------------------------------- |
||
20 | */ |
||
21 | |||
22 | /** @var \Illuminate\Contracts\Routing\Registrar */ |
||
23 | protected $router; |
||
24 | |||
25 | /** @var \Illuminate\Http\Request */ |
||
26 | protected $request; |
||
27 | |||
28 | /** @var array */ |
||
29 | protected $options = []; |
||
30 | |||
31 | /* ----------------------------------------------------------------- |
||
32 | | Constructor |
||
33 | | ----------------------------------------------------------------- |
||
34 | */ |
||
35 | |||
36 | /** |
||
37 | * AbstractRedirector constructor. |
||
38 | * |
||
39 | * @param \Illuminate\Contracts\Routing\Registrar $router |
||
40 | * @param array $options |
||
41 | */ |
||
42 | 30 | public function __construct(Router $router, array $options) |
|
43 | { |
||
44 | 30 | $this->router = $router; |
|
45 | 30 | $this->options = $options; |
|
46 | 30 | } |
|
47 | |||
48 | /* ----------------------------------------------------------------- |
||
49 | | Getters & Setters |
||
50 | | ----------------------------------------------------------------- |
||
51 | */ |
||
52 | /** |
||
53 | * Get an option. |
||
54 | * |
||
55 | * @param string $key |
||
56 | * @param mixed|null $default |
||
57 | * |
||
58 | * @return mixed |
||
59 | */ |
||
60 | 24 | public function getOption($key, $default = null) |
|
61 | { |
||
62 | 24 | return Arr::get($this->options, $key, $default); |
|
63 | } |
||
64 | |||
65 | /* ----------------------------------------------------------------- |
||
66 | | Main Methods |
||
67 | | ----------------------------------------------------------------- |
||
68 | */ |
||
69 | |||
70 | /** |
||
71 | * Get the redirect url. |
||
72 | * |
||
73 | * @param \Illuminate\Http\Request $request |
||
74 | * |
||
75 | * @return \Illuminate\Http\Response|null |
||
76 | */ |
||
77 | 18 | public function getRedirectFor(Request $request) |
|
1 ignored issue
–
show
Bug
introduced
by
Loading history...
|
|||
78 | { |
||
79 | 18 | $this->request = $request; |
|
80 | |||
81 | collect($this->getRedirectedUrls())->each(function ($redirects, $missingUrl) { |
||
82 | 18 | $this->router->get($missingUrl, function () use ($redirects) { |
|
83 | 15 | return redirect()->to( |
|
84 | 15 | $this->determineRedirectUrl($redirects), |
|
85 | 15 | $this->determineRedirectStatusCode($redirects) |
|
86 | 5 | ); |
|
87 | 18 | }); |
|
88 | 18 | }); |
|
89 | |||
90 | try { |
||
91 | 18 | return $this->router->dispatch($request); |
|
92 | } |
||
93 | 3 | catch (\Exception $e) { |
|
94 | 3 | return null; |
|
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param array|string $redirects |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 15 | protected function determineRedirectUrl($redirects) |
|
104 | { |
||
105 | 15 | return $this->resolveRouterParameters( |
|
106 | 15 | is_array($redirects) ? reset($redirects) : $redirects |
|
107 | 5 | ); |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param mixed $redirects |
||
112 | * |
||
113 | * @return int |
||
114 | */ |
||
115 | 15 | protected function determineRedirectStatusCode($redirects) |
|
116 | { |
||
117 | 15 | return is_array($redirects) ? end($redirects) : Response::HTTP_MOVED_PERMANENTLY; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param string $redirectUrl |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | 15 | protected function resolveRouterParameters($redirectUrl) |
|
126 | { |
||
127 | 15 | foreach ($this->router->getCurrentRoute()->parameters() as $key => $value) { |
|
128 | 9 | $redirectUrl = str_replace("{{$key}}", $value, $redirectUrl); |
|
129 | 5 | } |
|
130 | |||
131 | 15 | return $redirectUrl; |
|
132 | } |
||
133 | } |
||
134 |