We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 69 |
| Total Lines | 428 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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 |
||
| 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() |
||
| 43 | { |
||
| 44 | // Load current document. |
||
| 45 | $this->loadDocument(); |
||
| 46 | |||
| 47 | $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0); |
||
| 48 | $this->view->assign('double', $this->requestData['double']); |
||
| 49 | |||
| 50 | if (!$this->isDocMissingOrEmpty()) { |
||
| 51 | $this->doc = $this->document->getDoc(); |
||
| 52 | } |
||
| 53 | |||
| 54 | $tools = explode(',', $this->settings['tools']); |
||
| 55 | // Add the tools to the toolbox. |
||
| 56 | foreach ($tools as $tool) { |
||
| 57 | $tool = 'render' . trim(ucfirst(str_replace('tx_dlf_', '', str_replace('tool', 'Tool', $tool)))); |
||
| 58 | $this->$tool(); |
||
| 59 | $this->view->assign($tool, true); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Renders the annotation tool |
||
| 65 | * |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | public function renderAnnotationTool() |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Renders the fulltext download tool |
||
| 90 | * |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | public function renderFulltextdownloadTool() |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Renders the fulltext tool |
||
| 122 | * |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | public function renderFulltextTool() |
||
| 126 | { |
||
| 127 | if ( |
||
| 128 | $this->isDocMissingOrEmpty() |
||
| 129 | || empty($this->extConf['fileGrpFulltext']) |
||
| 130 | ) { |
||
| 131 | // Quit without doing anything if required variables are not set. |
||
| 132 | return ''; |
||
| 133 | } |
||
| 134 | |||
| 135 | $this->setPage(); |
||
| 136 | |||
| 137 | $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']); |
||
| 138 | while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) { |
||
| 139 | if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext])) { |
||
| 140 | $fullTextFile = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext]; |
||
| 141 | break; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | if (!empty($fullTextFile)) { |
||
| 145 | $this->view->assign('fulltext', true); |
||
| 146 | $this->view->assign('activateFullTextInitially', MathUtility::forceIntegerInRange($this->settings['activateFullTextInitially'], 0, 1, 0)); |
||
| 147 | } else { |
||
| 148 | $this->view->assign('fulltext', false); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Renders the image download tool |
||
| 154 | * |
||
| 155 | * @return void |
||
| 156 | */ |
||
| 157 | public function renderImagedownloadTool() |
||
| 158 | { |
||
| 159 | if ( |
||
| 160 | $this->isDocMissingOrEmpty() |
||
| 161 | || empty($this->settings['fileGrpsImageDownload']) |
||
| 162 | ) { |
||
| 163 | // Quit without doing anything if required variables are not set. |
||
| 164 | return ''; |
||
| 165 | } |
||
| 166 | |||
| 167 | $this->setPage(); |
||
| 168 | |||
| 169 | $imageArray = []; |
||
| 170 | // Get left or single page download. |
||
| 171 | $imageArray[0] = $this->getImage($this->requestData['page']); |
||
| 172 | if ($this->requestData['double'] == 1) { |
||
| 173 | $imageArray[1] = $this->getImage($this->requestData['page'] + 1); |
||
| 174 | } |
||
| 175 | $this->view->assign('imageDownload', $imageArray); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get image's URL and MIME type |
||
| 180 | * |
||
| 181 | * @access protected |
||
| 182 | * |
||
| 183 | * @param int $page: Page number |
||
| 184 | * |
||
| 185 | * @return array Array of image links and image format information |
||
| 186 | */ |
||
| 187 | protected function getImage($page) |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Renders the image manipulation tool |
||
| 218 | * |
||
| 219 | * @return void |
||
| 220 | */ |
||
| 221 | public function renderImagemanipulationtTool() |
||
| 222 | { |
||
| 223 | // Set parent element for initialization. |
||
| 224 | $parentContainer = !empty($this->settings['parentContainer']) ? $this->settings['parentContainer'] : '.tx-dlf-imagemanipulationtool'; |
||
| 225 | |||
| 226 | $this->view->assign('imageManipulation', true); |
||
| 227 | $this->view->assign('parentContainer', $parentContainer); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Renders the PDF download tool |
||
| 232 | * |
||
| 233 | * @return void |
||
| 234 | */ |
||
| 235 | public function renderPdfdownloadTool() |
||
| 236 | { |
||
| 237 | if ( |
||
| 238 | $this->isDocMissingOrEmpty() |
||
| 239 | || empty($this->extConf['fileGrpDownload']) |
||
| 240 | ) { |
||
| 241 | // Quit without doing anything if required variables are not set. |
||
| 242 | return ''; |
||
| 243 | } |
||
| 244 | |||
| 245 | $this->setPage(); |
||
| 246 | |||
| 247 | // Get single page downloads. |
||
| 248 | $this->view->assign('pageLinks', $this->getPageLink()); |
||
| 249 | // Get work download. |
||
| 250 | $this->view->assign('workLink', $this->getWorkLink()); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get page's download link |
||
| 255 | * |
||
| 256 | * @access protected |
||
| 257 | * |
||
| 258 | * @return array Link to downloadable page |
||
| 259 | */ |
||
| 260 | protected function getPageLink() |
||
| 261 | { |
||
| 262 | $page1Link = ''; |
||
| 263 | $page2Link = ''; |
||
| 264 | $pageLinkArray = []; |
||
| 265 | $pageNumber = $this->requestData['page']; |
||
| 266 | $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']); |
||
| 267 | // Get image link. |
||
| 268 | while ($fileGrpDownload = array_shift($fileGrpsDownload)) { |
||
| 269 | if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber]]['files'][$fileGrpDownload])) { |
||
| 270 | $page1Link = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber]]['files'][$fileGrpDownload]); |
||
| 271 | // Get second page, too, if double page view is activated. |
||
| 272 | if ( |
||
| 273 | $this->requestData['double'] |
||
| 274 | && $pageNumber < $this->doc->numPages |
||
| 275 | && !empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload]) |
||
| 276 | ) { |
||
| 277 | $page2Link = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload]); |
||
| 278 | } |
||
| 279 | break; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | if ( |
||
| 283 | empty($page1Link) |
||
| 284 | && empty($page2Link) |
||
| 285 | ) { |
||
| 286 | $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"'); |
||
| 287 | } |
||
| 288 | |||
| 289 | if (!empty($page1Link)) { |
||
| 290 | $pageLinkArray[0] = $page1Link; |
||
| 291 | } |
||
| 292 | if (!empty($page2Link)) { |
||
| 293 | $pageLinkArray[1] = $page2Link; |
||
| 294 | } |
||
| 295 | return $pageLinkArray; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get work's download link |
||
| 300 | * |
||
| 301 | * @access protected |
||
| 302 | * |
||
| 303 | * @return string Link to downloadable work |
||
| 304 | */ |
||
| 305 | protected function getWorkLink() |
||
| 306 | { |
||
| 307 | $workLink = ''; |
||
| 308 | $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']); |
||
| 309 | // Get work link. |
||
| 310 | while ($fileGrpDownload = array_shift($fileGrpsDownload)) { |
||
| 311 | if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[0]]['files'][$fileGrpDownload])) { |
||
| 312 | $workLink = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[0]]['files'][$fileGrpDownload]); |
||
| 313 | break; |
||
| 314 | } else { |
||
| 315 | $details = $this->doc->getLogicalStructure($this->doc->toplevelId); |
||
| 316 | if (!empty($details['files'][$fileGrpDownload])) { |
||
| 317 | $workLink = $this->doc->getFileLocation($details['files'][$fileGrpDownload]); |
||
| 318 | break; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | } |
||
| 322 | if (!empty($workLink)) { |
||
| 323 | $workLink = $workLink; |
||
| 324 | } else { |
||
| 325 | $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"'); |
||
| 326 | } |
||
| 327 | return $workLink; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Renders the searchInDocument tool |
||
| 332 | * |
||
| 333 | * @return void |
||
| 334 | */ |
||
| 335 | public function renderSearchindocumentTool() |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get current document id. As default the uid will be used. |
||
| 377 | * In case there is defined documentIdUrlSchema then the id will |
||
| 378 | * extracted from this URL. |
||
| 379 | * |
||
| 380 | * @access protected |
||
| 381 | * |
||
| 382 | * @return string with current document id |
||
| 383 | */ |
||
| 384 | protected function getCurrentDocumentId() |
||
| 385 | { |
||
| 386 | $id = $this->document->getUid(); |
||
| 387 | |||
| 388 | if ($id !== null && $id > 0) { |
||
| 389 | // we found the document uid |
||
| 390 | return (string) $id; |
||
| 391 | } else { |
||
| 392 | $id = $this->requestData['id']; |
||
| 393 | if (!GeneralUtility::isValidUrl($id)) { |
||
| 394 | // we found no valid URI --> something unexpected we cannot search within. |
||
| 395 | return ''; |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | // example: https://host.de/items/*id*/record |
||
| 400 | if (!empty($this->settings['documentIdUrlSchema'])) { |
||
| 401 | $arr = explode('*', $this->settings['documentIdUrlSchema']); |
||
| 402 | |||
| 403 | if (count($arr) == 2) { |
||
| 404 | $id = explode($arr[0], $id)[0]; |
||
| 405 | } else if (count($arr) == 3) { |
||
| 406 | $sub = substr($id, strpos($id, $arr[0]) + strlen($arr[0]), strlen($id)); |
||
| 407 | $id = substr($sub, 0, strpos($sub, $arr[2])); |
||
| 408 | } |
||
| 409 | } |
||
| 410 | return $id; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get the encrypted Solr core name |
||
| 415 | * |
||
| 416 | * @access protected |
||
| 417 | * |
||
| 418 | * @return string with encrypted core name |
||
| 419 | */ |
||
| 420 | protected function getEncryptedCoreName() |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Sets page value. |
||
| 433 | * |
||
| 434 | * @access private |
||
| 435 | * |
||
| 436 | * @return void |
||
| 437 | */ |
||
| 438 | private function setPage() { |
||
| 454 | } |
||
| 455 | } |
||
| 456 | } |
||
| 457 |