| Total Complexity | 49 |
| Total Lines | 303 |
| 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 ConfigurationManagerInterface |
||
| 83 | */ |
||
| 84 | protected $configurationManager; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var ExtensionService |
||
| 88 | */ |
||
| 89 | protected $extensionService; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | private $controllerAliasToClassMapping = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | private $controllerClassToAliasMapping = []; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var array|string[] |
||
| 103 | */ |
||
| 104 | private $allowedControllerAliases = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param ConfigurationManagerInterface $configurationManager |
||
| 108 | */ |
||
| 109 | public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager) |
||
| 110 | { |
||
| 111 | $this->configurationManager = $configurationManager; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param ExtensionService $extensionService |
||
| 116 | */ |
||
| 117 | public function injectExtensionService(ExtensionService $extensionService) |
||
| 118 | { |
||
| 119 | $this->extensionService = $extensionService; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @throws MvcException |
||
| 124 | * @see \TYPO3\CMS\Extbase\Core\Bootstrap::initializeConfiguration |
||
| 125 | */ |
||
| 126 | protected function loadDefaultValues() |
||
| 127 | { |
||
| 128 | // todo: See comment in \TYPO3\CMS\Extbase\Core\Bootstrap::initializeConfiguration for further explanation |
||
| 129 | // todo: on why we shouldn't use the configuration manager here. |
||
| 130 | $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); |
||
| 131 | if (empty($configuration['extensionName'])) { |
||
| 132 | throw new MvcException('"extensionName" is not properly configured. Request can\'t be dispatched!', 1289843275); |
||
| 133 | } |
||
| 134 | if (empty($configuration['pluginName'])) { |
||
| 135 | throw new MvcException('"pluginName" is not properly configured. Request can\'t be dispatched!', 1289843277); |
||
| 136 | } |
||
| 137 | $this->extensionName = $configuration['extensionName']; |
||
| 138 | $this->pluginName = $configuration['pluginName']; |
||
| 139 | $defaultControllerConfiguration = reset($configuration['controllerConfiguration']) ?? []; |
||
| 140 | $this->defaultControllerClassName = $defaultControllerConfiguration['className'] ?? null; |
||
| 141 | $this->defaultControllerName = $defaultControllerConfiguration['alias'] ?? null; |
||
| 142 | $this->allowedControllerActions = []; |
||
| 143 | foreach ($configuration['controllerConfiguration'] as $controllerClassName => $controllerConfiguration) { |
||
| 144 | $this->allowedControllerActions[$controllerClassName] = $controllerConfiguration['actions'] ?? null; |
||
| 145 | $this->controllerAliasToClassMapping[$controllerConfiguration['alias']] = $controllerConfiguration['className']; |
||
| 146 | $this->controllerClassToAliasMapping[$controllerConfiguration['className']] = $controllerConfiguration['alias']; |
||
| 147 | $this->allowedControllerAliases[] = $controllerConfiguration['alias']; |
||
| 148 | } |
||
| 149 | if (!empty($configuration['format'])) { |
||
| 150 | $this->defaultFormat = $configuration['format']; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Builds a web request object from the raw HTTP information and the configuration |
||
| 156 | * |
||
| 157 | * @param ServerRequestInterface $mainRequest |
||
| 158 | * @return Request The web request as an object |
||
| 159 | */ |
||
| 160 | public function build(ServerRequestInterface $mainRequest) |
||
| 161 | { |
||
| 162 | $this->loadDefaultValues(); |
||
| 163 | $pluginNamespace = $this->extensionService->getPluginNamespace($this->extensionName, $this->pluginName); |
||
| 164 | /** @var NormalizedParams $normalizedParams */ |
||
| 165 | $normalizedParams = $mainRequest->getAttribute('normalizedParams'); |
||
| 166 | $queryArguments = $mainRequest->getAttribute('routing'); |
||
| 167 | if ($queryArguments instanceof PageArguments) { |
||
| 168 | $parameters = $queryArguments->get($pluginNamespace) ?? []; |
||
| 169 | } else { |
||
| 170 | $parameters = $mainRequest->getQueryParams()[$pluginNamespace] ?? []; |
||
| 171 | } |
||
| 172 | $parameters = is_array($parameters) ? $parameters : []; |
||
| 173 | if ($mainRequest->getMethod() === 'POST') { |
||
| 174 | $postParameters = $mainRequest->getParsedBody()[$pluginNamespace] ?? []; |
||
| 175 | $postParameters = is_array($postParameters) ? $postParameters : []; |
||
| 176 | ArrayUtility::mergeRecursiveWithOverrule($parameters, $postParameters); |
||
| 177 | } |
||
| 178 | |||
| 179 | $files = $this->untangleFilesArray($_FILES); |
||
| 180 | if (is_array($files[$pluginNamespace] ?? null)) { |
||
| 181 | $parameters = array_replace_recursive($parameters, $files[$pluginNamespace]); |
||
| 182 | } |
||
| 183 | |||
| 184 | $controllerClassName = $this->resolveControllerClassName($parameters); |
||
| 185 | $actionName = $this->resolveActionName($controllerClassName, $parameters); |
||
| 186 | |||
| 187 | $request = GeneralUtility::makeInstance(Request::class); |
||
| 188 | $request->setPluginName($this->pluginName); |
||
| 189 | $request->setControllerExtensionName($this->extensionName); |
||
| 190 | $request->setControllerAliasToClassNameMapping($this->controllerAliasToClassMapping); |
||
| 191 | $request->setControllerName($this->controllerClassToAliasMapping[$controllerClassName]); |
||
| 192 | $request->setControllerActionName($actionName); |
||
| 193 | $request->setRequestUri($normalizedParams->getRequestUrl()); |
||
| 194 | $request->setMethod($mainRequest->getMethod()); |
||
| 195 | if (isset($parameters['format']) && is_string($parameters['format']) && $parameters['format'] !== '') { |
||
| 196 | $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING)); |
||
| 197 | } else { |
||
| 198 | $request->setFormat($this->defaultFormat); |
||
| 199 | } |
||
| 200 | foreach ($parameters as $argumentName => $argumentValue) { |
||
| 201 | $request->setArgument($argumentName, $argumentValue); |
||
| 202 | } |
||
| 203 | return $request; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Returns the current ControllerName extracted from given $parameters. |
||
| 208 | * If no controller is specified, the defaultControllerName will be returned. |
||
| 209 | * If that's not available, an exception is thrown. |
||
| 210 | * |
||
| 211 | * @param array $parameters |
||
| 212 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException |
||
| 213 | * @throws MvcException if the controller could not be resolved |
||
| 214 | * @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | protected function resolveControllerClassName(array $parameters) |
||
| 218 | { |
||
| 219 | if (!isset($parameters['controller']) || $parameters['controller'] === '') { |
||
| 220 | if (empty($this->defaultControllerClassName)) { |
||
| 221 | 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); |
||
| 222 | } |
||
| 223 | return $this->defaultControllerClassName; |
||
| 224 | } |
||
| 225 | $controllerClassName = $this->controllerAliasToClassMapping[$parameters['controller']] ?? ''; |
||
| 226 | if (!in_array($controllerClassName, array_keys($this->allowedControllerActions))) { |
||
| 227 | $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); |
||
| 228 | if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (bool)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) { |
||
| 229 | throw new PageNotFoundException('The requested resource was not found', 1313857897); |
||
| 230 | } |
||
| 231 | if (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) { |
||
| 232 | return $this->defaultControllerClassName; |
||
| 233 | } |
||
| 234 | throw new InvalidControllerNameException( |
||
| 235 | '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.', |
||
| 236 | 1313855173 |
||
| 237 | ); |
||
| 238 | } |
||
| 239 | return filter_var($controllerClassName, FILTER_SANITIZE_STRING); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Returns the current actionName extracted from given $parameters. |
||
| 244 | * If no action is specified, the defaultActionName will be returned. |
||
| 245 | * If that's not available or the specified action is not defined in the current plugin, an exception is thrown. |
||
| 246 | * |
||
| 247 | * @param string $controllerClassName |
||
| 248 | * @param array $parameters |
||
| 249 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidActionNameException |
||
| 250 | * @throws MvcException |
||
| 251 | * @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | protected function resolveActionName($controllerClassName, array $parameters) |
||
| 255 | { |
||
| 256 | $defaultActionName = is_array($this->allowedControllerActions[$controllerClassName]) ? current($this->allowedControllerActions[$controllerClassName]) : ''; |
||
| 257 | if (!isset($parameters['action']) || $parameters['action'] === '') { |
||
| 258 | if ($defaultActionName === '') { |
||
| 259 | 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); |
||
| 260 | } |
||
| 261 | return $defaultActionName; |
||
| 262 | } |
||
| 263 | $actionName = $parameters['action']; |
||
| 264 | $allowedActionNames = $this->allowedControllerActions[$controllerClassName]; |
||
| 265 | if (!in_array($actionName, $allowedActionNames)) { |
||
| 266 | $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); |
||
| 267 | if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (bool)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) { |
||
| 268 | throw new PageNotFoundException('The requested resource was not found', 1313857898); |
||
| 269 | } |
||
| 270 | if (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) { |
||
| 271 | return $defaultActionName; |
||
| 272 | } |
||
| 273 | 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); |
||
| 274 | } |
||
| 275 | return filter_var($actionName, FILTER_SANITIZE_STRING); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Transforms the convoluted _FILES superglobal into a manageable form. |
||
| 280 | * |
||
| 281 | * @param array $convolutedFiles The _FILES superglobal |
||
| 282 | * @return array Untangled files |
||
| 283 | */ |
||
| 284 | protected function untangleFilesArray(array $convolutedFiles) |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns an array of all possibles "field paths" for the given array. |
||
| 319 | * |
||
| 320 | * @param array $structure The array to walk through |
||
| 321 | * @param string $firstLevelFieldName |
||
| 322 | * @return array An array of paths (as strings) in the format "key1/key2/key3" ... |
||
| 323 | */ |
||
| 324 | protected function calculateFieldPaths(array $structure, $firstLevelFieldName = null) |
||
| 340 | } |
||
| 341 | } |
||
| 342 |