We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 40 |
| Total Lines | 225 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | 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 |
||
| 26 | class TableOfContentsController extends AbstractController |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * This holds the active entries according to the currently selected page |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | * @access protected |
||
| 33 | */ |
||
| 34 | protected $activeEntries = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $pluginConf; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * SearchController constructor. |
||
| 43 | */ |
||
| 44 | public function __construct() |
||
| 45 | { |
||
| 46 | parent::__construct(); |
||
| 47 | // Read plugin TS configuration. |
||
| 48 | // TODO: This is more or less obsolete - the table of document plugin does not use the TypoScript anymore |
||
| 49 | $this->pluginConf = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_dlf_tableofcontents.']; |
||
| 50 | $this->initialize(); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * This builds an array for one menu entry |
||
| 55 | * |
||
| 56 | * @access protected |
||
| 57 | * |
||
| 58 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 59 | * @param bool $recursive : Whether to include the child entries |
||
| 60 | * |
||
| 61 | * @return array HMENU array for menu entry |
||
| 62 | */ |
||
| 63 | protected function getMenuEntry(array $entry, $recursive = false) |
||
| 64 | { |
||
| 65 | $entryArray = []; |
||
| 66 | // Set "title", "volume", "type" and "pagination" from $entry array. |
||
| 67 | $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel']; |
||
| 68 | $entryArray['volume'] = $entry['volume']; |
||
| 69 | $entryArray['orderlabel'] = $entry['orderlabel']; |
||
| 70 | $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']); |
||
| 71 | $entryArray['pagination'] = htmlspecialchars($entry['pagination']); |
||
| 72 | $entryArray['_OVERRIDE_HREF'] = ''; |
||
| 73 | $entryArray['doNotLinkIt'] = 1; |
||
| 74 | $entryArray['ITEM_STATE'] = 'NO'; |
||
| 75 | // Build menu links based on the $entry['points'] array. |
||
| 76 | if ( |
||
| 77 | !empty($entry['points']) |
||
| 78 | && MathUtility::canBeInterpretedAsInteger($entry['points']) |
||
| 79 | ) { |
||
| 80 | $entryArray['page'] = $entry['points']; |
||
| 81 | |||
| 82 | $entryArray['doNotLinkIt'] = 0; |
||
| 83 | if ($this->settings['basketButton']) { |
||
| 84 | $entryArray['basketButton'] = [ |
||
| 85 | 'logId' => $entry['id'], |
||
| 86 | 'startpage' => $entry['points'] |
||
| 87 | ]; |
||
| 88 | } |
||
| 89 | } elseif ( |
||
| 90 | !empty($entry['points']) |
||
| 91 | && is_string($entry['points']) |
||
| 92 | ) { |
||
| 93 | $entryArray['id'] = $entry['points']; |
||
| 94 | $entryArray['page'] = 1; |
||
| 95 | $entryArray['doNotLinkIt'] = 0; |
||
| 96 | if ($this->settings['basketButton']) { |
||
| 97 | $entryArray['basketButton'] = [ |
||
| 98 | 'logId' => $entry['id'], |
||
| 99 | 'startpage' => $entry['points'] |
||
| 100 | ]; |
||
| 101 | } |
||
| 102 | } elseif (!empty($entry['targetUid'])) { |
||
| 103 | $entryArray['id'] = $entry['targetUid']; |
||
| 104 | $entryArray['page'] = 1; |
||
| 105 | $entryArray['doNotLinkIt'] = 0; |
||
| 106 | if ($this->settings['basketButton']) { |
||
| 107 | $entryArray['basketButton'] = [ |
||
| 108 | 'logId' => $entry['id'], |
||
| 109 | 'startpage' => $entry['targetUid'] |
||
| 110 | ]; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | // Set "ITEM_STATE" to "CUR" if this entry points to current page. |
||
| 114 | if (in_array($entry['id'], $this->activeEntries)) { |
||
| 115 | $entryArray['ITEM_STATE'] = 'CUR'; |
||
| 116 | } |
||
| 117 | // Build sub-menu if available and called recursively. |
||
| 118 | if ( |
||
| 119 | $recursive === true |
||
| 120 | && !empty($entry['children']) |
||
| 121 | ) { |
||
| 122 | // Build sub-menu only if one of the following conditions apply: |
||
| 123 | // 1. "expAll" is set for menu |
||
| 124 | // 2. Current menu node is in rootline |
||
| 125 | // 3. Current menu node points to another file |
||
| 126 | // 4. Current menu node has no corresponding images |
||
| 127 | if ( |
||
| 128 | !empty($this->pluginConf['menuConf.']['expAll']) |
||
| 129 | || $entryArray['ITEM_STATE'] == 'CUR' |
||
| 130 | || is_string($entry['points']) |
||
| 131 | || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']]) |
||
| 132 | ) { |
||
| 133 | $entryArray['_SUB_MENU'] = []; |
||
| 134 | foreach ($entry['children'] as $child) { |
||
| 135 | // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page. |
||
| 136 | if (in_array($child['id'], $this->activeEntries)) { |
||
| 137 | $entryArray['ITEM_STATE'] = 'ACT'; |
||
| 138 | } |
||
| 139 | $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries. |
||
| 143 | $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB'); |
||
| 144 | } |
||
| 145 | return $entryArray; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * The main method of the plugin |
||
| 150 | * |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | public function mainAction() |
||
| 154 | { |
||
| 155 | // Check for typoscript configuration to prevent fatal error. |
||
| 156 | if (empty($this->settings['menuConf'])) { |
||
| 157 | $this->logger->warning('Incomplete plugin configuration'); |
||
| 158 | } |
||
| 159 | |||
| 160 | $this->view->assign('toc', $this->makeMenuArray()); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * This builds a menu array for HMENU |
||
| 165 | * |
||
| 166 | * @access public |
||
| 167 | * @return array HMENU array |
||
| 168 | */ |
||
| 169 | public function makeMenuArray() |
||
| 251 | } |
||
| 252 | } |
||
| 253 |