Total Complexity | 53 |
Total Lines | 332 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like RequestBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RequestBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class RequestBuilder implements SingletonInterface |
||
38 | { |
||
39 | /** |
||
40 | * This is a unique key for a plugin (not the extension key!) |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $pluginName = 'plugin'; |
||
45 | |||
46 | /** |
||
47 | * The name of the extension (in UpperCamelCase) |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $extensionName; |
||
52 | |||
53 | /** |
||
54 | * The class name of the default controller |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | private $defaultControllerClassName; |
||
59 | |||
60 | /** |
||
61 | * The default controller name |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $defaultControllerName = ''; |
||
66 | |||
67 | /** |
||
68 | * The default format of the response object |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $defaultFormat = 'html'; |
||
73 | |||
74 | /** |
||
75 | * The allowed actions of the controller. This actions can be called via $_GET and $_POST. |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $allowedControllerActions = []; |
||
80 | |||
81 | /** |
||
82 | * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface |
||
83 | */ |
||
84 | protected $configurationManager; |
||
85 | |||
86 | /** |
||
87 | * @var \TYPO3\CMS\Extbase\Service\ExtensionService |
||
88 | */ |
||
89 | protected $extensionService; |
||
90 | |||
91 | /** |
||
92 | * @var \TYPO3\CMS\Extbase\Service\EnvironmentService |
||
93 | */ |
||
94 | protected $environmentService; |
||
95 | |||
96 | /** |
||
97 | * @var array |
||
98 | */ |
||
99 | private $controllerAliasToClassMapping = []; |
||
100 | |||
101 | /** |
||
102 | * @var array |
||
103 | */ |
||
104 | private $controllerClassToAliasMapping = []; |
||
105 | |||
106 | /** |
||
107 | * @var array|string[] |
||
108 | */ |
||
109 | private $allowedControllerAliases = []; |
||
110 | |||
111 | /** |
||
112 | * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager |
||
113 | */ |
||
114 | public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager) |
||
115 | { |
||
116 | $this->configurationManager = $configurationManager; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param \TYPO3\CMS\Extbase\Service\ExtensionService $extensionService |
||
121 | */ |
||
122 | public function injectExtensionService(ExtensionService $extensionService) |
||
123 | { |
||
124 | $this->extensionService = $extensionService; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService |
||
129 | */ |
||
130 | public function injectEnvironmentService(EnvironmentService $environmentService) |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @throws MvcException |
||
137 | * @see \TYPO3\CMS\Extbase\Core\Bootstrap::initializeConfiguration |
||
138 | */ |
||
139 | protected function loadDefaultValues() |
||
140 | { |
||
141 | // todo: See comment in \TYPO3\CMS\Extbase\Core\Bootstrap::initializeConfiguration for further explanation |
||
142 | // todo: on why we shouldn't use the configuration manager here. |
||
143 | $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); |
||
144 | if (empty($configuration['extensionName'])) { |
||
145 | throw new MvcException('"extensionName" is not properly configured. Request can\'t be dispatched!', 1289843275); |
||
146 | } |
||
147 | if (empty($configuration['pluginName'])) { |
||
148 | throw new MvcException('"pluginName" is not properly configured. Request can\'t be dispatched!', 1289843277); |
||
149 | } |
||
150 | $this->extensionName = $configuration['extensionName']; |
||
151 | $this->pluginName = $configuration['pluginName']; |
||
152 | $defaultControllerConfiguration = reset($configuration['controllerConfiguration']) ?? []; |
||
153 | $this->defaultControllerClassName = $defaultControllerConfiguration['className'] ?? null; |
||
154 | $this->defaultControllerName = $defaultControllerConfiguration['alias'] ?? null; |
||
155 | $this->allowedControllerActions = []; |
||
156 | foreach ($configuration['controllerConfiguration'] as $controllerClassName => $controllerConfiguration) { |
||
157 | $this->allowedControllerActions[$controllerClassName] = $controllerConfiguration['actions'] ?? null; |
||
158 | $this->controllerAliasToClassMapping[$controllerConfiguration['alias']] = $controllerConfiguration['className']; |
||
159 | $this->controllerClassToAliasMapping[$controllerConfiguration['className']] = $controllerConfiguration['alias']; |
||
160 | $this->allowedControllerAliases[] = $controllerConfiguration['alias']; |
||
161 | } |
||
162 | if (!empty($configuration['format'])) { |
||
163 | $this->defaultFormat = $configuration['format']; |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Builds a web request object from the raw HTTP information and the configuration |
||
169 | * |
||
170 | * @return \TYPO3\CMS\Extbase\Mvc\Request The web request as an object |
||
171 | */ |
||
172 | public function build() |
||
173 | { |
||
174 | $this->loadDefaultValues(); |
||
175 | $pluginNamespace = $this->extensionService->getPluginNamespace($this->extensionName, $this->pluginName); |
||
176 | /** @var \TYPO3\CMS\Core\Http\ServerRequest $typo3Request */ |
||
177 | $typo3Request = $GLOBALS['TYPO3_REQUEST'] ?? null; |
||
178 | if ($typo3Request instanceof ServerRequestInterface) { |
||
|
|||
179 | $queryArguments = $typo3Request->getAttribute('routing'); |
||
180 | if ($queryArguments instanceof PageArguments) { |
||
181 | $getParameters = $queryArguments->get($pluginNamespace) ?? []; |
||
182 | } else { |
||
183 | $getParameters = $typo3Request->getQueryParams()[$pluginNamespace] ?? []; |
||
184 | } |
||
185 | $bodyParameters = $typo3Request->getParsedBody()[$pluginNamespace] ?? []; |
||
186 | $parameters = $getParameters; |
||
187 | ArrayUtility::mergeRecursiveWithOverrule($parameters, $bodyParameters); |
||
188 | } else { |
||
189 | $parameters = GeneralUtility::_GPmerged($pluginNamespace); |
||
190 | } |
||
191 | |||
192 | $files = $this->untangleFilesArray($_FILES); |
||
193 | if (is_array($files[$pluginNamespace] ?? null)) { |
||
194 | $parameters = array_replace_recursive($parameters, $files[$pluginNamespace]); |
||
195 | } |
||
196 | |||
197 | $controllerClassName = $this->resolveControllerClassName($parameters); |
||
198 | $actionName = $this->resolveActionName($controllerClassName, $parameters); |
||
199 | |||
200 | $baseUri = GeneralUtility::getIndpEnv('TYPO3_SITE_URL'); |
||
201 | if ($this->environmentService->isEnvironmentInBackendMode()) { |
||
202 | $baseUri .= TYPO3_mainDir; |
||
203 | } |
||
204 | |||
205 | /** @var \TYPO3\CMS\Extbase\Mvc\Request $request */ |
||
206 | $request = GeneralUtility::makeInstance(Request::class); |
||
207 | $request->setPluginName($this->pluginName); |
||
208 | $request->setControllerExtensionName($this->extensionName); |
||
209 | $request->setControllerAliasToClassNameMapping($this->controllerAliasToClassMapping); |
||
210 | $request->setControllerName($this->controllerClassToAliasMapping[$controllerClassName]); |
||
211 | $request->setControllerActionName($actionName); |
||
212 | // @todo Use Environment |
||
213 | $request->setRequestUri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL')); |
||
214 | $request->setBaseUri($baseUri); |
||
215 | $request->setMethod($this->getServerRequestMethod($typo3Request)); |
||
216 | if (isset($parameters['format']) && is_string($parameters['format']) && $parameters['format'] !== '') { |
||
217 | $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING)); |
||
218 | } else { |
||
219 | $request->setFormat($this->defaultFormat); |
||
220 | } |
||
221 | foreach ($parameters as $argumentName => $argumentValue) { |
||
222 | $request->setArgument($argumentName, $argumentValue); |
||
223 | } |
||
224 | return $request; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Returns the current ControllerName extracted from given $parameters. |
||
229 | * If no controller is specified, the defaultControllerName will be returned. |
||
230 | * If that's not available, an exception is thrown. |
||
231 | * |
||
232 | * @param array $parameters |
||
233 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException |
||
234 | * @throws MvcException if the controller could not be resolved |
||
235 | * @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException |
||
236 | * @return string |
||
237 | */ |
||
238 | protected function resolveControllerClassName(array $parameters) |
||
239 | { |
||
240 | if (!isset($parameters['controller']) || $parameters['controller'] === '') { |
||
241 | if (empty($this->defaultControllerClassName)) { |
||
242 | throw new MvcException('The default controller for extension "' . $this->extensionName . '" and plugin "' . $this->pluginName . '" can not be determined. Please check for TYPO3\\CMS\\Extbase\\Utility\\ExtensionUtility::configurePlugin() in your ext_localconf.php.', 1316104317); |
||
243 | } |
||
244 | return $this->defaultControllerClassName; |
||
245 | } |
||
246 | $controllerClassName = $this->controllerAliasToClassMapping[$parameters['controller']] ?? ''; |
||
247 | if (!in_array($controllerClassName, array_keys($this->allowedControllerActions))) { |
||
248 | $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); |
||
249 | if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (bool)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) { |
||
250 | throw new PageNotFoundException('The requested resource was not found', 1313857897); |
||
251 | } |
||
252 | if (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) { |
||
253 | return $this->defaultControllerClassName; |
||
254 | } |
||
255 | throw new InvalidControllerNameException( |
||
256 | 'The controller "' . $parameters['controller'] . '" is not allowed by plugin "' . $this->pluginName . '". Please check for TYPO3\\CMS\\Extbase\\Utility\\ExtensionUtility::configurePlugin() in your ext_localconf.php.', |
||
257 | 1313855173 |
||
258 | ); |
||
259 | } |
||
260 | return filter_var($controllerClassName, FILTER_SANITIZE_STRING); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Returns the current actionName extracted from given $parameters. |
||
265 | * If no action is specified, the defaultActionName will be returned. |
||
266 | * If that's not available or the specified action is not defined in the current plugin, an exception is thrown. |
||
267 | * |
||
268 | * @param string $controllerClassName |
||
269 | * @param array $parameters |
||
270 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidActionNameException |
||
271 | * @throws MvcException |
||
272 | * @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException |
||
273 | * @return string |
||
274 | */ |
||
275 | protected function resolveActionName($controllerClassName, array $parameters) |
||
276 | { |
||
277 | $defaultActionName = is_array($this->allowedControllerActions[$controllerClassName]) ? current($this->allowedControllerActions[$controllerClassName]) : ''; |
||
278 | if (!isset($parameters['action']) || $parameters['action'] === '') { |
||
279 | if ($defaultActionName === '') { |
||
280 | throw new MvcException('The default action can not be determined for controller "' . $controllerClassName . '". Please check TYPO3\\CMS\\Extbase\\Utility\\ExtensionUtility::configurePlugin() in your ext_localconf.php.', 1295479651); |
||
281 | } |
||
282 | return $defaultActionName; |
||
283 | } |
||
284 | $actionName = $parameters['action']; |
||
285 | $allowedActionNames = $this->allowedControllerActions[$controllerClassName]; |
||
286 | if (!in_array($actionName, $allowedActionNames)) { |
||
287 | $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); |
||
288 | if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (bool)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) { |
||
289 | throw new PageNotFoundException('The requested resource was not found', 1313857898); |
||
290 | } |
||
291 | if (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) { |
||
292 | return $defaultActionName; |
||
293 | } |
||
294 | throw new InvalidActionNameException('The action "' . $actionName . '" (controller "' . $controllerClassName . '") is not allowed by this plugin / module. Please check TYPO3\\CMS\\Extbase\\Utility\\ExtensionUtility::configurePlugin() in your ext_localconf.php / TYPO3\\CMS\\Extbase\\Utility\\ExtensionUtility::configureModule() in your ext_tables.php.', 1313855175); |
||
295 | } |
||
296 | return filter_var($actionName, FILTER_SANITIZE_STRING); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Transforms the convoluted _FILES superglobal into a manageable form. |
||
301 | * |
||
302 | * @param array $convolutedFiles The _FILES superglobal |
||
303 | * @return array Untangled files |
||
304 | */ |
||
305 | protected function untangleFilesArray(array $convolutedFiles) |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Returns an array of all possibles "field paths" for the given array. |
||
340 | * |
||
341 | * @param array $structure The array to walk through |
||
342 | * @param string $firstLevelFieldName |
||
343 | * @return array An array of paths (as strings) in the format "key1/key2/key3" ... |
||
344 | */ |
||
345 | protected function calculateFieldPaths(array $structure, $firstLevelFieldName = null) |
||
346 | { |
||
347 | $fieldPaths = []; |
||
348 | if (is_array($structure)) { |
||
349 | foreach ($structure as $key => $subStructure) { |
||
350 | $fieldPath = ($firstLevelFieldName !== null ? $firstLevelFieldName . '/' : '') . $key; |
||
351 | if (is_array($subStructure)) { |
||
352 | foreach ($this->calculateFieldPaths($subStructure) as $subFieldPath) { |
||
353 | $fieldPaths[] = $fieldPath . '/' . $subFieldPath; |
||
354 | } |
||
355 | } else { |
||
356 | $fieldPaths[] = $fieldPath; |
||
357 | } |
||
358 | } |
||
359 | } |
||
360 | return $fieldPaths; |
||
361 | } |
||
362 | |||
363 | protected function getServerRequestMethod(?ServerRequestInterface $typo3Request): string |
||
369 | } |
||
370 | } |
||
371 |