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