Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 14 |
CRAP | |
0.00% |
0 / 1 |
DefaultUrlService | |
0.00% |
0 / 41 |
|
0.00% |
0 / 14 |
462 | |
0.00% |
0 / 1 |
getUrlRoute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getUrl | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getLoginUrl | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getLogoutUrl | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getDefaultUrl | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getRoute | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getRoutes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setRoute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getRedirectUrl | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
resolveRedirect | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
createRedirect | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
getRedirectByDynamicConfigProvider | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getRedirectByOptionConfig | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getDynamicConfigProvider | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /* |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | */ |
20 | |
21 | namespace oat\tao\model\mvc; |
22 | |
23 | use common_Exception; |
24 | use common_exception_Error; |
25 | use common_Logger; |
26 | use Laminas\ServiceManager\ServiceLocatorAwareInterface; |
27 | use oat\oatbox\service\ConfigurableService; |
28 | use oat\oatbox\service\exception\InvalidServiceManagerException; |
29 | use oat\tao\model\DynamicConfig\DynamicConfigProviderInterface; |
30 | use oat\tao\model\mvc\DefaultUrlModule\RedirectResolveInterface; |
31 | use Psr\Container\ContainerExceptionInterface; |
32 | use Psr\Container\NotFoundExceptionInterface; |
33 | |
34 | class DefaultUrlService extends ConfigurableService |
35 | { |
36 | public const SERVICE_ID = 'tao/urlroute'; |
37 | |
38 | /** |
39 | * |
40 | * @param string $name |
41 | * @return array |
42 | */ |
43 | public function getUrlRoute($name) |
44 | { |
45 | return $this->getOption($name); |
46 | } |
47 | |
48 | public function getUrl($name, array $params = []) |
49 | { |
50 | $route = $this->getOption($name); |
51 | return _url($route['action'], $route['controller'], $route['ext'], $params); |
52 | } |
53 | |
54 | /** |
55 | * |
56 | * @throws ContainerExceptionInterface |
57 | * @throws InvalidServiceManagerException |
58 | * @throws NotFoundExceptionInterface |
59 | */ |
60 | public function getLoginUrl(array $params = []): ?string |
61 | { |
62 | return $this->getDynamicConfigProvider()->getConfigByName( |
63 | DynamicConfigProviderInterface::LOGIN_URL_CONFIG_NAME |
64 | ) ?? $this->getUrl('login', $params); |
65 | } |
66 | |
67 | /** |
68 | * |
69 | * @return string |
70 | */ |
71 | public function getLogoutUrl(array $params = []) |
72 | { |
73 | return $this->getUrl('logout', $params); |
74 | } |
75 | |
76 | /** |
77 | * |
78 | * @return string |
79 | */ |
80 | public function getDefaultUrl(array $params = []) |
81 | { |
82 | return $this->getUrl('default', $params); |
83 | } |
84 | |
85 | /** |
86 | * Get the config associated to given $name |
87 | * |
88 | * @param $name |
89 | * @return mixed |
90 | * @throws common_Exception |
91 | */ |
92 | public function getRoute($name) |
93 | { |
94 | if (!$this->hasOption($name)) { |
95 | throw new common_Exception('Route ' . $name . ' not found into UrlService config'); |
96 | } |
97 | return $this->getOption($name); |
98 | } |
99 | |
100 | /** |
101 | * Get all routes from the configuration |
102 | * |
103 | * @return array |
104 | */ |
105 | public function getRoutes() |
106 | { |
107 | return $this->getOptions(); |
108 | } |
109 | |
110 | /** |
111 | * Set the config associated to given $name |
112 | * |
113 | * @param $name |
114 | * @param array $value |
115 | */ |
116 | public function setRoute($name, array $value) |
117 | { |
118 | $this->setOption($name, $value); |
119 | } |
120 | |
121 | /** |
122 | * @param string $name |
123 | * @throws ContainerExceptionInterface |
124 | * @throws InvalidServiceManagerException |
125 | * @throws NotFoundExceptionInterface |
126 | * @throws common_exception_Error |
127 | */ |
128 | public function getRedirectUrl($name): ?string |
129 | { |
130 | if ($this->hasOption($name)) { |
131 | return $this->getRedirectByDynamicConfigProvider($name) |
132 | ?? $this->getRedirectByOptionConfig($name); |
133 | } |
134 | return ''; |
135 | } |
136 | |
137 | /** |
138 | * @param array $redirectParams |
139 | * @return string |
140 | * @throws common_exception_Error |
141 | * @throws InvalidServiceManagerException |
142 | */ |
143 | protected function resolveRedirect(array $redirectParams) |
144 | { |
145 | $redirectAdapterClass = $redirectParams['class']; |
146 | if (is_a($redirectAdapterClass, RedirectResolveInterface::class, true)) { |
147 | /** |
148 | * @var RedirectResolveInterface $redirectAdapter |
149 | */ |
150 | $redirectAdapter = new $redirectAdapterClass(); |
151 | if ($redirectAdapter instanceof ServiceLocatorAwareInterface) { |
152 | $redirectAdapter->setServiceLocator($this->getServiceManager()); |
153 | } |
154 | |
155 | return $redirectAdapter->resolve($redirectParams['options']); |
156 | } |
157 | throw new common_exception_Error( |
158 | 'invalid redirect resolver class ' . $redirectAdapterClass . '. it must implements ' |
159 | . RedirectResolveInterface::class |
160 | ); |
161 | } |
162 | |
163 | /** |
164 | * @param string|array $redirect |
165 | * @return string |
166 | * @throws common_exception_Error |
167 | * @throws InvalidServiceManagerException |
168 | */ |
169 | public function createRedirect($redirect) |
170 | { |
171 | if (is_string($redirect) && filter_var($redirect, FILTER_VALIDATE_URL)) { |
172 | common_Logger::w('deprecated usage or redirect'); |
173 | return $redirect; |
174 | } |
175 | return $this->resolveRedirect($redirect); |
176 | } |
177 | |
178 | /** |
179 | * @throws InvalidServiceManagerException |
180 | * @throws ContainerExceptionInterface |
181 | * @throws NotFoundExceptionInterface |
182 | */ |
183 | private function getRedirectByDynamicConfigProvider(string $name): ?string |
184 | { |
185 | return $this->getDynamicConfigProvider()->getConfigByName($name); |
186 | } |
187 | |
188 | /** |
189 | * @throws InvalidServiceManagerException |
190 | * @throws common_exception_Error |
191 | */ |
192 | private function getRedirectByOptionConfig(string $name): ?string |
193 | { |
194 | $options = $this->getOption($name); |
195 | if (array_key_exists('redirect', $options)) { |
196 | return $this->createRedirect($options['redirect']); |
197 | } |
198 | |
199 | return null; |
200 | } |
201 | |
202 | /** |
203 | * @throws InvalidServiceManagerException |
204 | * @throws ContainerExceptionInterface |
205 | * @throws NotFoundExceptionInterface |
206 | */ |
207 | private function getDynamicConfigProvider(): DynamicConfigProviderInterface |
208 | { |
209 | return $this->getServiceManager()->getContainer()->get(DynamicConfigProviderInterface::class); |
210 | } |
211 | } |