We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 43 |
| Total Lines | 227 |
| 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 |
||
| 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 builds an array for one menu entry |
||
| 39 | * |
||
| 40 | * @access protected |
||
| 41 | * |
||
| 42 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 43 | * @param bool $recursive : Whether to include the child entries |
||
| 44 | * |
||
| 45 | * @return array HMENU array for menu entry |
||
| 46 | */ |
||
| 47 | protected function getMenuEntry(array $entry, $recursive = false) |
||
| 48 | { |
||
| 49 | $entryArray = []; |
||
| 50 | // Set "title", "volume", "type" and "pagination" from $entry array. |
||
| 51 | $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel']; |
||
| 52 | $entryArray['volume'] = $entry['volume']; |
||
| 53 | $entryArray['orderlabel'] = $entry['orderlabel']; |
||
| 54 | $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']); |
||
| 55 | $entryArray['pagination'] = htmlspecialchars($entry['pagination']); |
||
| 56 | $entryArray['_OVERRIDE_HREF'] = ''; |
||
| 57 | $entryArray['doNotLinkIt'] = 1; |
||
| 58 | $entryArray['ITEM_STATE'] = 'NO'; |
||
| 59 | // Build menu links based on the $entry['points'] array. |
||
| 60 | if ( |
||
| 61 | !empty($entry['points']) |
||
| 62 | && MathUtility::canBeInterpretedAsInteger($entry['points']) |
||
| 63 | ) { |
||
| 64 | $entryArray['page'] = $entry['points']; |
||
| 65 | |||
| 66 | $entryArray['doNotLinkIt'] = 0; |
||
| 67 | if ($this->settings['basketButton']) { |
||
| 68 | $entryArray['basketButton'] = [ |
||
| 69 | 'logId' => $entry['id'], |
||
| 70 | 'startpage' => $entry['points'] |
||
| 71 | ]; |
||
| 72 | } |
||
| 73 | } elseif ( |
||
| 74 | !empty($entry['points']) |
||
| 75 | && is_string($entry['points']) |
||
| 76 | ) { |
||
| 77 | $entryArray['id'] = $entry['points']; |
||
| 78 | $entryArray['page'] = 1; |
||
| 79 | $entryArray['doNotLinkIt'] = 0; |
||
| 80 | if ($this->settings['basketButton']) { |
||
| 81 | $entryArray['basketButton'] = [ |
||
| 82 | 'logId' => $entry['id'], |
||
| 83 | 'startpage' => $entry['points'] |
||
| 84 | ]; |
||
| 85 | } |
||
| 86 | } elseif (!empty($entry['targetUid'])) { |
||
| 87 | $entryArray['id'] = $entry['targetUid']; |
||
| 88 | $entryArray['page'] = 1; |
||
| 89 | $entryArray['doNotLinkIt'] = 0; |
||
| 90 | if ($this->settings['basketButton']) { |
||
| 91 | $entryArray['basketButton'] = [ |
||
| 92 | 'logId' => $entry['id'], |
||
| 93 | 'startpage' => $entry['targetUid'] |
||
| 94 | ]; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | // Set "ITEM_STATE" to "CUR" if this entry points to current page. |
||
| 98 | if (in_array($entry['id'], $this->activeEntries)) { |
||
| 99 | $entryArray['ITEM_STATE'] = 'CUR'; |
||
| 100 | } |
||
| 101 | // Build sub-menu if available and called recursively. |
||
| 102 | if ( |
||
| 103 | $recursive === true |
||
| 104 | && !empty($entry['children']) |
||
| 105 | ) { |
||
| 106 | // Build sub-menu only if one of the following conditions apply: |
||
| 107 | // 1. Current menu node is in rootline |
||
| 108 | // 2. Current menu node points to another file |
||
| 109 | // 3. Current menu node has no corresponding images |
||
| 110 | if ( |
||
| 111 | $entryArray['ITEM_STATE'] == 'CUR' |
||
| 112 | || is_string($entry['points']) |
||
| 113 | || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']]) |
||
| 114 | ) { |
||
| 115 | $entryArray['_SUB_MENU'] = []; |
||
| 116 | foreach ($entry['children'] as $child) { |
||
| 117 | // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page. |
||
| 118 | if (in_array($child['id'], $this->activeEntries)) { |
||
| 119 | $entryArray['ITEM_STATE'] = 'ACT'; |
||
| 120 | } |
||
| 121 | $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries. |
||
| 125 | $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB'); |
||
| 126 | } |
||
| 127 | return $entryArray; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * If $entry references an external METS file (as mptr), try to resolve its database UID |
||
| 132 | * and return an updated $entry. |
||
| 133 | * |
||
| 134 | * This is so that when linking from a child document back to its parent, |
||
| 135 | * that link is via UID, so that subsequently the parent's TOC is built from database. |
||
| 136 | * |
||
| 137 | * @param array $entry |
||
| 138 | * @return string|int |
||
| 139 | */ |
||
| 140 | protected function resolveMenuEntry($entry) |
||
| 141 | { |
||
| 142 | if (!empty($entry['points']) && is_string($entry['points'])) { |
||
| 143 | $doc = Doc::getInstance($entry['points'], ['storagePid' => $this->settings['storagePid']]); |
||
| 144 | if ($doc !== null && !empty($doc->recordId)) { |
||
| 145 | $model = $this->documentRepository->findOneByRecordId($doc->recordId); |
||
|
|
|||
| 146 | if ($model !== null) { |
||
| 147 | unset($entry['points']); |
||
| 148 | $entry['targetUid'] = $model->getUid(); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | return $entry; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * The main method of the plugin |
||
| 158 | * |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | public function mainAction() |
||
| 162 | { |
||
| 163 | $this->view->assign('toc', $this->makeMenuArray()); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * This builds a menu array for HMENU |
||
| 168 | * |
||
| 169 | * @access public |
||
| 170 | * @return array HMENU array |
||
| 171 | */ |
||
| 172 | public function makeMenuArray() |
||
| 254 | } |
||
| 255 | } |
||
| 256 |