| Total Complexity | 94 |
| Total Lines | 648 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ShortcutRepository 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 ShortcutRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class ShortcutRepository |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var int Number of super global (All) group |
||
| 45 | */ |
||
| 46 | protected const SUPERGLOBAL_GROUP = -100; |
||
| 47 | |||
| 48 | protected const TABLE_NAME = 'sys_be_shortcuts'; |
||
| 49 | |||
| 50 | protected array $shortcuts; |
||
| 51 | |||
| 52 | protected array $shortcutGroups; |
||
| 53 | |||
| 54 | protected ConnectionPool $connectionPool; |
||
| 55 | |||
| 56 | protected IconFactory $iconFactory; |
||
| 57 | |||
| 58 | protected ModuleLoader $moduleLoader; |
||
| 59 | |||
| 60 | public function __construct(ConnectionPool $connectionPool, IconFactory $iconFactory, ModuleLoader $moduleLoader) |
||
| 61 | { |
||
| 62 | $this->connectionPool = $connectionPool; |
||
| 63 | $this->iconFactory = $iconFactory; |
||
| 64 | $this->moduleLoader = $moduleLoader; |
||
| 65 | $this->moduleLoader->load($GLOBALS['TBE_MODULES']); |
||
| 66 | |||
| 67 | $this->shortcutGroups = $this->initShortcutGroups(); |
||
| 68 | $this->shortcuts = $this->initShortcuts(); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Gets a shortcut by its uid |
||
| 73 | * |
||
| 74 | * @param int $shortcutId Shortcut id to get the complete shortcut for |
||
| 75 | * @return mixed An array containing the shortcut's data on success or FALSE on failure |
||
| 76 | */ |
||
| 77 | public function getShortcutById(int $shortcutId) |
||
| 78 | { |
||
| 79 | foreach ($this->shortcuts as $shortcut) { |
||
| 80 | if ($shortcut['raw']['uid'] === $shortcutId) { |
||
| 81 | return $shortcut; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | return false; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Gets shortcuts for a specific group |
||
| 90 | * |
||
| 91 | * @param int $groupId Group Id |
||
| 92 | * @return array Array of shortcuts that matched the group |
||
| 93 | */ |
||
| 94 | public function getShortcutsByGroup(int $groupId): array |
||
| 95 | { |
||
| 96 | $shortcuts = []; |
||
| 97 | |||
| 98 | foreach ($this->shortcuts as $shortcut) { |
||
| 99 | if ($shortcut['group'] === $groupId) { |
||
| 100 | $shortcuts[] = $shortcut; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | return $shortcuts; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get shortcut groups the current user has access to |
||
| 109 | * |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | public function getShortcutGroups(): array |
||
| 113 | { |
||
| 114 | $shortcutGroups = $this->shortcutGroups; |
||
| 115 | |||
| 116 | if (!$this->getBackendUser()->isAdmin()) { |
||
| 117 | foreach ($shortcutGroups as $groupId => $groupName) { |
||
| 118 | if ((int)$groupId < 0) { |
||
| 119 | unset($shortcutGroups[$groupId]); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | return $shortcutGroups; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * runs through the available shortcuts and collects their groups |
||
| 129 | * |
||
| 130 | * @return array Array of groups which have shortcuts |
||
| 131 | */ |
||
| 132 | public function getGroupsFromShortcuts(): array |
||
| 133 | { |
||
| 134 | $groups = []; |
||
| 135 | |||
| 136 | foreach ($this->shortcuts as $shortcut) { |
||
| 137 | $groups[$shortcut['group']] = $this->shortcutGroups[$shortcut['group']]; |
||
| 138 | } |
||
| 139 | |||
| 140 | return array_unique($groups); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Returns if there already is a shortcut entry for a given TYPO3 URL |
||
| 145 | * |
||
| 146 | * @param string $routeIdentifier |
||
| 147 | * @param string $arguments |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function shortcutExists(string $routeIdentifier, string $arguments): bool |
||
| 151 | { |
||
| 152 | $queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::TABLE_NAME); |
||
| 153 | $queryBuilder->getRestrictions()->removeAll(); |
||
| 154 | |||
| 155 | $uid = $queryBuilder->select('uid') |
||
| 156 | ->from(self::TABLE_NAME) |
||
| 157 | ->where( |
||
| 158 | $queryBuilder->expr()->eq( |
||
| 159 | 'userid', |
||
| 160 | $queryBuilder->createNamedParameter($this->getBackendUser()->user['uid'], \PDO::PARAM_INT) |
||
| 161 | ), |
||
| 162 | $queryBuilder->expr()->eq('route', $queryBuilder->createNamedParameter($routeIdentifier)), |
||
| 163 | $queryBuilder->expr()->eq('arguments', $queryBuilder->createNamedParameter($arguments)) |
||
| 164 | ) |
||
| 165 | ->execute() |
||
| 166 | ->fetchColumn(); |
||
| 167 | |||
| 168 | return (bool)$uid; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Add a shortcut |
||
| 173 | * |
||
| 174 | * @param string $routeIdentifier route identifier of the new shortcut |
||
| 175 | * @param string $arguments arguments of the new shortcut |
||
| 176 | * @param string $title title of the new shortcut |
||
| 177 | * @return bool |
||
| 178 | * @throws \RuntimeException if the given URL is invalid |
||
| 179 | */ |
||
| 180 | public function addShortcut(string $routeIdentifier, string $arguments = '', string $title = ''): bool |
||
| 181 | { |
||
| 182 | // Do not add shortcuts for routes which do not exist |
||
| 183 | if (!$this->routeExists($routeIdentifier)) { |
||
| 184 | return false; |
||
| 185 | } |
||
| 186 | |||
| 187 | $languageService = $this->getLanguageService(); |
||
| 188 | |||
| 189 | // Only apply "magic" if title is not set |
||
| 190 | // @todo This is deprecated and can be removed in v12 |
||
| 191 | if ($title === '') { |
||
| 192 | $queryParameters = json_decode($arguments, true); |
||
| 193 | $titlePrefix = ''; |
||
| 194 | $type = 'other'; |
||
| 195 | $table = ''; |
||
| 196 | $recordId = 0; |
||
| 197 | $pageId = 0; |
||
| 198 | |||
| 199 | if ($queryParameters && is_array($queryParameters['edit'])) { |
||
| 200 | $table = (string)key($queryParameters['edit']); |
||
| 201 | $recordId = (int)key($queryParameters['edit'][$table]); |
||
| 202 | $pageId = (int)BackendUtility::getRecord($table, $recordId)['pid']; |
||
| 203 | $languageFile = 'LLL:EXT:core/Resources/Private/Language/locallang_misc.xlf'; |
||
| 204 | $action = $queryParameters['edit'][$table][$recordId]; |
||
| 205 | |||
| 206 | switch ($action) { |
||
| 207 | case 'edit': |
||
| 208 | $type = 'edit'; |
||
| 209 | $titlePrefix = $languageService->sL($languageFile . ':shortcut_edit'); |
||
| 210 | break; |
||
| 211 | case 'new': |
||
| 212 | $type = 'new'; |
||
| 213 | $titlePrefix = $languageService->sL($languageFile . ':shortcut_create'); |
||
| 214 | break; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | // Check if given id is a combined identifier |
||
| 218 | if (!empty($queryParameters['id']) && preg_match('/^[\d]+:/', $queryParameters['id'])) { |
||
| 219 | try { |
||
| 220 | $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class); |
||
| 221 | $resource = $resourceFactory->getObjectFromCombinedIdentifier($queryParameters['id']); |
||
| 222 | $title = trim(sprintf( |
||
| 223 | '%s (%s)', |
||
| 224 | $titlePrefix, |
||
| 225 | $resource->getName() |
||
| 226 | )); |
||
| 227 | } catch (ResourceDoesNotExistException $e) { |
||
|
|
|||
| 228 | } |
||
| 229 | } else { |
||
| 230 | // Lookup the title of this page and use it as default description |
||
| 231 | $pageId = $pageId ?: $recordId ?: (int)($queryParameters['id'] ?? 0); |
||
| 232 | $page = $pageId ? BackendUtility::getRecord('pages', $pageId) : null; |
||
| 233 | |||
| 234 | if (!empty($page)) { |
||
| 235 | // Set the name to the title of the page |
||
| 236 | if ($type === 'other') { |
||
| 237 | $title = sprintf( |
||
| 238 | '%s (%s)', |
||
| 239 | $title, |
||
| 240 | $page['title'] |
||
| 241 | ); |
||
| 242 | } else { |
||
| 243 | $title = sprintf( |
||
| 244 | '%s %s (%s)', |
||
| 245 | $titlePrefix, |
||
| 246 | $languageService->sL($GLOBALS['TCA'][$table]['ctrl']['title']), |
||
| 247 | $page['title'] |
||
| 248 | ); |
||
| 249 | } |
||
| 250 | } elseif (!empty($table)) { |
||
| 251 | $title = trim(sprintf( |
||
| 252 | '%s %s', |
||
| 253 | $titlePrefix, |
||
| 254 | $languageService->sL($GLOBALS['TCA'][$table]['ctrl']['title']) |
||
| 255 | )); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | // In case title is still empty try to set the modules short description label |
||
| 261 | // @todo This is deprecated and can be removed in v12 |
||
| 262 | if ($title === '') { |
||
| 263 | $moduleLabels = $this->moduleLoader->getLabelsForModule($this->getModuleNameFromRouteIdentifier($routeIdentifier)); |
||
| 264 | if (!empty($moduleLabels['shortdescription'])) { |
||
| 265 | $title = $this->getLanguageService()->sL($moduleLabels['shortdescription']); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | $queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::TABLE_NAME); |
||
| 270 | $affectedRows = $queryBuilder |
||
| 271 | ->insert(self::TABLE_NAME) |
||
| 272 | ->values([ |
||
| 273 | 'userid' => $this->getBackendUser()->user['uid'], |
||
| 274 | 'route' => $routeIdentifier, |
||
| 275 | 'arguments' => $arguments, |
||
| 276 | 'description' => $title ?: 'Shortcut', |
||
| 277 | 'sorting' => $GLOBALS['EXEC_TIME'], |
||
| 278 | ]) |
||
| 279 | ->execute(); |
||
| 280 | |||
| 281 | return $affectedRows === 1; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Update a shortcut |
||
| 286 | * |
||
| 287 | * @param int $id identifier of a shortcut |
||
| 288 | * @param string $title new title of the shortcut |
||
| 289 | * @param int $groupId new group identifier of the shortcut |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | public function updateShortcut(int $id, string $title, int $groupId): bool |
||
| 293 | { |
||
| 294 | $backendUser = $this->getBackendUser(); |
||
| 295 | $queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::TABLE_NAME); |
||
| 296 | $queryBuilder->update(self::TABLE_NAME) |
||
| 297 | ->where( |
||
| 298 | $queryBuilder->expr()->eq( |
||
| 299 | 'uid', |
||
| 300 | $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT) |
||
| 301 | ) |
||
| 302 | ) |
||
| 303 | ->set('description', $title) |
||
| 304 | ->set('sc_group', $groupId); |
||
| 305 | |||
| 306 | if (!$backendUser->isAdmin()) { |
||
| 307 | // Users can only modify their own shortcuts |
||
| 308 | $queryBuilder->andWhere( |
||
| 309 | $queryBuilder->expr()->eq( |
||
| 310 | 'userid', |
||
| 311 | $queryBuilder->createNamedParameter($backendUser->user['uid'], \PDO::PARAM_INT) |
||
| 312 | ) |
||
| 313 | ); |
||
| 314 | |||
| 315 | if ($groupId < 0) { |
||
| 316 | $queryBuilder->set('sc_group', 0); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | $affectedRows = $queryBuilder->execute(); |
||
| 321 | |||
| 322 | return $affectedRows === 1; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Remove a shortcut |
||
| 327 | * |
||
| 328 | * @param int $id identifier of a shortcut |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | public function removeShortcut(int $id): bool |
||
| 332 | { |
||
| 333 | $shortcut = $this->getShortcutById($id); |
||
| 334 | $success = false; |
||
| 335 | |||
| 336 | if ((int)$shortcut['raw']['userid'] === (int)$this->getBackendUser()->user['uid']) { |
||
| 337 | $queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::TABLE_NAME); |
||
| 338 | $affectedRows = $queryBuilder->delete(self::TABLE_NAME) |
||
| 339 | ->where( |
||
| 340 | $queryBuilder->expr()->eq( |
||
| 341 | 'uid', |
||
| 342 | $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT) |
||
| 343 | ) |
||
| 344 | ) |
||
| 345 | ->execute(); |
||
| 346 | |||
| 347 | if ($affectedRows === 1) { |
||
| 348 | $success = true; |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | return $success; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Gets the available shortcut groups from default groups, user TSConfig, and global groups |
||
| 357 | * |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | protected function initShortcutGroups(): array |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Retrieves the shortcuts for the current user |
||
| 430 | * |
||
| 431 | * @return array Array of shortcuts |
||
| 432 | */ |
||
| 433 | protected function initShortcuts(): array |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Gets a list of global groups, shortcuts in these groups are available to all users |
||
| 548 | * |
||
| 549 | * @return array Array of global groups |
||
| 550 | */ |
||
| 551 | protected function getGlobalShortcutGroups(): array |
||
| 552 | { |
||
| 553 | $globalGroups = []; |
||
| 554 | |||
| 555 | foreach ($this->shortcutGroups as $groupId => $groupLabel) { |
||
| 556 | if ($groupId < 0) { |
||
| 557 | $globalGroups[$groupId] = $groupLabel; |
||
| 558 | } |
||
| 559 | } |
||
| 560 | |||
| 561 | return $globalGroups; |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Gets the label for a shortcut group |
||
| 566 | * |
||
| 567 | * @param int $groupId A shortcut group id |
||
| 568 | * @return string The shortcut group label, can be an empty string if no group was found for the id |
||
| 569 | */ |
||
| 570 | protected function getShortcutGroupLabel(int $groupId): string |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Gets the icon for the shortcut |
||
| 577 | * |
||
| 578 | * @param string $routeIdentifier |
||
| 579 | * @param string $moduleName |
||
| 580 | * @param array $shortcut |
||
| 581 | * @return string Shortcut icon as img tag |
||
| 582 | */ |
||
| 583 | protected function getShortcutIcon(string $routeIdentifier, string $moduleName, array $shortcut): string |
||
| 584 | { |
||
| 585 | switch ($routeIdentifier) { |
||
| 586 | case 'record_edit': |
||
| 587 | $table = $shortcut['table']; |
||
| 588 | $recordid = $shortcut['recordid']; |
||
| 589 | $icon = ''; |
||
| 590 | |||
| 591 | if ($shortcut['type'] === 'edit') { |
||
| 592 | $row = BackendUtility::getRecordWSOL($table, $recordid) ?? []; |
||
| 593 | $icon = $this->iconFactory->getIconForRecord($table, $row, Icon::SIZE_SMALL)->render(); |
||
| 594 | } elseif ($shortcut['type'] === 'new') { |
||
| 595 | $icon = $this->iconFactory->getIconForRecord($table, [], Icon::SIZE_SMALL)->render(); |
||
| 596 | } |
||
| 597 | break; |
||
| 598 | case 'file_edit': |
||
| 599 | $icon = $this->iconFactory->getIcon('mimetypes-text-html', Icon::SIZE_SMALL)->render(); |
||
| 600 | break; |
||
| 601 | case 'wizard_rte': |
||
| 602 | $icon = $this->iconFactory->getIcon('mimetypes-word', Icon::SIZE_SMALL)->render(); |
||
| 603 | break; |
||
| 604 | default: |
||
| 605 | $iconIdentifier = ''; |
||
| 606 | |||
| 607 | if (strpos($moduleName, '_') !== false) { |
||
| 608 | [$mainModule, $subModule] = explode('_', $moduleName, 2); |
||
| 609 | $iconIdentifier = $this->moduleLoader->modules[$mainModule]['sub'][$subModule]['iconIdentifier'] ?? ''; |
||
| 610 | } elseif ($moduleName !== '') { |
||
| 611 | $iconIdentifier = $this->moduleLoader->modules[$moduleName]['iconIdentifier'] ?? ''; |
||
| 612 | } |
||
| 613 | |||
| 614 | if (!$iconIdentifier) { |
||
| 615 | $iconIdentifier = 'empty-empty'; |
||
| 616 | } |
||
| 617 | |||
| 618 | $icon = $this->iconFactory->getIcon($iconIdentifier, Icon::SIZE_SMALL)->render(); |
||
| 619 | } |
||
| 620 | |||
| 621 | return $icon; |
||
| 622 | } |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Get the module name from the resolved route or by static mapping for some special cases. |
||
| 626 | * |
||
| 627 | * @param string $routeIdentifier |
||
| 628 | * @return string |
||
| 629 | */ |
||
| 630 | protected function getModuleNameFromRouteIdentifier(string $routeIdentifier): string |
||
| 638 | } |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Get the route for a given route identifier |
||
| 642 | * |
||
| 643 | * @param string $routeIdentifier |
||
| 644 | * @return Route|null |
||
| 645 | */ |
||
| 646 | protected function getRoute(string $routeIdentifier): ?Route |
||
| 647 | { |
||
| 648 | return GeneralUtility::makeInstance(Router::class)->getRoutes()[$routeIdentifier] ?? null; |
||
| 649 | } |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Check if a route for the given identifier exists |
||
| 653 | * |
||
| 654 | * @param string $routeIdentifier |
||
| 655 | * @return bool |
||
| 656 | */ |
||
| 657 | protected function routeExists(string $routeIdentifier): bool |
||
| 658 | { |
||
| 659 | return $this->getRoute($routeIdentifier) !== null; |
||
| 660 | } |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Check if given route identifier is a special "no module" route |
||
| 664 | * |
||
| 665 | * @param string $routeIdentifier |
||
| 666 | * @return bool |
||
| 667 | */ |
||
| 668 | protected function isSpecialRoute(string $routeIdentifier): bool |
||
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Returns the current BE user. |
||
| 675 | * |
||
| 676 | * @return BackendUserAuthentication |
||
| 677 | */ |
||
| 678 | protected function getBackendUser(): BackendUserAuthentication |
||
| 681 | } |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @return LanguageService |
||
| 685 | */ |
||
| 686 | protected function getLanguageService(): LanguageService |
||
| 689 | } |
||
| 690 | } |
||
| 691 |