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