We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 58 |
| Total Lines | 334 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CalendarController 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 CalendarController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class CalendarController extends AbstractController |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * This holds all issues for the list view. |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | * @access protected |
||
| 26 | */ |
||
| 27 | protected $allIssues = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The main method of the plugin |
||
| 31 | * |
||
| 32 | * @return void |
||
| 33 | */ |
||
| 34 | public function mainAction() |
||
| 35 | { |
||
| 36 | // Set initial document (anchor or year file) if configured. |
||
| 37 | if (empty($this->requestData['id']) && !empty($this->settings['initialDocument'])) { |
||
| 38 | $this->requestData['id'] = $this->settings['initialDocument']; |
||
| 39 | } |
||
| 40 | |||
| 41 | // Load current document. |
||
| 42 | $this->loadDocument($this->requestData); |
||
| 43 | if ($this->document === null) { |
||
| 44 | // Quit without doing anything if required variables are not set. |
||
| 45 | return; |
||
| 46 | } |
||
| 47 | |||
| 48 | $metadata = $this->document->getDoc()->getTitledata(); |
||
| 49 | if (!empty($metadata['type'][0])) { |
||
| 50 | $type = $metadata['type'][0]; |
||
| 51 | } else { |
||
| 52 | return; |
||
| 53 | } |
||
| 54 | |||
| 55 | switch ($type) { |
||
| 56 | case 'newspaper': |
||
| 57 | case 'ephemera': |
||
| 58 | $this->forward('years', null, null, $this->requestData); |
||
| 59 | break; |
||
| 60 | case 'year': |
||
| 61 | $this->forward('calendar', null, null, $this->requestData); |
||
| 62 | break; |
||
| 63 | case 'issue': |
||
| 64 | default: |
||
| 65 | break; |
||
| 66 | } |
||
| 67 | |||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The Calendar Method |
||
| 72 | * |
||
| 73 | * @access public |
||
| 74 | * |
||
| 75 | * @param string $content: The PlugIn content |
||
| 76 | * @param array $conf: The PlugIn configuration |
||
| 77 | * |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | public function calendarAction() |
||
| 81 | { |
||
| 82 | // access arguments passed by the mainAction() |
||
| 83 | $mainrequestData = $this->request->getArguments(); |
||
| 84 | |||
| 85 | // merge both arguments together --> passing id by GET parameter tx_dlf[id] should win |
||
| 86 | $this->requestData = array_merge($this->requestData, $mainrequestData); |
||
| 87 | |||
| 88 | // Load current document. |
||
| 89 | $this->loadDocument($this->requestData); |
||
| 90 | if ($this->document === null) { |
||
| 91 | // Quit without doing anything if required variables are not set. |
||
| 92 | return; |
||
| 93 | } |
||
| 94 | |||
| 95 | $documents = $this->documentRepository->getChildrenOfYearAnchor($this->document->getUid(), 'issue'); |
||
| 96 | |||
| 97 | $issues = []; |
||
| 98 | |||
| 99 | // Process results. |
||
| 100 | /** @var Document $document */ |
||
| 101 | foreach ($documents as $document) { |
||
| 102 | // Set title for display in calendar view. |
||
| 103 | if (!empty($document->getTitle())) { |
||
| 104 | $title = $document->getTitle(); |
||
| 105 | } else { |
||
| 106 | $title = !empty($document->getMetsLabel()) ? $document->getMetsLabel() : $document->getMetsOrderlabel(); |
||
| 107 | if (strtotime($title) !== false) { |
||
| 108 | $title = strftime('%x', strtotime($title)); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | $issues[] = [ |
||
| 112 | 'uid' => $document->getUid(), |
||
| 113 | 'title' => $title, |
||
| 114 | 'year' => $document->getYear() |
||
| 115 | ]; |
||
| 116 | } |
||
| 117 | |||
| 118 | // We need an array of issues with year => month => day number as key. |
||
| 119 | $calendarIssuesByYear = []; |
||
| 120 | foreach ($issues as $issue) { |
||
| 121 | $dateTimestamp = strtotime($issue['year']); |
||
| 122 | if ($dateTimestamp !== false) { |
||
| 123 | $_year = date('Y', $dateTimestamp); |
||
| 124 | $_month = date('n', $dateTimestamp); |
||
| 125 | $_day = date('j', $dateTimestamp); |
||
| 126 | $calendarIssuesByYear[$_year][$_month][$_day][] = $issue; |
||
| 127 | } else { |
||
| 128 | $this->logger->warning('Document with UID ' . $issue['uid'] . 'has no valid date of publication'); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | // Sort by years. |
||
| 132 | ksort($calendarIssuesByYear); |
||
| 133 | // Build calendar for year (default) or season. |
||
| 134 | $iteration = 1; |
||
| 135 | foreach ($calendarIssuesByYear as $year => $calendarIssuesByMonth) { |
||
| 136 | // Sort by months. |
||
| 137 | ksort($calendarIssuesByMonth); |
||
| 138 | // Default: First month is January, last month is December. |
||
| 139 | $firstMonth = 1; |
||
| 140 | $lastMonth = 12; |
||
| 141 | // Show calendar from first issue up to end of season if applicable. |
||
| 142 | if ( |
||
| 143 | empty($this->settings['showEmptyMonths']) |
||
| 144 | && count($calendarIssuesByYear) > 1 |
||
| 145 | ) { |
||
| 146 | if ($iteration == 1) { |
||
| 147 | $firstMonth = (int) key($calendarIssuesByMonth); |
||
| 148 | } elseif ($iteration == count($calendarIssuesByYear)) { |
||
| 149 | end($calendarIssuesByMonth); |
||
| 150 | $lastMonth = (int) key($calendarIssuesByMonth); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | $this->getCalendarYear($calendarIssuesByMonth, $year, $firstMonth, $lastMonth); |
||
| 154 | $iteration++; |
||
| 155 | } |
||
| 156 | // Prepare list as alternative view. |
||
| 157 | $issueData = []; |
||
| 158 | foreach ($this->allIssues as $dayTimestamp => $issues) { |
||
| 159 | $issueData[$dayTimestamp]['dateString'] = strftime('%A, %x', $dayTimestamp); |
||
| 160 | $issueData[$dayTimestamp]['items'] = []; |
||
| 161 | foreach ($issues as $issue) { |
||
| 162 | $issueData[$dayTimestamp]['items'][] = $issue; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | $this->view->assign('issueData', $issueData); |
||
| 166 | |||
| 167 | // Link to current year. |
||
| 168 | $linkTitleData = $this->document->getDoc()->getTitledata(); |
||
| 169 | $yearLinkTitle = !empty($linkTitleData['mets_orderlabel'][0]) ? $linkTitleData['mets_orderlabel'][0] : $linkTitleData['mets_label'][0]; |
||
| 170 | |||
| 171 | $this->view->assign('documentId', $this->document->getUid()); |
||
| 172 | $this->view->assign('yearLinkTitle', $yearLinkTitle); |
||
| 173 | $this->view->assign('parentDocumentId', $this->document->getPartof()); |
||
| 174 | $this->view->assign('allYearDocTitle', $this->document->getDoc()->getTitle($this->document->getPartof())); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * The Years Method |
||
| 179 | * |
||
| 180 | * @access public |
||
| 181 | * |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | public function yearsAction() |
||
| 185 | { |
||
| 186 | // access arguments passed by the mainAction() |
||
| 187 | $mainrequestData = $this->request->getArguments(); |
||
| 188 | |||
| 189 | // merge both arguments together --> passing id by GET parameter tx_dlf[id] should win |
||
| 190 | $this->requestData = array_merge($this->requestData, $mainrequestData); |
||
| 191 | |||
| 192 | // Load current document. |
||
| 193 | $this->loadDocument($this->requestData); |
||
| 194 | if ($this->document === null) { |
||
| 195 | // Quit without doing anything if required variables are not set. |
||
| 196 | return; |
||
| 197 | } |
||
| 198 | |||
| 199 | // Get all children of anchor. This should be the year anchor documents |
||
| 200 | $documents = $this->documentRepository->getChildrenOfYearAnchor($this->document->getUid(), 'year'); |
||
| 201 | |||
| 202 | $years = []; |
||
| 203 | // Process results. |
||
| 204 | /** @var Document $document */ |
||
| 205 | foreach ($documents as $document) { |
||
| 206 | $years[] = [ |
||
| 207 | 'title' => !empty($document->getMetsLabel()) ? $document->getMetsLabel() : (!empty($document->getMetsOrderlabel()) ? $document->getMetsOrderlabel() : $document->getTitle()), |
||
| 208 | 'uid' => $document->getUid() |
||
| 209 | ]; |
||
| 210 | } |
||
| 211 | |||
| 212 | $yearArray = []; |
||
| 213 | if (count($years) > 0) { |
||
| 214 | foreach ($years as $year) { |
||
| 215 | $yearArray[] = [ |
||
| 216 | 'documentId' => $year['uid'], |
||
| 217 | 'title' => $year['title'] |
||
| 218 | ]; |
||
| 219 | } |
||
| 220 | $this->view->assign('yearName', $yearArray); |
||
| 221 | } |
||
| 222 | |||
| 223 | $this->view->assign('documentId', $this->document->getUid()); |
||
| 224 | $this->view->assign('allYearDocTitle', $this->document->getDoc()->getTitle($this->document->getPartof())); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Build calendar for a certain year |
||
| 229 | * |
||
| 230 | * @access protected |
||
| 231 | * |
||
| 232 | * @param array $calendarIssuesByMonth All issues sorted by month => day |
||
| 233 | * @param int $year Gregorian year |
||
| 234 | * @param int $firstMonth 1 for January, 2 for February, ... 12 for December |
||
| 235 | * @param int $lastMonth 1 for January, 2 for February, ... 12 for December |
||
| 236 | * |
||
| 237 | * @return string Content for template subpart |
||
| 238 | */ |
||
| 239 | protected function getCalendarYear($calendarIssuesByMonth, $year, $firstMonth = 1, $lastMonth = 12) |
||
| 353 | } |
||
| 354 | } |
||
| 355 |