We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 45 |
| Total Lines | 273 |
| 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 | * @return void |
||
| 40 | */ |
||
| 41 | public function mainAction() |
||
| 42 | { |
||
| 43 | // Load current document. |
||
| 44 | $this->loadDocument(); |
||
| 45 | if ($this->isDocMissing()) { |
||
| 46 | // Quit without doing anything if required variables are not set. |
||
| 47 | return; |
||
| 48 | } else { |
||
| 49 | $this->setPage(); |
||
| 50 | |||
| 51 | $this->view->assign('toc', $this->makeMenuArray()); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * This builds a menu array for HMENU |
||
| 57 | * |
||
| 58 | * @access protected |
||
| 59 | * @return array HMENU array |
||
| 60 | */ |
||
| 61 | protected function makeMenuArray() |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * This builds an array for one menu entry |
||
| 121 | * |
||
| 122 | * @access protected |
||
| 123 | * |
||
| 124 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 125 | * @param bool $recursive : Whether to include the child entries |
||
| 126 | * |
||
| 127 | * @return array HMENU array for menu entry |
||
| 128 | */ |
||
| 129 | protected function getMenuEntry(array $entry, $recursive = false) |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * If $entry references an external METS file (as mptr), |
||
| 220 | * try to resolve its database UID and return an updated $entry. |
||
| 221 | * |
||
| 222 | * This is so that when linking from a child document back to its parent, |
||
| 223 | * that link is via UID, so that subsequently the parent's TOC is built from database. |
||
| 224 | * |
||
| 225 | * @param array $entry |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | protected function resolveMenuEntry($entry) |
||
| 229 | { |
||
| 230 | // If the menu entry points to the parent document, |
||
| 231 | // resolve to the parent UID set on indexation. |
||
| 232 | $doc = $this->document->getDoc(); |
||
| 233 | if ( |
||
| 234 | $doc instanceof MetsDocument |
||
| 235 | && ($entry['points'] === $doc->parentHref || $this->isMultiElement($entry['type'])) |
||
| 236 | && !empty($this->document->getPartof()) |
||
| 237 | ) { |
||
| 238 | unset($entry['points']); |
||
| 239 | $entry['targetUid'] = $this->document->getPartof(); |
||
| 240 | } |
||
| 241 | |||
| 242 | return $entry; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get translated type of entry. |
||
| 247 | * |
||
| 248 | * @param string $type |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | private function getTranslatedType($type) { |
||
| 253 | return Helper::translate($type, 'tx_dlf_structures', $this->settings['storagePid']); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Check if element has type 'multivolume_work' or 'multipart_manuscript'. |
||
| 258 | * For Kitodo.Production prior to version 3.x, hierarchical child documents |
||
| 259 | * always come with their own METS file for their parent document, even |
||
| 260 | * if multiple documents in fact have the same parent. To make sure that all |
||
| 261 | * of them point to the same parent document in Kitodo.Presentation, we |
||
| 262 | * need some workaround here. |
||
| 263 | * |
||
| 264 | * @todo Should be removed when Kitodo.Production 2.x is no longer supported. |
||
| 265 | * |
||
| 266 | * @access private |
||
| 267 | * |
||
| 268 | * @param string $type |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | private function isMultiElement($type) { |
||
| 273 | return $type === 'multivolume_work' || $type === 'multipart_manuscript'; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Sort menu by orderlabel - currently implemented for newspaper. |
||
| 278 | * |
||
| 279 | * @param array &$menu |
||
| 280 | * |
||
| 281 | * @return void |
||
| 282 | */ |
||
| 283 | private function sortMenu(&$menu) { |
||
| 284 | if ($menu[0]['type'] == $this->getTranslatedType("newspaper")) { |
||
| 285 | $this->sortMenuForNewspapers($menu); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Sort menu years of the newspaper by orderlabel. |
||
| 291 | * |
||
| 292 | * @param array &$menu |
||
| 293 | * |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | private function sortMenuForNewspapers(&$menu) { |
||
| 299 | }); |
||
| 300 | } |
||
| 301 | } |
||
| 302 |