We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 77 |
| Total Lines | 579 |
| Duplicated Lines | 0 % |
| Changes | 14 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AbstractController 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 AbstractController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | abstract class AbstractController extends ActionController implements LoggerAwareInterface |
||
| 43 | { |
||
| 44 | use LoggerAwareTrait; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @access protected |
||
| 48 | * @var DocumentRepository |
||
| 49 | */ |
||
| 50 | protected DocumentRepository $documentRepository; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @access public |
||
| 54 | * |
||
| 55 | * @param DocumentRepository $documentRepository |
||
| 56 | * |
||
| 57 | * @return void |
||
| 58 | */ |
||
| 59 | public function injectDocumentRepository(DocumentRepository $documentRepository): void |
||
| 60 | { |
||
| 61 | $this->documentRepository = $documentRepository; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @access protected |
||
| 66 | * @var Document|null This holds the current document |
||
| 67 | */ |
||
| 68 | protected ?Document $document = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @access protected |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $documentArray; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @access protected |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected array $extConf; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @access protected |
||
| 84 | * @var array This holds the request parameter |
||
| 85 | */ |
||
| 86 | protected array $requestData; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @access protected |
||
| 90 | * @var array This holds some common data for the fluid view |
||
| 91 | */ |
||
| 92 | protected array $viewData; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @access protected |
||
| 96 | * @var int |
||
| 97 | */ |
||
| 98 | protected int $pageUid; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Initialize the plugin controller |
||
| 102 | * |
||
| 103 | * @access protected |
||
| 104 | * |
||
| 105 | * @param RequestInterface $request the HTTP request |
||
| 106 | * |
||
| 107 | * @return void |
||
| 108 | */ |
||
| 109 | protected function initialize(RequestInterface $request): void |
||
| 110 | { |
||
| 111 | /** @var Request $request */ |
||
| 112 | $this->requestData = $request->getQueryParams()['tx_dlf'] ?? []; |
||
| 113 | $this->pageUid = (int) ($this->requestData['id'] ?? 0); |
||
| 114 | $this->requestData['page'] = $this->requestData['page'] ?? 1; |
||
| 115 | |||
| 116 | // Sanitize user input to prevent XSS attacks. |
||
| 117 | $this->sanitizeRequestData(); |
||
| 118 | |||
| 119 | // Get extension configuration. |
||
| 120 | $this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf'); |
||
| 121 | |||
| 122 | $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__); |
||
| 123 | |||
| 124 | $this->viewData = [ |
||
| 125 | 'pageUid' => $this->pageUid, |
||
| 126 | 'uniqueId' => uniqid(), |
||
| 127 | 'requestData' => $this->requestData |
||
| 128 | ]; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Loads the current document into $this->document |
||
| 133 | * |
||
| 134 | * @access protected |
||
| 135 | * |
||
| 136 | * @param int $documentId The document's UID (fallback: $this->requestData[id]) |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | protected function loadDocument(int $documentId = 0): void |
||
| 141 | { |
||
| 142 | // Sanitize FlexForm settings to avoid later casting. |
||
| 143 | $this->sanitizeSettings(); |
||
| 144 | |||
| 145 | // Get document ID from request data if not passed as parameter. |
||
| 146 | if ($documentId === 0 && !empty($this->requestData['id'])) { |
||
| 147 | $documentId = $this->requestData['id']; |
||
| 148 | } |
||
| 149 | |||
| 150 | // Try to get document format from database |
||
| 151 | if (!empty($documentId)) { |
||
| 152 | |||
| 153 | $doc = null; |
||
| 154 | |||
| 155 | if (MathUtility::canBeInterpretedAsInteger($documentId)) { |
||
| 156 | $doc = $this->getDocumentByUid($documentId); |
||
| 157 | } elseif (GeneralUtility::isValidUrl($documentId)) { |
||
| 158 | $doc = $this->getDocumentByUrl($documentId); |
||
| 159 | } |
||
| 160 | |||
| 161 | if ($this->document !== null && $doc !== null) { |
||
| 162 | $this->document->setCurrentDocument($doc); |
||
| 163 | } |
||
| 164 | |||
| 165 | } elseif (!empty($this->requestData['recordId'])) { |
||
| 166 | |||
| 167 | $this->document = $this->documentRepository->findOneByRecordId($this->requestData['recordId']); |
||
|
|
|||
| 168 | |||
| 169 | if ($this->document !== null) { |
||
| 170 | $doc = AbstractDocument::getInstance($this->document->getLocation(), $this->settings, true); |
||
| 171 | if ($doc !== null) { |
||
| 172 | $this->document->setCurrentDocument($doc); |
||
| 173 | } else { |
||
| 174 | $this->logger->error('Failed to load document with record ID "' . $this->requestData['recordId'] . '"'); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } else { |
||
| 178 | $this->logger->error('Invalid ID "' . $documentId . '" or PID "' . $this->settings['storagePid'] . '" for document loading'); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Configure URL for proxy. |
||
| 184 | * |
||
| 185 | * @access protected |
||
| 186 | * |
||
| 187 | * @param string $url URL for proxy configuration |
||
| 188 | * |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | protected function configureProxyUrl(string &$url): void |
||
| 192 | { |
||
| 193 | $this->uriBuilder->reset() |
||
| 194 | ->setTargetPageUid($this->pageUid) |
||
| 195 | ->setCreateAbsoluteUri(!empty($this->extConf['general']['forceAbsoluteUrl'])) |
||
| 196 | ->setArguments( |
||
| 197 | [ |
||
| 198 | 'eID' => 'tx_dlf_pageview_proxy', |
||
| 199 | 'url' => $url, |
||
| 200 | 'uHash' => GeneralUtility::hmac($url, 'PageViewProxy') |
||
| 201 | ] |
||
| 202 | ) |
||
| 203 | ->build(); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Checks if doc is missing or is empty (no pages) |
||
| 208 | * |
||
| 209 | * @access protected |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | protected function isDocMissingOrEmpty(): bool |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Checks if doc is missing |
||
| 221 | * |
||
| 222 | * @access protected |
||
| 223 | * |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | protected function isDocMissing(): bool |
||
| 227 | { |
||
| 228 | return $this->document === null || $this->document->getCurrentDocument() === null; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the LanguageService |
||
| 233 | * |
||
| 234 | * @access protected |
||
| 235 | * |
||
| 236 | * @return LanguageService |
||
| 237 | */ |
||
| 238 | protected function getLanguageService(): LanguageService |
||
| 239 | { |
||
| 240 | return $GLOBALS['LANG']; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Safely gets Parameters from request if they exist |
||
| 245 | * |
||
| 246 | * @access protected |
||
| 247 | * |
||
| 248 | * @param string $parameterName |
||
| 249 | * |
||
| 250 | * @return null|string|array |
||
| 251 | */ |
||
| 252 | protected function getParametersSafely(string $parameterName) |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Sanitize input variables. |
||
| 262 | * |
||
| 263 | * @access protected |
||
| 264 | * |
||
| 265 | * @return void |
||
| 266 | */ |
||
| 267 | protected function sanitizeRequestData(): void |
||
| 268 | { |
||
| 269 | // tx_dlf[id] may only be an UID or URI. |
||
| 270 | if ( |
||
| 271 | !empty($this->requestData['id']) |
||
| 272 | && !MathUtility::canBeInterpretedAsInteger($this->requestData['id']) |
||
| 273 | && !GeneralUtility::isValidUrl($this->requestData['id']) |
||
| 274 | ) { |
||
| 275 | $this->logger->warning('Invalid ID or URI "' . $this->requestData['id'] . '" for document loading'); |
||
| 276 | unset($this->requestData['id']); |
||
| 277 | } |
||
| 278 | |||
| 279 | // tx_dlf[page] may only be a positive integer or valid XML ID. |
||
| 280 | if ( |
||
| 281 | !empty($this->requestData['page']) |
||
| 282 | && !MathUtility::canBeInterpretedAsInteger($this->requestData['page']) |
||
| 283 | && !Helper::isValidXmlId($this->requestData['page']) |
||
| 284 | ) { |
||
| 285 | $this->requestData['page'] = 1; |
||
| 286 | } |
||
| 287 | |||
| 288 | // tx_dlf[double] may only be 0 or 1. |
||
| 289 | $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'] ?? 0, 0, 1); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Sanitize settings from FlexForm. |
||
| 294 | * |
||
| 295 | * @access protected |
||
| 296 | * |
||
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | protected function sanitizeSettings(): void |
||
| 300 | { |
||
| 301 | $this->setDefaultIntSetting('storagePid', 0); |
||
| 302 | |||
| 303 | if ($this instanceof MetadataController) { |
||
| 304 | $this->setDefaultIntSetting('rootline', 0); |
||
| 305 | $this->setDefaultIntSetting('originalIiifMetadata', 0); |
||
| 306 | $this->setDefaultIntSetting('displayIiifDescription', 1); |
||
| 307 | $this->setDefaultIntSetting('displayIiifRights', 1); |
||
| 308 | $this->setDefaultIntSetting('displayIiifLinks', 1); |
||
| 309 | } |
||
| 310 | |||
| 311 | if ($this instanceof NavigationController) { |
||
| 312 | $this->setDefaultIntSetting('pageStep', 5); |
||
| 313 | } |
||
| 314 | |||
| 315 | if ($this instanceof OaiPmhController) { |
||
| 316 | $this->setDefaultIntSetting('limit', 5); |
||
| 317 | $this->setDefaultIntSetting('solr_limit', 50000); |
||
| 318 | } |
||
| 319 | |||
| 320 | if ($this instanceof PageViewController) { |
||
| 321 | $this->setDefaultIntSetting('useInternalProxy', 0); |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Sets default value for setting if not yet set. |
||
| 327 | * |
||
| 328 | * @access protected |
||
| 329 | * |
||
| 330 | * @param string $setting name of setting |
||
| 331 | * @param int $value for being set if empty |
||
| 332 | * |
||
| 333 | * @return void |
||
| 334 | */ |
||
| 335 | protected function setDefaultIntSetting(string $setting, int $value): void |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Sets page value. |
||
| 347 | * |
||
| 348 | * @access protected |
||
| 349 | * |
||
| 350 | * @return void |
||
| 351 | */ |
||
| 352 | protected function setPage(): void |
||
| 353 | { |
||
| 354 | if (!empty($this->requestData['logicalPage'])) { |
||
| 355 | $this->requestData['page'] = $this->document->getCurrentDocument()->getPhysicalPage($this->requestData['logicalPage']); |
||
| 356 | // The logical page parameter should not appear again |
||
| 357 | unset($this->requestData['logicalPage']); |
||
| 358 | } |
||
| 359 | |||
| 360 | $this->setDefaultPage(); |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Sets default page value. |
||
| 365 | * |
||
| 366 | * @access protected |
||
| 367 | * |
||
| 368 | * @return void |
||
| 369 | */ |
||
| 370 | protected function setDefaultPage(): void |
||
| 371 | { |
||
| 372 | // Set default values if not set. |
||
| 373 | // $this->requestData['page'] may be integer or string (physical structure @ID) |
||
| 374 | if ( |
||
| 375 | isset($this->requestData['page']) |
||
| 376 | && ( |
||
| 377 | (int) $this->requestData['page'] > 0 |
||
| 378 | || empty($this->requestData['page']) |
||
| 379 | || is_array($this->requestData['docPage']) |
||
| 380 | ) |
||
| 381 | ) { |
||
| 382 | if (isset($this->settings['multiViewType']) && $this->document->getCurrentDocument()->tableOfContents[0]['type'] === $this->settings['multiViewType']) { |
||
| 383 | $i = 0; |
||
| 384 | foreach ($this->documentArray as $document) { |
||
| 385 | if ($document !== null) { |
||
| 386 | $this->requestData['docPage'][$i] = MathUtility::forceIntegerInRange((int) $this->requestData['docPage'][$i], 1, $document->numPages, 1); |
||
| 387 | $i++; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | } else { |
||
| 391 | $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getCurrentDocument()->numPages, 1); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | // reassign viewData to get correct page |
||
| 395 | $this->viewData['requestData'] = $this->requestData; |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Wrapper for ActionController::processRequest in order to initialize things |
||
| 400 | * without using a constructor. |
||
| 401 | * |
||
| 402 | * @access public |
||
| 403 | * |
||
| 404 | * @param RequestInterface $request the request |
||
| 405 | * |
||
| 406 | * @return ResponseInterface the response |
||
| 407 | */ |
||
| 408 | public function processRequest(RequestInterface $request): ResponseInterface |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * build simple pagination |
||
| 416 | * |
||
| 417 | * @param PaginationInterface $pagination |
||
| 418 | * @param PaginatorInterface $paginator |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | //TODO: clean this function |
||
| 422 | protected function buildSimplePagination(PaginationInterface $pagination, PaginatorInterface $paginator): array |
||
| 532 | ]; |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Get document from repository by uid. |
||
| 537 | * |
||
| 538 | * @access private |
||
| 539 | * |
||
| 540 | * @param int $documentId The document's UID |
||
| 541 | * |
||
| 542 | * @return AbstractDocument |
||
| 543 | */ |
||
| 544 | private function getDocumentByUid(int $documentId) |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Get document by URL. |
||
| 563 | * |
||
| 564 | * @access protected |
||
| 565 | * |
||
| 566 | * @param string $documentId The document's URL |
||
| 567 | * |
||
| 568 | * @return AbstractDocument |
||
| 569 | */ |
||
| 570 | protected function getDocumentByUrl(string $documentId) |
||
| 623 |