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