We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 103 |
| Total Lines | 547 |
| Duplicated Lines | 0 % |
| Changes | 13 | ||
| Bugs | 1 | Features | 0 |
Complex classes like TableOfContentsController 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 TableOfContentsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class TableOfContentsController extends AbstractController |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * This holds the active entries according to the currently selected page |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | * @access protected |
||
| 34 | */ |
||
| 35 | protected $activeEntries = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * This holds the list of authors for autocomplete |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | * @access protected |
||
| 42 | */ |
||
| 43 | protected $authors = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * This holds the list of titles for autocomplete |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | * @access protected |
||
| 50 | */ |
||
| 51 | protected $titles = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The main method of the plugin |
||
| 55 | * |
||
| 56 | * @return void |
||
| 57 | */ |
||
| 58 | public function mainAction() |
||
| 59 | { |
||
| 60 | // Load current document. |
||
| 61 | $this->loadDocument($this->requestData); |
||
| 62 | if ( |
||
| 63 | $this->document === null |
||
| 64 | || $this->document->getDoc() === null |
||
| 65 | ) { |
||
| 66 | // Quit without doing anything if required variables are not set. |
||
| 67 | return; |
||
| 68 | } else { |
||
| 69 | if (!empty($this->requestData['logicalPage'])) { |
||
| 70 | $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']); |
||
| 71 | // The logical page parameter should not appear again |
||
| 72 | unset($this->requestData['logicalPage']); |
||
| 73 | } |
||
| 74 | if ($this->document->getDoc()->tableOfContents[0]['type'] == 'collection') { |
||
| 75 | $this->view->assign('currentList', $this->requestData['id']); |
||
| 76 | if (isset($this->requestData['transform'])) { |
||
| 77 | $this->view->assign('transform', $this->requestData['transform']); |
||
| 78 | } else { |
||
| 79 | $this->view->assign('transform', 'something'); |
||
| 80 | } |
||
| 81 | $this->view->assign('type', 'collection'); |
||
| 82 | $this->view->assign('types', $this->getTypes($this->document->getDoc()->tableOfContents)); |
||
| 83 | $this->view->assign('toc', $this->makeMenuFor3DObjects()); |
||
| 84 | $this->sortAuthors(); |
||
| 85 | $this->view->assign('authors', $this->authors); |
||
| 86 | natcasesort($this->titles); |
||
| 87 | $this->view->assign('titles', $this->titles); |
||
| 88 | $this->view->assign('softwares', $this->getSoftwares($this->document->getDoc()->tableOfContents)); |
||
| 89 | } else { |
||
| 90 | $this->view->assign('type', 'other'); |
||
| 91 | $this->view->assign('toc', $this->makeMenuArray()); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * This builds a menu array for HMENU |
||
| 98 | * |
||
| 99 | * @access protected |
||
| 100 | * @return array HMENU array |
||
| 101 | */ |
||
| 102 | protected function makeMenuArray() |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * This builds a menu for list of 3D objects |
||
| 171 | * |
||
| 172 | * @access protected |
||
| 173 | * |
||
| 174 | * @param string $content: The PlugIn content |
||
| 175 | * @param array $conf: The PlugIn configuration |
||
| 176 | * |
||
| 177 | * @return array HMENU array |
||
| 178 | */ |
||
| 179 | protected function makeMenuFor3DObjects() |
||
| 180 | { |
||
| 181 | $menuArray = []; |
||
| 182 | |||
| 183 | // Go through table of contents and create all menu entries. |
||
| 184 | foreach ($this->document->getDoc()->tableOfContents as $entry) { |
||
| 185 | $menuArray[] = $this->getMenuEntryWithImage($entry, true); |
||
| 186 | } |
||
| 187 | return $menuArray; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * This builds an array for one menu entry |
||
| 192 | * |
||
| 193 | * @access protected |
||
| 194 | * |
||
| 195 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 196 | * @param bool $recursive : Whether to include the child entries |
||
| 197 | * |
||
| 198 | * @return array HMENU array for menu entry |
||
| 199 | */ |
||
| 200 | protected function getMenuEntry(array $entry, $recursive = false) |
||
| 201 | { |
||
| 202 | $entry = $this->resolveMenuEntry($entry); |
||
| 203 | |||
| 204 | $entryArray = []; |
||
| 205 | // Set "title", "volume", "type" and "pagination" from $entry array. |
||
| 206 | $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel']; |
||
| 207 | $entryArray['volume'] = $entry['volume']; |
||
| 208 | $entryArray['orderlabel'] = $entry['orderlabel']; |
||
| 209 | $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']); |
||
| 210 | $entryArray['pagination'] = htmlspecialchars($entry['pagination']); |
||
| 211 | $entryArray['_OVERRIDE_HREF'] = ''; |
||
| 212 | $entryArray['doNotLinkIt'] = 1; |
||
| 213 | $entryArray['ITEM_STATE'] = 'NO'; |
||
| 214 | // Build menu links based on the $entry['points'] array. |
||
| 215 | if ( |
||
| 216 | !empty($entry['points']) |
||
| 217 | && MathUtility::canBeInterpretedAsInteger($entry['points']) |
||
| 218 | ) { |
||
| 219 | $entryArray['page'] = $entry['points']; |
||
| 220 | |||
| 221 | $entryArray['doNotLinkIt'] = 0; |
||
| 222 | if ($this->settings['basketButton']) { |
||
| 223 | $entryArray['basketButton'] = [ |
||
| 224 | 'logId' => $entry['id'], |
||
| 225 | 'startpage' => $entry['points'] |
||
| 226 | ]; |
||
| 227 | } |
||
| 228 | } elseif ( |
||
| 229 | !empty($entry['points']) |
||
| 230 | && is_string($entry['points']) |
||
| 231 | ) { |
||
| 232 | $entryArray['id'] = $entry['points']; |
||
| 233 | $entryArray['page'] = 1; |
||
| 234 | $entryArray['doNotLinkIt'] = 0; |
||
| 235 | if ($this->settings['basketButton']) { |
||
| 236 | $entryArray['basketButton'] = [ |
||
| 237 | 'logId' => $entry['id'], |
||
| 238 | 'startpage' => $entry['points'] |
||
| 239 | ]; |
||
| 240 | } |
||
| 241 | } elseif (!empty($entry['targetUid'])) { |
||
| 242 | $entryArray['id'] = $entry['targetUid']; |
||
| 243 | $entryArray['page'] = 1; |
||
| 244 | $entryArray['doNotLinkIt'] = 0; |
||
| 245 | if ($this->settings['basketButton']) { |
||
| 246 | $entryArray['basketButton'] = [ |
||
| 247 | 'logId' => $entry['id'], |
||
| 248 | 'startpage' => $entry['targetUid'] |
||
| 249 | ]; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | // Set "ITEM_STATE" to "CUR" if this entry points to current page. |
||
| 253 | if (in_array($entry['id'], $this->activeEntries)) { |
||
| 254 | $entryArray['ITEM_STATE'] = 'CUR'; |
||
| 255 | } |
||
| 256 | // Build sub-menu if available and called recursively. |
||
| 257 | if ( |
||
| 258 | $recursive === true |
||
| 259 | && !empty($entry['children']) |
||
| 260 | ) { |
||
| 261 | // Build sub-menu only if one of the following conditions apply: |
||
| 262 | // 1. Current menu node is in rootline |
||
| 263 | // 2. Current menu node points to another file |
||
| 264 | // 3. Current menu node has no corresponding images |
||
| 265 | if ( |
||
| 266 | $entryArray['ITEM_STATE'] == 'CUR' |
||
| 267 | || is_string($entry['points']) |
||
| 268 | || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']]) |
||
| 269 | ) { |
||
| 270 | $entryArray['_SUB_MENU'] = []; |
||
| 271 | foreach ($entry['children'] as $child) { |
||
| 272 | // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page. |
||
| 273 | if (in_array($child['id'], $this->activeEntries)) { |
||
| 274 | $entryArray['ITEM_STATE'] = 'ACT'; |
||
| 275 | } |
||
| 276 | $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true); |
||
| 277 | } |
||
| 278 | } |
||
| 279 | // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries. |
||
| 280 | $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB'); |
||
| 281 | } |
||
| 282 | return $entryArray; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * If $entry references an external METS file (as mptr), |
||
| 287 | * try to resolve its database UID and return an updated $entry. |
||
| 288 | * |
||
| 289 | * This is so that when linking from a child document back to its parent, |
||
| 290 | * that link is via UID, so that subsequently the parent's TOC is built from database. |
||
| 291 | * |
||
| 292 | * @param array $entry |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | protected function resolveMenuEntry($entry) |
||
| 296 | { |
||
| 297 | // If the menu entry points to the parent document, |
||
| 298 | // resolve to the parent UID set on indexation. |
||
| 299 | $doc = $this->document->getDoc(); |
||
| 300 | if ( |
||
| 301 | $doc instanceof MetsDocument |
||
| 302 | && $entry['points'] === $doc->parentHref |
||
| 303 | && !empty($this->document->getPartof()) |
||
| 304 | ) { |
||
| 305 | unset($entry['points']); |
||
| 306 | $entry['targetUid'] = $this->document->getPartof(); |
||
| 307 | } |
||
| 308 | |||
| 309 | return $entry; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * This builds an array for one 3D menu entry |
||
| 314 | * |
||
| 315 | * @access protected |
||
| 316 | * |
||
| 317 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 318 | * @param bool $recursive : Whether to include the child entries |
||
| 319 | * |
||
| 320 | * @return array HMENU array for 3D menu entry |
||
| 321 | */ |
||
| 322 | protected function getMenuEntryWithImage(array $entry, $recursive = false) |
||
| 323 | { |
||
| 324 | $entryArray = []; |
||
| 325 | |||
| 326 | // don't filter if the entry type is collection |
||
| 327 | if ($entry['type'] != 'collection') { |
||
| 328 | if (!$this->isFound($entry)) { |
||
| 329 | return $entryArray; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | // Set "title", "volume", "type" and "pagination" from $entry array. |
||
| 334 | $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel']; |
||
| 335 | $entryArray['orderlabel'] = $entry['orderlabel']; |
||
| 336 | $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']); |
||
| 337 | $entryArray['pagination'] = htmlspecialchars($entry['pagination']); |
||
| 338 | $entryArray['doNotLinkIt'] = 1; |
||
| 339 | $entryArray['ITEM_STATE'] = 'HEADER'; |
||
| 340 | |||
| 341 | if ($entry['children'] === null) { |
||
| 342 | $entryArray['description'] = $entry['description']; |
||
| 343 | $id = $this->document->getDoc()->smLinks['l2p'][$entry['id']][0]; |
||
| 344 | $entryArray['image'] = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$id]['files']['THUMBS']); |
||
| 345 | $entryArray['doNotLinkIt'] = 0; |
||
| 346 | // index.php?tx_dlf%5Bid%5D=http%3A%2F%2Flink_to_METS_file.xml |
||
| 347 | $entryArray['urlId'] = GeneralUtility::_GET('id'); |
||
| 348 | $entryArray['urlXml'] = $entry['points']; |
||
| 349 | $entryArray['ITEM_STATE'] = 'ITEM'; |
||
| 350 | |||
| 351 | $this->addAuthorToAutocomplete($entryArray['author']); |
||
| 352 | $this->addTitleToAutocomplete($entryArray['title']); |
||
| 353 | } |
||
| 354 | |||
| 355 | // Build sub-menu if available and called recursively. |
||
| 356 | if ( |
||
| 357 | $recursive == true |
||
| 358 | && !empty($entry['children']) |
||
| 359 | ) { |
||
| 360 | // Build sub-menu only if one of the following conditions apply: |
||
| 361 | // 1. Current menu node points to another file |
||
| 362 | // 2. Current menu node has no corresponding images |
||
| 363 | if ( |
||
| 364 | is_string($entry['points']) |
||
| 365 | || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']]) |
||
| 366 | ) { |
||
| 367 | $entryArray['_SUB_MENU'] = []; |
||
| 368 | foreach ($entry['children'] as $child) { |
||
| 369 | $menuEntry = $this->getMenuEntryWithImage($child); |
||
| 370 | if (!empty($menuEntry)) { |
||
| 371 | $entryArray['_SUB_MENU'][] = $menuEntry; |
||
| 372 | } |
||
| 373 | } |
||
| 374 | } |
||
| 375 | } |
||
| 376 | return $entryArray; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Check or possible combinations of requested params. |
||
| 381 | * |
||
| 382 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 383 | * |
||
| 384 | * @return bool true if found, false otherwise |
||
| 385 | */ |
||
| 386 | private function isFound($entry) { |
||
| 387 | if (!empty($this->requestData['title'] && !empty($this->requestData['type']) && !empty($this->requestData['author']))) { |
||
| 388 | return $this->isTitleFound($entry) && $this->isTypeFound($entry) && $this->isAuthorFound($entry); |
||
| 389 | } else if (!empty($this->requestData['title']) && !empty($this->requestData['author'])) { |
||
| 390 | return $this->isTitleFound($entry) && $this->isAuthorFound($entry); |
||
| 391 | } else if (!empty($this->requestData['title']) && !empty($this->requestData['type'])) { |
||
| 392 | return $this->isTitleFound($entry) && $this->isTypeFound($entry); |
||
| 393 | } else if (!empty($this->requestData['author']) && !empty($this->requestData['type'])) { |
||
| 394 | return $this->isAuthorFound($entry) && $this->isTypeFound($entry); |
||
| 395 | } else if (!empty($this->requestData['title'])) { |
||
| 396 | return $this->isTitleFound($entry); |
||
| 397 | } else if (!empty($this->requestData['author'])) { |
||
| 398 | return $this->isTypeFound($entry); |
||
| 399 | } else if (!empty($this->requestData['licence'])) { |
||
| 400 | return $this->isLicenceFound($entry); |
||
| 401 | } else if (!empty($this->requestData['type'])) { |
||
| 402 | return $this->isTypeFound($entry); |
||
| 403 | } else if (!empty($this->requestData['type'])) { |
||
| 404 | return $this->isSoftwareFound($entry); |
||
| 405 | } else { |
||
| 406 | // no parameters so entry is matching |
||
| 407 | return true; |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Check if author is found. |
||
| 413 | * |
||
| 414 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 415 | * |
||
| 416 | * @return bool true if found, false otherwise |
||
| 417 | */ |
||
| 418 | private function isAuthorFound($entry) { |
||
| 419 | $value = strtolower($entry['author']); |
||
| 420 | $author = strtolower($this->requestData['author']); |
||
| 421 | return str_contains($value, $author); |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Check if title is found. |
||
| 426 | * |
||
| 427 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 428 | * |
||
| 429 | * @return bool true if found, false otherwise |
||
| 430 | */ |
||
| 431 | private function isTitleFound($entry) { |
||
| 432 | $value = strtolower($entry['label']); |
||
| 433 | $title = strtolower($this->requestData['title']); |
||
| 434 | return str_contains($value, $title); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Check if type is found. |
||
| 439 | * |
||
| 440 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 441 | * |
||
| 442 | * @return bool true if found, false otherwise |
||
| 443 | */ |
||
| 444 | private function isTypeFound($entry) { |
||
| 445 | return str_contains($entry['identifier'], $this->requestData['types']); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Add author to the authors autocomplete array. |
||
| 450 | * |
||
| 451 | * @param string $author : author to be inserted to the authors autocomplete array |
||
| 452 | * |
||
| 453 | * @return void |
||
| 454 | */ |
||
| 455 | private function addAuthorToAutocomplete($author) { |
||
| 467 | } |
||
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Sort authors by surname - second part of the string |
||
| 473 | * |
||
| 474 | * @return void |
||
| 475 | */ |
||
| 476 | private function sortAuthors() { |
||
| 477 | usort($this->authors, function($firstAuthor, $secondAuthor) { |
||
| 478 | $firstAuthor = substr(strrchr($firstAuthor, ' '), 1); |
||
| 479 | $secondAuthor = substr(strrchr($secondAuthor, ' '), 1); |
||
| 480 | return strcmp($firstAuthor, $secondAuthor); |
||
| 481 | }); |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Add title to the titles autocomplete array. |
||
| 486 | * |
||
| 487 | * @param string $title : title to be inserted to the titles autocomplete array |
||
| 488 | * |
||
| 489 | * @return void |
||
| 490 | */ |
||
| 491 | private function addTitleToAutocomplete($title) { |
||
| 492 | if (!(in_array($title, $this->titles)) && $title != NULL) { |
||
| 493 | $this->titles[] = $title; |
||
| 494 | } |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Get all types. |
||
| 499 | * |
||
| 500 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 501 | * |
||
| 502 | * @return array of object types |
||
| 503 | */ |
||
| 504 | private function getTypes($entry) { |
||
| 505 | $types = []; |
||
| 506 | $index = 0; |
||
| 507 | |||
| 508 | if (!empty($entry[0]['children'])) { |
||
| 509 | foreach ($entry[0]['children'] as $child) { |
||
| 510 | $type = $this->getType($child); |
||
| 511 | if (!(in_array($type, $types)) && $type != NULL) { |
||
| 512 | $types[$index] = $type; |
||
| 513 | $index++; |
||
| 514 | } |
||
| 515 | } |
||
| 516 | } |
||
| 517 | natcasesort($types); |
||
| 518 | return $types; |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Get single type for given entry. |
||
| 523 | * |
||
| 524 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 525 | * |
||
| 526 | * @return string type name without number |
||
| 527 | */ |
||
| 528 | private function getType($entry) { |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Get all softwares. |
||
| 539 | * |
||
| 540 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 541 | * |
||
| 542 | * @return array of object softwares |
||
| 543 | */ |
||
| 544 | private function getSoftwares($entry) { |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Get single software for given entry. |
||
| 563 | * |
||
| 564 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 565 | * |
||
| 566 | * @return string software name split by commas |
||
| 567 | */ |
||
| 568 | private function getSoftware($entry) { |
||
| 574 | } |
||
| 575 | } |
||
| 576 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.