| Total Complexity | 66 |
| Total Lines | 392 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ConfigurationService 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 ConfigurationService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class ConfigurationService |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var BackendUserAuthentication|null |
||
| 45 | */ |
||
| 46 | private $backendUser; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var UrlService |
||
| 50 | */ |
||
| 51 | private $urlService; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var ConfigurationRepository |
||
| 55 | */ |
||
| 56 | private $configurationRepository; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | private $extensionSettings; |
||
| 62 | |||
| 63 | public function __construct() |
||
| 69 | } |
||
| 70 | |||
| 71 | public static function removeDisallowedConfigurations(array $allowedConfigurations, array $configurations): array |
||
| 82 | } |
||
| 83 | |||
| 84 | public function getConfigurationFromPageTS(array $pageTSConfig, int $pageId, array $res, string $mountPoint = ''): array |
||
| 119 | } |
||
| 120 | |||
| 121 | public function getConfigurationFromDatabase(int $pageId, array $res): array |
||
| 167 | } |
||
| 168 | |||
| 169 | public function expandExcludeString(string $excludeString): array |
||
| 170 | { |
||
| 171 | // internal static caches; |
||
| 172 | static $expandedExcludeStringCache; |
||
| 173 | static $treeCache; |
||
| 174 | |||
| 175 | if (empty($expandedExcludeStringCache[$excludeString])) { |
||
| 176 | $pidList = []; |
||
| 177 | |||
| 178 | if (! empty($excludeString)) { |
||
| 179 | /** @var PageTreeView $tree */ |
||
| 180 | $tree = GeneralUtility::makeInstance(PageTreeView::class); |
||
| 181 | $tree->init('AND ' . $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW)); |
||
| 182 | |||
| 183 | $excludeParts = GeneralUtility::trimExplode(',', $excludeString); |
||
| 184 | |||
| 185 | foreach ($excludeParts as $excludePart) { |
||
| 186 | [$pid, $depth] = GeneralUtility::trimExplode('+', $excludePart); |
||
| 187 | |||
| 188 | // default is "page only" = "depth=0" |
||
| 189 | if (empty($depth)) { |
||
| 190 | $depth = (strpos($excludePart, '+') !== false) ? 99 : 0; |
||
| 191 | } |
||
| 192 | |||
| 193 | $pidList[] = (int) $pid; |
||
| 194 | if ($depth > 0) { |
||
| 195 | $pidList = $this->expandPidList($treeCache, $pid, $depth, $tree, $pidList); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | $expandedExcludeStringCache[$excludeString] = array_unique($pidList); |
||
| 201 | } |
||
| 202 | |||
| 203 | return $expandedExcludeStringCache[$excludeString]; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Will expand the parameters configuration to individual values. This follows a certain syntax of the value of each parameter. |
||
| 208 | * Syntax of values: |
||
| 209 | * - Basically: If the value is wrapped in [...] it will be expanded according to the following syntax, otherwise the value is taken literally |
||
| 210 | * - Configuration is splitted by "|" and the parts are processed individually and finally added together |
||
| 211 | * - For each configuration part: |
||
| 212 | * - "[int]-[int]" = Integer range, will be expanded to all values in between, values included, starting from low to high (max. 1000). Example "1-34" or "-40--30" |
||
| 213 | * - "_TABLE:[TCA table name];[_PID:[optional page id, default is current page]];[_ENABLELANG:1]" = Look up of table records from PID, filtering out deleted records. Example "_TABLE:tt_content; _PID:123" |
||
| 214 | * _ENABLELANG:1 picks only original records without their language overlays |
||
| 215 | * - Default: Literal value |
||
| 216 | */ |
||
| 217 | private function expandParameters(array $paramArray, int $pid): array |
||
| 218 | { |
||
| 219 | // Traverse parameter names: |
||
| 220 | foreach ($paramArray as $parameter => $parameterValue) { |
||
| 221 | $parameterValue = trim($parameterValue); |
||
| 222 | |||
| 223 | // If value is encapsulated in square brackets it means there are some ranges of values to find, otherwise the value is literal |
||
| 224 | if ($this->isWrappedInSquareBrackets($parameterValue)) { |
||
| 225 | // So, find the value inside brackets and reset the paramArray value as an array. |
||
| 226 | $parameterValue = substr($parameterValue, 1, -1); |
||
| 227 | $paramArray[$parameter] = []; |
||
| 228 | |||
| 229 | // Explode parts and traverse them: |
||
| 230 | $parts = explode('|', $parameterValue); |
||
| 231 | foreach ($parts as $part) { |
||
| 232 | |||
| 233 | // Look for integer range: (fx. 1-34 or -40--30 // reads minus 40 to minus 30) |
||
| 234 | if (preg_match('/^(-?[0-9]+)\s*-\s*(-?[0-9]+)$/', trim($part), $reg)) { |
||
| 235 | $reg = $this->swapIfFirstIsLargerThanSecond($reg); |
||
| 236 | $paramArray = $this->addValuesInRange($reg, $paramArray, $parameter); |
||
| 237 | } elseif (strpos(trim($part), '_TABLE:') === 0) { |
||
| 238 | |||
| 239 | // Parse parameters: |
||
| 240 | $subparts = GeneralUtility::trimExplode(';', $part); |
||
| 241 | $subpartParams = []; |
||
| 242 | foreach ($subparts as $spV) { |
||
| 243 | [$pKey, $pVal] = GeneralUtility::trimExplode(':', $spV); |
||
| 244 | $subpartParams[$pKey] = $pVal; |
||
| 245 | } |
||
| 246 | |||
| 247 | // Table exists: |
||
| 248 | if (isset($GLOBALS['TCA'][$subpartParams['_TABLE']])) { |
||
| 249 | $lookUpPid = isset($subpartParams['_PID']) ? intval($subpartParams['_PID']) : $pid; |
||
| 250 | $recursiveDepth = isset($subpartParams['_RECURSIVE']) ? intval($subpartParams['_RECURSIVE']) : 0; |
||
| 251 | $pidField = isset($subpartParams['_PIDFIELD']) ? trim($subpartParams['_PIDFIELD']) : 'pid'; |
||
| 252 | $where = $subpartParams['_WHERE'] ?? ''; |
||
| 253 | $addTable = $subpartParams['_ADDTABLE'] ?? ''; |
||
| 254 | |||
| 255 | $fieldName = $subpartParams['_FIELD'] ?: 'uid'; |
||
| 256 | if ($fieldName === 'uid' || $GLOBALS['TCA'][$subpartParams['_TABLE']]['columns'][$fieldName]) { |
||
| 257 | $queryBuilder = $this->getQueryBuilder($subpartParams['_TABLE']); |
||
| 258 | $pidArray = $this->getPidArray($recursiveDepth, $lookUpPid); |
||
| 259 | |||
| 260 | $queryBuilder->getRestrictions() |
||
| 261 | ->removeAll() |
||
| 262 | ->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 263 | |||
| 264 | $queryBuilder |
||
| 265 | ->select($fieldName) |
||
| 266 | ->from($subpartParams['_TABLE']) |
||
| 267 | ->where( |
||
| 268 | $queryBuilder->expr()->in($pidField, $queryBuilder->createNamedParameter($pidArray, Connection::PARAM_INT_ARRAY)), |
||
| 269 | $where |
||
| 270 | ); |
||
| 271 | |||
| 272 | if (! empty($addTable)) { |
||
| 273 | // TODO: Check if this works as intended! |
||
| 274 | $queryBuilder->add('from', $addTable); |
||
| 275 | } |
||
| 276 | $transOrigPointerField = $GLOBALS['TCA'][$subpartParams['_TABLE']]['ctrl']['transOrigPointerField']; |
||
| 277 | |||
| 278 | if ($subpartParams['_ENABLELANG'] && $transOrigPointerField) { |
||
| 279 | $queryBuilder->andWhere( |
||
| 280 | $queryBuilder->expr()->lte( |
||
| 281 | $transOrigPointerField, |
||
| 282 | 0 |
||
| 283 | ) |
||
| 284 | ); |
||
| 285 | } |
||
| 286 | |||
| 287 | $statement = $queryBuilder->execute(); |
||
| 288 | |||
| 289 | $rows = []; |
||
| 290 | while ($row = $statement->fetch()) { |
||
| 291 | $rows[$row[$fieldName]] = $row; |
||
| 292 | } |
||
| 293 | |||
| 294 | if (is_array($rows)) { |
||
| 295 | $paramArray[$parameter] = array_merge($paramArray[$parameter], array_keys($rows)); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | } |
||
| 299 | } else { |
||
| 300 | // Just add value: |
||
| 301 | $paramArray[$parameter][] = $part; |
||
| 302 | } |
||
| 303 | // Hook for processing own expandParameters place holder |
||
| 304 | $paramArray = $this->runExpandParametersHook($paramArray, $parameter, $part, $pid); |
||
| 305 | } |
||
| 306 | |||
| 307 | // Make unique set of values and sort array by key: |
||
| 308 | $paramArray[$parameter] = array_unique($paramArray[$parameter]); |
||
| 309 | ksort($paramArray); |
||
| 310 | } else { |
||
| 311 | // Set the literal value as only value in array: |
||
| 312 | $paramArray[$parameter] = [$parameterValue]; |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | return $paramArray; |
||
| 317 | } |
||
| 318 | |||
| 319 | private function isWrappedInSquareBrackets(string $string): bool |
||
| 320 | { |
||
| 321 | return (strpos($string, '[') === 0 && substr($string, -1) === ']'); |
||
| 322 | } |
||
| 323 | |||
| 324 | private function swapIfFirstIsLargerThanSecond(array $reg): array |
||
| 325 | { |
||
| 326 | // Swap if first is larger than last: |
||
| 327 | if ($reg[1] > $reg[2]) { |
||
| 328 | $temp = $reg[2]; |
||
| 329 | $reg[2] = $reg[1]; |
||
| 330 | $reg[1] = $temp; |
||
| 331 | } |
||
| 332 | |||
| 333 | return $reg; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return BackendUserAuthentication |
||
| 338 | */ |
||
| 339 | private function getBackendUser() |
||
| 340 | { |
||
| 341 | // Make sure the _cli_ user is loaded |
||
| 342 | Bootstrap::initializeBackendAuthentication(); |
||
| 343 | if ($this->backendUser === null) { |
||
| 344 | $this->backendUser = $GLOBALS['BE_USER']; |
||
| 345 | } |
||
| 346 | return $this->backendUser; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get querybuilder for given table |
||
| 351 | * |
||
| 352 | * @return QueryBuilder |
||
| 353 | */ |
||
| 354 | private function getQueryBuilder(string $table) |
||
| 355 | { |
||
| 356 | return GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table); |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param $parameter |
||
| 361 | * @param $path |
||
| 362 | */ |
||
| 363 | private function runExpandParametersHook(array $paramArray, $parameter, $path, int $pid): array |
||
| 378 | } |
||
| 379 | |||
| 380 | protected function getUrlService(): UrlService |
||
| 381 | { |
||
| 382 | $this->urlService = $this->urlService ?? GeneralUtility::makeInstance(UrlService::class); |
||
| 383 | return $this->urlService; |
||
| 384 | } |
||
| 385 | |||
| 386 | private function getPidArray(int $recursiveDepth, int $lookUpPid): array |
||
| 387 | { |
||
| 388 | if ($recursiveDepth > 0) { |
||
| 389 | /** @var QueryGenerator $queryGenerator */ |
||
| 390 | $queryGenerator = GeneralUtility::makeInstance(QueryGenerator::class); |
||
| 391 | $pidList = $queryGenerator->getTreeList($lookUpPid, $recursiveDepth, 0, 1); |
||
| 392 | $pidArray = GeneralUtility::intExplode(',', $pidList); |
||
| 393 | } else { |
||
| 394 | $pidArray = [$lookUpPid]; |
||
| 395 | } |
||
| 396 | return $pidArray; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param $parameter |
||
| 401 | * |
||
| 402 | * Traverse range, add values: |
||
| 403 | * Limit to size of range! |
||
| 404 | */ |
||
| 405 | private function addValuesInRange(array $reg, array $paramArray, $parameter): array |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param $depth |
||
| 420 | */ |
||
| 421 | private function expandPidList(array $treeCache, string $pid, $depth, PageTreeView $tree, array $pidList): array |
||
| 433 | } |
||
| 434 | } |
||
| 435 |