We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 89 |
| Total Lines | 481 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ToolboxController 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 ToolboxController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class ToolboxController extends AbstractController |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * The main method of the plugin |
||
| 23 | * |
||
| 24 | * @return void |
||
| 25 | */ |
||
| 26 | public function mainAction() |
||
| 27 | { |
||
| 28 | $this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf'); |
||
| 29 | |||
| 30 | // Load current document. |
||
| 31 | $this->loadDocument($this->requestData); |
||
| 32 | |||
| 33 | $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0); |
||
| 34 | $this->view->assign('double', $this->requestData['double']); |
||
| 35 | |||
| 36 | $tools = explode(',', $this->settings['tools']); |
||
| 37 | // Add the tools to the toolbox. |
||
| 38 | foreach ($tools as $tool) { |
||
| 39 | $tool = trim(str_replace('tx_dlf_', '', $tool)); |
||
| 40 | $this->$tool(); |
||
| 41 | $this->view->assign($tool, true); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Renders the annotation tool |
||
| 47 | * |
||
| 48 | * @return void |
||
| 49 | */ |
||
| 50 | public function annotationtool() |
||
| 51 | { |
||
| 52 | if ( |
||
| 53 | $this->document === null |
||
| 54 | || $this->document->getDoc()->numPages < 1 |
||
| 55 | ) { |
||
| 56 | // Quit without doing anything if required variables are not set. |
||
| 57 | return; |
||
| 58 | } else { |
||
| 59 | if (!empty($this->requestData['logicalPage'])) { |
||
| 60 | $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']); |
||
| 61 | // The logical page parameter should not appear again |
||
| 62 | unset($this->requestData['logicalPage']); |
||
| 63 | } |
||
| 64 | // Set default values if not set. |
||
| 65 | // $this->requestData['page'] may be integer or string (physical structure @ID) |
||
| 66 | if ( |
||
| 67 | (int) $this->requestData['page'] > 0 |
||
| 68 | || empty($this->requestData['page']) |
||
| 69 | ) { |
||
| 70 | $this->requestData['page'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1); |
||
| 71 | } else { |
||
| 72 | $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | $annotationContainers = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['annotationContainers']; |
||
| 77 | if ( |
||
| 78 | $annotationContainers != null |
||
| 79 | && sizeof($annotationContainers) > 0 |
||
| 80 | ) { |
||
| 81 | $this->view->assign('annotationTool', true); |
||
| 82 | } else { |
||
| 83 | $this->view->assign('annotationTool', false); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Renders the fulltext download tool |
||
| 89 | * |
||
| 90 | * @return void |
||
| 91 | */ |
||
| 92 | public function fulltextdownloadtool() |
||
| 93 | { |
||
| 94 | if ( |
||
| 95 | $this->document === null |
||
| 96 | || $this->document->getDoc()->numPages < 1 |
||
| 97 | || empty($this->extConf['fileGrpFulltext']) |
||
| 98 | ) { |
||
| 99 | // Quit without doing anything if required variables are not set. |
||
| 100 | return; |
||
| 101 | } else { |
||
| 102 | if (!empty($this->requestData['logicalPage'])) { |
||
| 103 | $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']); |
||
| 104 | // The logical page parameter should not appear again |
||
| 105 | unset($this->requestData['logicalPage']); |
||
| 106 | } |
||
| 107 | // Set default values if not set. |
||
| 108 | // $this->requestData['page'] may be integer or string (physical structure @ID) |
||
| 109 | if ( |
||
| 110 | (int) $this->requestData['page'] > 0 |
||
| 111 | || empty($this->requestData['page']) |
||
| 112 | ) { |
||
| 113 | $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1); |
||
| 114 | } else { |
||
| 115 | $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | // Get text download. |
||
| 119 | $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']); |
||
| 120 | while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) { |
||
| 121 | if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext])) { |
||
| 122 | $fullTextFile = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext]; |
||
| 123 | break; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | if (!empty($fullTextFile)) { |
||
| 127 | $this->view->assign('fulltextDownload', true); |
||
| 128 | } else { |
||
| 129 | $this->view->assign('fulltextDownload', false); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Renders the fulltext tool |
||
| 135 | * |
||
| 136 | * @return void |
||
| 137 | */ |
||
| 138 | public function fulltexttool() |
||
| 139 | { |
||
| 140 | if ( |
||
| 141 | $this->document === null |
||
| 142 | || $this->document->getDoc()->numPages < 1 |
||
| 143 | || empty($this->extConf['fileGrpFulltext']) |
||
| 144 | ) { |
||
| 145 | // Quit without doing anything if required variables are not set. |
||
| 146 | return; |
||
| 147 | } else { |
||
| 148 | if (!empty($this->requestData['logicalPage'])) { |
||
| 149 | $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']); |
||
| 150 | // The logical page parameter should not appear again |
||
| 151 | unset($this->requestData['logicalPage']); |
||
| 152 | } |
||
| 153 | // Set default values if not set. |
||
| 154 | // $this->requestData['page'] may be integer or string (physical structure @ID) |
||
| 155 | if ( |
||
| 156 | (int) $this->requestData['page'] > 0 |
||
| 157 | || empty($this->requestData['page']) |
||
| 158 | ) { |
||
| 159 | $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1); |
||
| 160 | } else { |
||
| 161 | $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']); |
||
| 165 | while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) { |
||
| 166 | if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext])) { |
||
| 167 | $fullTextFile = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext]; |
||
| 168 | break; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | if (!empty($fullTextFile)) { |
||
| 172 | $this->view->assign('fulltext', true); |
||
| 173 | $this->view->assign('activateFullTextInitially', MathUtility::forceIntegerInRange($this->settings['activateFullTextInitially'], 0, 1, 0)); |
||
| 174 | } else { |
||
| 175 | $this->view->assign('fulltext', false); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Renders the image download tool |
||
| 181 | * |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | public function imagedownloadtool() |
||
| 185 | { |
||
| 186 | if ( |
||
| 187 | $this->document === null |
||
| 188 | || $this->document->getDoc()->numPages < 1 |
||
| 189 | || empty($this->settings['fileGrpsImageDownload']) |
||
| 190 | ) { |
||
| 191 | // Quit without doing anything if required variables are not set. |
||
| 192 | return; |
||
| 193 | } else { |
||
| 194 | if (!empty($this->requestData['logicalPage'])) { |
||
| 195 | $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']); |
||
| 196 | // The logical page parameter should not appear again |
||
| 197 | unset($this->requestData['logicalPage']); |
||
| 198 | } |
||
| 199 | // Set default values if not set. |
||
| 200 | // $this->requestData['page'] may be integer or string (physical structure @ID) |
||
| 201 | if ( |
||
| 202 | (int) $this->requestData['page'] > 0 |
||
| 203 | || empty($this->requestData['page']) |
||
| 204 | ) { |
||
| 205 | $this->requestData['page'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1); |
||
| 206 | } else { |
||
| 207 | $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | $imageArray = []; |
||
| 211 | // Get left or single page download. |
||
| 212 | $imageArray[0] = $this->getImage($this->requestData['page']); |
||
| 213 | if ($this->requestData['double'] == 1) { |
||
| 214 | $imageArray[1] = $this->getImage($this->requestData['page'] + 1); |
||
| 215 | } |
||
| 216 | $this->view->assign('imageDownload', $imageArray); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get image's URL and MIME type |
||
| 221 | * |
||
| 222 | * @access protected |
||
| 223 | * |
||
| 224 | * @param int $page: Page number |
||
| 225 | * |
||
| 226 | * @return array Array of image links and image format information |
||
| 227 | */ |
||
| 228 | protected function getImage($page) |
||
| 229 | { |
||
| 230 | $image = []; |
||
| 231 | // Get @USE value of METS fileGrp. |
||
| 232 | $fileGrps = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->settings['fileGrpsImageDownload']); |
||
| 233 | while ($fileGrp = @array_pop($fileGrps)) { |
||
| 234 | // Get image link. |
||
| 235 | if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrp])) { |
||
| 236 | $image['url'] = $this->document->getDoc()->getDownloadLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrp]); |
||
| 237 | $image['mimetype'] = $this->document->getDoc()->getFileMimeType($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrp]); |
||
| 238 | switch ($image['mimetype']) { |
||
| 239 | case 'image/jpeg': |
||
| 240 | $mimetypeLabel = ' (JPG)'; |
||
| 241 | break; |
||
| 242 | case 'image/tiff': |
||
| 243 | $mimetypeLabel = ' (TIFF)'; |
||
| 244 | break; |
||
| 245 | default: |
||
| 246 | $mimetypeLabel = ''; |
||
| 247 | } |
||
| 248 | $image['mimetypeLabel'] = $mimetypeLabel; |
||
| 249 | break; |
||
| 250 | } else { |
||
| 251 | $this->logger->warning('File not found in fileGrp "' . $fileGrp . '"'); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | return $image; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Renders the image manipulation tool |
||
| 259 | * |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | public function imagemanipulationtool() |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Renders the PDF download tool |
||
| 273 | * |
||
| 274 | * @return void |
||
| 275 | */ |
||
| 276 | public function pdfdownloadtool() |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get page's download link |
||
| 311 | * |
||
| 312 | * @access protected |
||
| 313 | * |
||
| 314 | * @return array Link to downloadable page |
||
| 315 | */ |
||
| 316 | protected function getPageLink() |
||
| 317 | { |
||
| 318 | $page1Link = ''; |
||
| 319 | $page2Link = ''; |
||
| 320 | $pageLinkArray = []; |
||
| 321 | $pageNumber = $this->requestData['page']; |
||
| 322 | $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']); |
||
| 323 | // Get image link. |
||
| 324 | while ($fileGrpDownload = array_shift($fileGrpsDownload)) { |
||
| 325 | if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber]]['files'][$fileGrpDownload])) { |
||
| 326 | $page1Link = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber]]['files'][$fileGrpDownload]); |
||
| 327 | // Get second page, too, if double page view is activated. |
||
| 328 | if ( |
||
| 329 | $this->requestData['double'] |
||
| 330 | && $pageNumber < $this->document->getDoc()->numPages |
||
| 331 | && !empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload]) |
||
| 332 | ) { |
||
| 333 | $page2Link = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload]); |
||
| 334 | } |
||
| 335 | break; |
||
| 336 | } |
||
| 337 | } |
||
| 338 | if ( |
||
| 339 | empty($page1Link) |
||
| 340 | && empty($page2Link) |
||
| 341 | ) { |
||
| 342 | $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"'); |
||
| 343 | } |
||
| 344 | |||
| 345 | if (!empty($page1Link)) { |
||
| 346 | $pageLinkArray[0] = $page1Link; |
||
| 347 | } |
||
| 348 | if (!empty($page2Link)) { |
||
| 349 | $pageLinkArray[1] = $page2Link; |
||
| 350 | } |
||
| 351 | return $pageLinkArray; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get work's download link |
||
| 356 | * |
||
| 357 | * @access protected |
||
| 358 | * |
||
| 359 | * @return string Link to downloadable work |
||
| 360 | */ |
||
| 361 | protected function getWorkLink() |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Renders the searchInDocument tool |
||
| 388 | * |
||
| 389 | * @return void |
||
| 390 | */ |
||
| 391 | public function searchindocumenttool() |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Get current document id. As default the uid will be used. |
||
| 448 | * In case there is defined documentIdUrlSchema then the id will |
||
| 449 | * extracted from this URL. |
||
| 450 | * |
||
| 451 | * @access protected |
||
| 452 | * |
||
| 453 | * @return string with current document id |
||
| 454 | */ |
||
| 455 | protected function getCurrentDocumentId() |
||
| 456 | { |
||
| 457 | $id = $this->document->getUid(); |
||
| 458 | |||
| 459 | if ($id !== null && $id > 0) { |
||
| 460 | // we found the document uid |
||
| 461 | return (string) $id; |
||
| 462 | } else { |
||
| 463 | $id = $this->requestData['id']; |
||
| 464 | if (!GeneralUtility::isValidUrl($id)) { |
||
| 465 | // we found no valid URI --> something unexpected we cannot search within. |
||
| 466 | return ''; |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | // example: https://host.de/items/*id*/record |
||
| 471 | if (!empty($this->settings['documentIdUrlSchema'])) { |
||
| 472 | $arr = explode('*', $this->settings['documentIdUrlSchema']); |
||
| 473 | |||
| 474 | if (count($arr) == 2) { |
||
| 475 | $id = explode($arr[0], $id)[0]; |
||
| 476 | } else if (count($arr) == 3) { |
||
| 477 | $sub = substr($id, strpos($id, $arr[0]) + strlen($arr[0]), strlen($id)); |
||
| 478 | $id = substr($sub, 0, strpos($sub, $arr[2])); |
||
| 479 | } |
||
| 480 | } |
||
| 481 | return $id; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get the encrypted Solr core name |
||
| 486 | * |
||
| 487 | * @access protected |
||
| 488 | * |
||
| 489 | * @return string with encrypted core name |
||
| 490 | */ |
||
| 491 | protected function getEncryptedCoreName() |
||
| 500 | } |
||
| 501 | } |
||
| 502 |