We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 54 |
| Total Lines | 328 |
| 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 |
||
| 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 | * The main method of the plugin |
||
| 38 | * |
||
| 39 | * @access public |
||
| 40 | * |
||
| 41 | * @return void |
||
| 42 | */ |
||
| 43 | public function mainAction() |
||
| 44 | { |
||
| 45 | // Load current document. |
||
| 46 | $this->loadDocument(); |
||
| 47 | if ($this->isDocMissing()) { |
||
| 48 | // Quit without doing anything if required variables are not set. |
||
| 49 | return; |
||
| 50 | } else { |
||
| 51 | $this->setPage(); |
||
| 52 | |||
| 53 | $this->view->assign('toc', $this->makeMenuArray()); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * This builds a menu array for HMENU |
||
| 59 | * |
||
| 60 | * @access private |
||
| 61 | * |
||
| 62 | * @return array HMENU array |
||
| 63 | */ |
||
| 64 | private function makeMenuArray() |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * This builds an array for one menu entry |
||
| 111 | * |
||
| 112 | * @access private |
||
| 113 | * |
||
| 114 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 115 | * @param bool $recursive : Whether to include the child entries |
||
| 116 | * |
||
| 117 | * @return array HMENU array for menu entry |
||
| 118 | */ |
||
| 119 | private function getMenuEntry(array $entry, $recursive = false) |
||
| 120 | { |
||
| 121 | $entry = $this->resolveMenuEntry($entry); |
||
| 122 | |||
| 123 | $entryArray = []; |
||
| 124 | // Set "title", "volume", "type" and "pagination" from $entry array. |
||
| 125 | $entryArray['title'] = $this->setTitle($entry); |
||
| 126 | $entryArray['volume'] = $entry['volume']; |
||
| 127 | $entryArray['year'] = $entry['year']; |
||
| 128 | $entryArray['orderlabel'] = $entry['orderlabel']; |
||
| 129 | $entryArray['type'] = $this->getTranslatedType($entry['type']); |
||
| 130 | $entryArray['pagination'] = htmlspecialchars($entry['pagination']); |
||
| 131 | $entryArray['_OVERRIDE_HREF'] = ''; |
||
| 132 | $entryArray['doNotLinkIt'] = 1; |
||
| 133 | $entryArray['ITEM_STATE'] = 'NO'; |
||
| 134 | |||
| 135 | // Build menu links based on the $entry['points'] array. |
||
| 136 | if ( |
||
| 137 | !empty($entry['points']) |
||
| 138 | && MathUtility::canBeInterpretedAsInteger($entry['points']) |
||
| 139 | ) { |
||
| 140 | $entryArray['page'] = $entry['points']; |
||
| 141 | |||
| 142 | $entryArray['doNotLinkIt'] = 0; |
||
| 143 | if ($this->settings['basketButton']) { |
||
| 144 | $entryArray['basketButton'] = [ |
||
| 145 | 'logId' => $entry['id'], |
||
| 146 | 'startpage' => $entry['points'] |
||
| 147 | ]; |
||
| 148 | } |
||
| 149 | } elseif ( |
||
| 150 | !empty($entry['points']) |
||
| 151 | && is_string($entry['points']) |
||
| 152 | ) { |
||
| 153 | $entryArray['id'] = $entry['points']; |
||
| 154 | $entryArray['page'] = 1; |
||
| 155 | $entryArray['doNotLinkIt'] = 0; |
||
| 156 | if ($this->settings['basketButton']) { |
||
| 157 | $entryArray['basketButton'] = [ |
||
| 158 | 'logId' => $entry['id'], |
||
| 159 | 'startpage' => $entry['points'] |
||
| 160 | ]; |
||
| 161 | } |
||
| 162 | } elseif (!empty($entry['targetUid'])) { |
||
| 163 | $entryArray['id'] = $entry['targetUid']; |
||
| 164 | $entryArray['page'] = 1; |
||
| 165 | $entryArray['doNotLinkIt'] = 0; |
||
| 166 | if ($this->settings['basketButton']) { |
||
| 167 | $entryArray['basketButton'] = [ |
||
| 168 | 'logId' => $entry['id'], |
||
| 169 | 'startpage' => $entry['targetUid'] |
||
| 170 | ]; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | // Set "ITEM_STATE" to "CUR" if this entry points to current page. |
||
| 174 | if (in_array($entry['id'], $this->activeEntries)) { |
||
| 175 | $entryArray['ITEM_STATE'] = 'CUR'; |
||
| 176 | } |
||
| 177 | // Build sub-menu if available and called recursively. |
||
| 178 | if ( |
||
| 179 | $recursive === true |
||
| 180 | && !empty($entry['children']) |
||
| 181 | ) { |
||
| 182 | // Build sub-menu only if one of the following conditions apply: |
||
| 183 | // 1. Current menu node is in rootline |
||
| 184 | // 2. Current menu node points to another file |
||
| 185 | // 3. Current menu node has no corresponding images |
||
| 186 | if ( |
||
| 187 | $entryArray['ITEM_STATE'] == 'CUR' |
||
| 188 | || is_string($entry['points']) |
||
| 189 | || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']]) |
||
| 190 | ) { |
||
| 191 | $entryArray['_SUB_MENU'] = []; |
||
| 192 | foreach ($entry['children'] as $child) { |
||
| 193 | // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page. |
||
| 194 | if (in_array($child['id'], $this->activeEntries)) { |
||
| 195 | $entryArray['ITEM_STATE'] = 'ACT'; |
||
| 196 | } |
||
| 197 | $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries. |
||
| 201 | $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB'); |
||
| 202 | } |
||
| 203 | return $entryArray; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * If $entry references an external METS file (as mptr), |
||
| 208 | * try to resolve its database UID and return an updated $entry. |
||
| 209 | * |
||
| 210 | * This is so that when linking from a child document back to its parent, |
||
| 211 | * that link is via UID, so that subsequently the parent's TOC is built from database. |
||
| 212 | * |
||
| 213 | * @access private |
||
| 214 | * |
||
| 215 | * @param array $entry |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | private function resolveMenuEntry($entry) |
||
| 219 | { |
||
| 220 | // If the menu entry points to the parent document, |
||
| 221 | // resolve to the parent UID set on indexation. |
||
| 222 | $doc = $this->document->getDoc(); |
||
| 223 | if ( |
||
| 224 | $doc instanceof MetsDocument |
||
| 225 | && ($entry['points'] === $doc->parentHref || $this->isMultiElement($entry['type'])) |
||
| 226 | && !empty($this->document->getPartof()) |
||
| 227 | ) { |
||
| 228 | unset($entry['points']); |
||
| 229 | $entry['targetUid'] = $this->document->getPartof(); |
||
| 230 | } |
||
| 231 | |||
| 232 | return $entry; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get all logical units the current page or track is a part of. |
||
| 237 | * |
||
| 238 | * @access private |
||
| 239 | * |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | private function getAllLogicalUnits() { |
||
| 243 | if ( |
||
| 244 | !empty($this->requestData['page']) |
||
| 245 | && !empty($this->document->getDoc()->physicalStructure) |
||
| 246 | ) { |
||
| 247 | $this->activeEntries = array_merge((array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[0]], |
||
| 248 | (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]]); |
||
| 249 | if ( |
||
| 250 | !empty($this->requestData['double']) |
||
| 251 | && $this->requestData['page'] < $this->document->getDoc()->numPages |
||
| 252 | ) { |
||
| 253 | $this->activeEntries = array_merge($this->activeEntries, |
||
| 254 | (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page'] + 1]]); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get translated type of entry. |
||
| 261 | * |
||
| 262 | * @access private |
||
| 263 | * |
||
| 264 | * @param string $type |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | private function getTranslatedType($type) { |
||
| 269 | return Helper::translate($type, 'tx_dlf_structures', $this->settings['storagePid']); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Check if element has type 'multivolume_work' or 'multipart_manuscript'. |
||
| 274 | * For Kitodo.Production prior to version 3.x, hierarchical child documents |
||
| 275 | * always come with their own METS file for their parent document, even |
||
| 276 | * if multiple documents in fact have the same parent. To make sure that all |
||
| 277 | * of them point to the same parent document in Kitodo.Presentation, we |
||
| 278 | * need some workaround here. |
||
| 279 | * |
||
| 280 | * @todo Should be removed when Kitodo.Production 2.x is no longer supported. |
||
| 281 | * |
||
| 282 | * @access private |
||
| 283 | * |
||
| 284 | * @param string $type |
||
| 285 | * |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | private function isMultiElement($type) { |
||
| 290 | } |
||
| 291 | /** |
||
| 292 | * Set title from entry. |
||
| 293 | * |
||
| 294 | * @access private |
||
| 295 | * |
||
| 296 | * @param array $entry |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | private function setTitle($entry) { |
||
| 301 | if (empty($entry['label']) && empty($entry['orderlabel'])) { |
||
| 302 | foreach ($this->settings['titleReplacements'] as $titleReplacement) { |
||
| 303 | if ($entry['type'] == $titleReplacement['type']) { |
||
| 304 | $fields = explode(",", $titleReplacement['fields']); |
||
| 305 | $title = ''; |
||
| 306 | foreach ($fields as $field) { |
||
| 307 | if ($field == 'type') { |
||
| 308 | $title .= $this->getTranslatedType($entry['type']) . ' '; |
||
| 309 | } else { |
||
| 310 | $title .= $entry[$field] . ' '; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | return trim($title); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } |
||
| 318 | return $entry['label'] ?: $entry['orderlabel']; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Sort menu by orderlabel. |
||
| 323 | * |
||
| 324 | * @access private |
||
| 325 | * |
||
| 326 | * @param array &$menu |
||
| 327 | * |
||
| 328 | * @return void |
||
| 329 | */ |
||
| 330 | private function sortMenu(&$menu) { |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Sort sub menu e.g years of the newspaper by orderlabel. |
||
| 341 | * |
||
| 342 | * @access private |
||
| 343 | * |
||
| 344 | * @param array &$menu |
||
| 345 | * |
||
| 346 | * @return void |
||
| 347 | */ |
||
| 348 | private function sortSubMenu(&$menu) { |
||
| 354 | }); |
||
| 355 | } |
||
| 357 |