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 | 460 |
| Duplicated Lines | 0 % |
| Changes | 12 | ||
| Bugs | 1 | 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 |
||
| 26 | class ToolboxController extends AbstractController |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * This holds the current document |
||
| 31 | * |
||
| 32 | * @var \Kitodo\Dlf\Common\Doc |
||
| 33 | * @access private |
||
| 34 | */ |
||
| 35 | private $doc; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The main method of the plugin |
||
| 39 | * |
||
| 40 | * @return void |
||
| 41 | */ |
||
| 42 | public function mainAction() |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Renders tool in the toolbox. |
||
| 58 | * |
||
| 59 | * @access private |
||
| 60 | * |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | private function renderTool() { |
||
| 64 | if (!empty($this->settings['tool'])) { |
||
| 65 | switch ($this->settings['tool']) { |
||
| 66 | case 'tx_dlf_annotationtool': |
||
| 67 | case 'annotationtool': |
||
| 68 | $this->renderToolByName('renderAnnotationTool'); |
||
| 69 | break; |
||
| 70 | case 'tx_dlf_fulltextdownloadtool': |
||
| 71 | case 'fulltextdownloadtool': |
||
| 72 | $this->renderToolByName('renderFulltextDownloadTool'); |
||
| 73 | break; |
||
| 74 | case 'tx_dlf_fulltexttool': |
||
| 75 | case 'fulltexttool': |
||
| 76 | $this->renderToolByName('renderFulltextTool'); |
||
| 77 | break; |
||
| 78 | case 'tx_dlf_imagedownloadtool': |
||
| 79 | case 'imagedownloadtool': |
||
| 80 | $this->renderToolByName('renderImageDownloadTool'); |
||
| 81 | break; |
||
| 82 | case 'tx_dlf_imagemanipulationtool': |
||
| 83 | case 'imagemanipulationtool': |
||
| 84 | $this->renderToolByName('renderImageManipulationTool'); |
||
| 85 | break; |
||
| 86 | case 'tx_dlf_pdfdownloadtool': |
||
| 87 | case 'pdfdownloadtool': |
||
| 88 | $this->renderToolByName('renderPdfDownloadTool'); |
||
| 89 | break; |
||
| 90 | case 'tx_dlf_searchindocumenttool': |
||
| 91 | case 'searchindocumenttool': |
||
| 92 | $this->renderToolByName('renderSearchInDocumentTool'); |
||
| 93 | break; |
||
| 94 | default: |
||
| 95 | $this->logger->warning('Incorrect tool configuration: "' . $this->settings['tool'] . '". This tool does not exist.'); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Renders tool by the name in the toolbox. |
||
| 102 | * |
||
| 103 | * @access private |
||
| 104 | * |
||
| 105 | * @return void |
||
| 106 | */ |
||
| 107 | private function renderToolByName(string $tool) { |
||
| 108 | $this->$tool(); |
||
| 109 | $this->view->assign($tool, true); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Renders the annotation tool |
||
| 114 | * |
||
| 115 | * @access private |
||
| 116 | * |
||
| 117 | * @return void |
||
| 118 | */ |
||
| 119 | private function renderAnnotationTool() |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Renders the fulltext download tool |
||
| 141 | * |
||
| 142 | * @access private |
||
| 143 | * |
||
| 144 | * @return void |
||
| 145 | */ |
||
| 146 | private function renderFulltextDownloadTool() |
||
| 147 | { |
||
| 148 | if ( |
||
| 149 | $this->isDocMissingOrEmpty() |
||
| 150 | || empty($this->extConf['fileGrpFulltext']) |
||
| 151 | ) { |
||
| 152 | // Quit without doing anything if required variables are not set. |
||
| 153 | return ''; |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->setPage(); |
||
| 157 | |||
| 158 | // Get text download. |
||
| 159 | $this->view->assign('fulltextDownload', !$this->isFullTextEmpty()); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Renders the fulltext tool |
||
| 164 | * |
||
| 165 | * @access private |
||
| 166 | * |
||
| 167 | * @return void |
||
| 168 | */ |
||
| 169 | private function renderFulltextTool() |
||
| 170 | { |
||
| 171 | if ( |
||
| 172 | $this->isDocMissingOrEmpty() |
||
| 173 | || empty($this->extConf['fileGrpFulltext']) |
||
| 174 | ) { |
||
| 175 | // Quit without doing anything if required variables are not set. |
||
| 176 | return ''; |
||
| 177 | } |
||
| 178 | |||
| 179 | $this->setPage(); |
||
| 180 | |||
| 181 | if (!$this->isFullTextEmpty()) { |
||
| 182 | $this->view->assign('fulltext', true); |
||
| 183 | $this->view->assign('activateFullTextInitially', MathUtility::forceIntegerInRange($this->settings['activateFullTextInitially'], 0, 1, 0)); |
||
| 184 | } else { |
||
| 185 | $this->view->assign('fulltext', false); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Renders the image download tool |
||
| 191 | * |
||
| 192 | * @access private |
||
| 193 | * |
||
| 194 | * @return void |
||
| 195 | */ |
||
| 196 | private function renderImageDownloadTool() |
||
| 197 | { |
||
| 198 | if ( |
||
| 199 | $this->isDocMissingOrEmpty() |
||
| 200 | || empty($this->settings['fileGrpsImageDownload']) |
||
| 201 | ) { |
||
| 202 | // Quit without doing anything if required variables are not set. |
||
| 203 | return ''; |
||
| 204 | } |
||
| 205 | |||
| 206 | $this->setPage(); |
||
| 207 | |||
| 208 | $imageArray = []; |
||
| 209 | // Get left or single page download. |
||
| 210 | $imageArray[0] = $this->getImage($this->requestData['page']); |
||
| 211 | if ($this->requestData['double'] == 1) { |
||
| 212 | $imageArray[1] = $this->getImage($this->requestData['page'] + 1); |
||
| 213 | } |
||
| 214 | $this->view->assign('imageDownload', $imageArray); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Get image's URL and MIME type |
||
| 219 | * |
||
| 220 | * @access private |
||
| 221 | * |
||
| 222 | * @param int $page: Page number |
||
| 223 | * |
||
| 224 | * @return array Array of image links and image format information |
||
| 225 | */ |
||
| 226 | private function getImage($page) |
||
| 227 | { |
||
| 228 | $image = []; |
||
| 229 | // Get @USE value of METS fileGrp. |
||
| 230 | $fileGrps = GeneralUtility::trimExplode(',', $this->settings['fileGrpsImageDownload']); |
||
| 231 | while ($fileGrp = @array_pop($fileGrps)) { |
||
| 232 | // Get image link. |
||
| 233 | $fileGroup = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrp]; |
||
| 234 | if (!empty($fileGroup)) { |
||
| 235 | $image['url'] = $this->doc->getDownloadLocation($fileGroup); |
||
| 236 | $image['mimetype'] = $this->doc->getFileMimeType($fileGroup); |
||
| 237 | switch ($image['mimetype']) { |
||
| 238 | case 'image/jpeg': |
||
| 239 | $image['mimetypeLabel'] = ' (JPG)'; |
||
| 240 | break; |
||
| 241 | case 'image/tiff': |
||
| 242 | $image['mimetypeLabel'] = ' (TIFF)'; |
||
| 243 | break; |
||
| 244 | default: |
||
| 245 | $image['mimetypeLabel'] = ''; |
||
| 246 | } |
||
| 247 | break; |
||
| 248 | } else { |
||
| 249 | $this->logger->warning('File not found in fileGrp "' . $fileGrp . '"'); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | return $image; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Renders the image manipulation tool |
||
| 257 | * |
||
| 258 | * @access private |
||
| 259 | * |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | private function renderImageManipulationTool() |
||
| 263 | { |
||
| 264 | // Set parent element for initialization. |
||
| 265 | $parentContainer = !empty($this->settings['parentContainer']) ? $this->settings['parentContainer'] : '.tx-dlf-imagemanipulationtool'; |
||
| 266 | |||
| 267 | $this->view->assign('imageManipulation', true); |
||
| 268 | $this->view->assign('parentContainer', $parentContainer); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Renders the PDF download tool |
||
| 273 | * |
||
| 274 | * @access private |
||
| 275 | * |
||
| 276 | * @return void |
||
| 277 | */ |
||
| 278 | private function renderPdfDownloadTool() |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get page's download link |
||
| 298 | * |
||
| 299 | * @access private |
||
| 300 | * |
||
| 301 | * @return array Link to downloadable page |
||
| 302 | */ |
||
| 303 | private function getPageLink() |
||
| 304 | { |
||
| 305 | $firstPageLink = ''; |
||
| 306 | $secondPageLink = ''; |
||
| 307 | $pageLinkArray = []; |
||
| 308 | $pageNumber = $this->requestData['page']; |
||
| 309 | $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']); |
||
| 310 | // Get image link. |
||
| 311 | while ($fileGrpDownload = array_shift($fileGrpsDownload)) { |
||
| 312 | $firstFileGroupDownload = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber]]['files'][$fileGrpDownload]; |
||
| 313 | if (!empty($firstFileGroupDownload)) { |
||
| 314 | $firstPageLink = $this->doc->getFileLocation($firstFileGroupDownload); |
||
| 315 | // Get second page, too, if double page view is activated. |
||
| 316 | $secondFileGroupDownload = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload]; |
||
| 317 | if ( |
||
| 318 | $this->requestData['double'] |
||
| 319 | && $pageNumber < $this->doc->numPages |
||
| 320 | && !empty($secondFileGroupDownload) |
||
| 321 | ) { |
||
| 322 | $secondPageLink = $this->doc->getFileLocation($secondFileGroupDownload); |
||
| 323 | } |
||
| 324 | break; |
||
| 325 | } |
||
| 326 | } |
||
| 327 | if ( |
||
| 328 | empty($firstPageLink) |
||
| 329 | && empty($secondPageLink) |
||
| 330 | ) { |
||
| 331 | $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"'); |
||
| 332 | } |
||
| 333 | |||
| 334 | if (!empty($firstPageLink)) { |
||
| 335 | $pageLinkArray[0] = $firstPageLink; |
||
| 336 | } |
||
| 337 | if (!empty($secondPageLink)) { |
||
| 338 | $pageLinkArray[1] = $secondPageLink; |
||
| 339 | } |
||
| 340 | return $pageLinkArray; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get work's download link |
||
| 345 | * |
||
| 346 | * @access private |
||
| 347 | * |
||
| 348 | * @return string Link to downloadable work |
||
| 349 | */ |
||
| 350 | private function getWorkLink() |
||
| 351 | { |
||
| 352 | $workLink = ''; |
||
| 353 | $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']); |
||
| 354 | // Get work link. |
||
| 355 | while ($fileGrpDownload = array_shift($fileGrpsDownload)) { |
||
| 356 | $fileGroupDownload = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[0]]['files'][$fileGrpDownload]; |
||
| 357 | if (!empty($fileGroupDownload)) { |
||
| 358 | $workLink = $this->doc->getFileLocation($fileGroupDownload); |
||
| 359 | break; |
||
| 360 | } else { |
||
| 361 | $details = $this->doc->getLogicalStructure($this->doc->toplevelId); |
||
| 362 | if (!empty($details['files'][$fileGrpDownload])) { |
||
| 363 | $workLink = $this->doc->getFileLocation($details['files'][$fileGrpDownload]); |
||
| 364 | break; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | } |
||
| 368 | if (empty($workLink)) { |
||
| 369 | $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"'); |
||
| 370 | } |
||
| 371 | return $workLink; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Renders the searchInDocument tool |
||
| 376 | * |
||
| 377 | * @access private |
||
| 378 | * |
||
| 379 | * @return void |
||
| 380 | */ |
||
| 381 | private function renderSearchInDocumentTool() |
||
| 382 | { |
||
| 383 | if ( |
||
| 384 | $this->isDocMissingOrEmpty() |
||
| 385 | || empty($this->extConf['fileGrpFulltext']) |
||
| 386 | || empty($this->settings['solrcore']) |
||
| 387 | ) { |
||
| 388 | // Quit without doing anything if required variables are not set. |
||
| 389 | return ''; |
||
| 390 | } |
||
| 391 | |||
| 392 | $this->setPage(); |
||
| 393 | |||
| 394 | // Quit if no fulltext file is present |
||
| 395 | if ($this->isFullTextEmpty()) { |
||
| 396 | return; |
||
| 397 | } |
||
| 398 | |||
| 399 | // Fill markers. |
||
| 400 | $viewArray = [ |
||
| 401 | 'LABEL_QUERY_URL' => $this->settings['queryInputName'], |
||
| 402 | 'LABEL_START' => $this->settings['startInputName'], |
||
| 403 | 'LABEL_ID' => $this->settings['idInputName'], |
||
| 404 | 'LABEL_PAGE_URL' => $this->settings['pageInputName'], |
||
| 405 | 'LABEL_HIGHLIGHT_WORD' => $this->settings['highlightWordInputName'], |
||
| 406 | 'LABEL_ENCRYPTED' => $this->settings['encryptedInputName'], |
||
| 407 | 'CURRENT_DOCUMENT' => $this->getCurrentDocumentId(), |
||
| 408 | 'SOLR_ENCRYPTED' => $this->getEncryptedCoreName() ? : '' |
||
| 409 | ]; |
||
| 410 | |||
| 411 | $this->view->assign('searchInDocument', $viewArray); |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get current document id. As default the uid will be used. |
||
| 416 | * In case there is defined documentIdUrlSchema then the id will |
||
| 417 | * extracted from this URL. |
||
| 418 | * |
||
| 419 | * @access private |
||
| 420 | * |
||
| 421 | * @return string with current document id |
||
| 422 | */ |
||
| 423 | private function getCurrentDocumentId() |
||
| 424 | { |
||
| 425 | $id = $this->document->getUid(); |
||
| 426 | |||
| 427 | if ($id !== null && $id > 0) { |
||
| 428 | // we found the document uid |
||
| 429 | return (string) $id; |
||
| 430 | } else { |
||
| 431 | $id = $this->requestData['id']; |
||
| 432 | if (!GeneralUtility::isValidUrl($id)) { |
||
| 433 | // we found no valid URI --> something unexpected we cannot search within. |
||
| 434 | return ''; |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | // example: https://host.de/items/*id*/record |
||
| 439 | if (!empty($this->settings['documentIdUrlSchema'])) { |
||
| 440 | $arr = explode('*', $this->settings['documentIdUrlSchema']); |
||
| 441 | |||
| 442 | if (count($arr) == 2) { |
||
| 443 | $id = explode($arr[0], $id)[0]; |
||
| 444 | } else if (count($arr) == 3) { |
||
| 445 | $sub = substr($id, strpos($id, $arr[0]) + strlen($arr[0]), strlen($id)); |
||
| 446 | $id = substr($sub, 0, strpos($sub, $arr[2])); |
||
| 447 | } |
||
| 448 | } |
||
| 449 | return $id; |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Get the encrypted Solr core name |
||
| 454 | * |
||
| 455 | * @access private |
||
| 456 | * |
||
| 457 | * @return string with encrypted core name |
||
| 458 | */ |
||
| 459 | private function getEncryptedCoreName() |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Check if the full text is empty. |
||
| 472 | * |
||
| 473 | * @access private |
||
| 474 | * |
||
| 475 | * @return bool true if empty, false otherwise |
||
| 476 | */ |
||
| 477 | private function isFullTextEmpty() { |
||
| 486 | } |
||
| 487 | } |
||
| 488 |