Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 889b8e...f9b98e )
by Sebastian
32s queued 19s
created

Calendar   F

Complexity

Total Complexity 62

Size/Duplication

Total Lines 409
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 62
eloc 245
c 6
b 0
f 0
dl 0
loc 409
rs 3.44

4 Methods

Rating   Name   Duplication   Size   Complexity  
B main() 0 36 9
F calendar() 0 132 18
D getCalendarYear() 0 107 28
B years() 0 74 7

How to fix   Complexity   

Complex Class

Complex classes like Calendar 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 Calendar, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Plugin;
14
15
use Kitodo\Dlf\Common\Helper;
16
use TYPO3\CMS\Core\Database\ConnectionPool;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19
/**
20
 * Plugin 'Calendar' for the 'dlf' extension
21
 *
22
 * @author Alexander Bigga <[email protected]>
23
 * @author Sebastian Meyer <[email protected]>
24
 * @package TYPO3
25
 * @subpackage dlf
26
 * @access public
27
 */
28
class Calendar extends \Kitodo\Dlf\Common\AbstractPlugin
29
{
30
    public $scriptRelPath = 'Classes/Plugin/Calendar.php';
31
32
    /**
33
     * This holds all issues for the list view.
34
     *
35
     * @var array
36
     * @access protected
37
     */
38
    protected $allIssues = [];
39
40
    /**
41
     * The main method of the PlugIn
42
     *
43
     * @access public
44
     *
45
     * @param string $content: The PlugIn content
46
     * @param array $conf: The PlugIn configuration
47
     *
48
     * @return string The content that is displayed on the website
49
     */
50
    public function main($content, $conf)
51
    {
52
        $this->init($conf);
53
54
        // Set initial document (anchor or year file) if configured.
55
        if (empty($this->piVars['id']) && !empty($this->conf['initialDocument'])) {
56
            $this->piVars['id'] = $this->conf['initialDocument'];
0 ignored issues
show
Bug Best Practice introduced by
The property piVars does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
57
        }
58
59
        // Load current document.
60
        $this->loadDocument();
61
        if ($this->doc === null) {
62
            // Quit without doing anything if required variables are not set.
63
            return $content;
64
        }
65
66
        $metadata = $this->doc->getTitledata();
67
        if (!empty($metadata['type'][0])) {
68
            $type = $metadata['type'][0];
69
        } else {
70
            return $content;
71
        }
72
73
        switch ($type) {
74
            case 'newspaper':
75
            case 'ephemera':
76
                return $this->years($content, $conf);
77
            case 'year':
78
                return $this->calendar($content, $conf);
79
            case 'issue':
80
            default:
81
                break;
82
        }
83
84
        // Nothing to do here.
85
        return $content;
86
    }
87
88
    /**
89
     * The Calendar Method
90
     *
91
     * @access public
92
     *
93
     * @param string $content: The PlugIn content
94
     * @param array $conf: The PlugIn configuration
95
     *
96
     * @return string The content that is displayed on the website
97
     */
98
    public function calendar($content, $conf)
99
    {
100
        $this->init($conf);
101
        // Load current document.
102
        $this->loadDocument();
103
        if ($this->doc === null) {
104
            // Quit without doing anything if required variables are not set.
105
            return $content;
106
        }
107
        // Load template file.
108
        $this->getTemplate('###TEMPLATECALENDAR###');
109
110
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
111
            ->getQueryBuilderForTable('tx_dlf_documents');
112
113
        // Get all children of year anchor.
114
        $result = $queryBuilder
115
            ->select(
116
                'tx_dlf_documents.uid AS uid',
117
                'tx_dlf_documents.title AS title',
118
                'tx_dlf_documents.year AS year',
119
                'tx_dlf_documents.mets_label AS label',
120
                'tx_dlf_documents.mets_orderlabel AS orderlabel'
121
            )
122
            ->from('tx_dlf_documents')
123
            ->where(
124
                $queryBuilder->expr()->eq('tx_dlf_documents.structure', Helper::getUidFromIndexName('issue', 'tx_dlf_structures', $this->doc->cPid)),
0 ignored issues
show
introduced by
The property cPid is declared write-only in Kitodo\Dlf\Common\Document.
Loading history...
125
                $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($this->doc->uid)),
126
                Helper::whereExpression('tx_dlf_documents')
127
            )
128
            ->orderBy('tx_dlf_documents.mets_orderlabel')
129
            ->execute();
130
131
        $issues = [];
132
133
        // Process results.
134
        while ($resArray = $result->fetch()) {
135
            // Set title for display in calendar view.
136
            if (!empty($resArray['title'])) {
137
                $title = $resArray['title'];
138
            } else {
139
                $title = !empty($resArray['label']) ? $resArray['label'] : $resArray['orderlabel'];
140
                if (strtotime($title) !== false) {
141
                    $title = strftime('%x', strtotime($title));
142
                }
143
            }
144
            $issues[] = [
145
                'uid' => $resArray['uid'],
146
                'title' => $title,
147
                'year' => $resArray['year']
148
            ];
149
        }
150
        //  We need an array of issues with year => month => day number as key.
151
        $calendarIssuesByYear = [];
152
        foreach ($issues as $issue) {
153
            $dateTimestamp = strtotime($issue['year']);
154
            if ($dateTimestamp !== false) {
155
                $_year = date('Y', $dateTimestamp);
156
                $_month = date('n', $dateTimestamp);
157
                $_day = date('j', $dateTimestamp);
158
                $calendarIssuesByYear[$_year][$_month][$_day][] = $issue;
159
            } else {
160
                Helper::devLog('Document with UID ' . $issue['uid'] . 'has no valid date of publication', DEVLOG_SEVERITY_WARNING);
161
            }
162
        }
163
        // Sort by years.
164
        ksort($calendarIssuesByYear);
165
        // Build calendar for year (default) or season.
166
        $subPartContent = '';
167
        $iteration = 1;
168
        foreach ($calendarIssuesByYear as $year => $calendarIssuesByMonth) {
169
            // Sort by months.
170
            ksort($calendarIssuesByMonth);
171
            // Default: First month is January, last month is December.
172
            $firstMonth = 1;
173
            $lastMonth = 12;
174
            // Show calendar from first issue up to end of season if applicable.
175
            if (
176
                empty($this->conf['showEmptyMonths'])
177
                && count($calendarIssuesByYear) > 1
178
            ) {
179
                if ($iteration == 1) {
180
                    $firstMonth = (int) key($calendarIssuesByMonth);
181
                } elseif ($iteration == count($calendarIssuesByYear)) {
182
                    end($calendarIssuesByMonth);
183
                    $lastMonth = (int) key($calendarIssuesByMonth);
184
                }
185
            }
186
            $subPartContent .= $this->getCalendarYear($calendarIssuesByMonth, $year, $firstMonth, $lastMonth);
187
            $iteration++;
188
        }
189
        // Prepare list as alternative view.
190
        $subPartContentList = '';
191
        // Get subpart templates.
192
        $subParts['list'] = $this->templateService->getSubpart($this->template, '###ISSUELIST###');
193
        $subParts['singleday'] = $this->templateService->getSubpart($subParts['list'], '###SINGLEDAY###');
194
        foreach ($this->allIssues as $dayTimestamp => $issues) {
195
            $markerArrayDay['###DATE_STRING###'] = strftime('%A, %x', $dayTimestamp);
196
            $markerArrayDay['###ITEMS###'] = '';
197
            foreach ($issues as $issue) {
198
                $markerArrayDay['###ITEMS###'] .= $issue;
199
            }
200
            $subPartContentList .= $this->templateService->substituteMarkerArray($subParts['singleday'], $markerArrayDay);
201
        }
202
        $this->template = $this->templateService->substituteSubpart($this->template, '###SINGLEDAY###', $subPartContentList);
203
        // Link to current year.
204
        $linkConf = [
205
            'useCacheHash' => 1,
206
            'parameter' => $this->conf['targetPid'],
207
            'additionalParams' => '&' . $this->prefixId . '[id]=' . urlencode($this->doc->uid),
208
        ];
209
        $linkTitleData = $this->doc->getTitledata();
210
        $linkTitle = !empty($linkTitleData['mets_orderlabel'][0]) ? $linkTitleData['mets_orderlabel'][0] : $linkTitleData['mets_label'][0];
211
        $yearLink = $this->cObj->typoLink($linkTitle, $linkConf);
212
        // Link to years overview.
213
        $linkConf = [
214
            'useCacheHash' => 1,
215
            'parameter' => $this->conf['targetPid'],
216
            'additionalParams' => '&' . $this->prefixId . '[id]=' . urlencode($this->doc->parentId),
217
        ];
218
        $allYearsLink = $this->cObj->typoLink($this->pi_getLL('allYears', '', true) . ' ' . $this->doc->getTitle($this->doc->parentId), $linkConf);
219
        // Fill marker array.
220
        $markerArray = [
221
            '###CALENDARVIEWACTIVE###' => count($this->allIssues) > 5 ? 'active' : '',
222
            '###LISTVIEWACTIVE###' => count($this->allIssues) < 6 ? 'active' : '',
223
            '###CALYEAR###' => $yearLink,
224
            '###CALALLYEARS###' => $allYearsLink,
225
            '###LABEL_CALENDAR###' => $this->pi_getLL('label.view_calendar'),
226
            '###LABEL_LIST_VIEW###' => $this->pi_getLL('label.view_list'),
227
        ];
228
        $this->template = $this->templateService->substituteMarkerArray($this->template, $markerArray);
229
        return $this->templateService->substituteSubpart($this->template, '###CALMONTH###', $subPartContent);
230
    }
231
232
    /**
233
     * Build calendar for a certain year
234
     *
235
     * @access protected
236
     *
237
     * @param array $calendarIssuesByMonth All issues sorted by month => day
238
     * @param int $year Gregorian year
239
     * @param int $firstMonth 1 for January, 2 for February, ... 12 for December
240
     * @param int $lastMonth 1 for January, 2 for February, ... 12 for December
241
     *
242
     * @return string Content for template subpart
243
     */
244
    protected function getCalendarYear($calendarIssuesByMonth, $year, $firstMonth = 1, $lastMonth = 12)
245
    {
246
        // Get subpart templates.
247
        $subPartContent = '';
248
        $subParts['month'] = $this->templateService->getSubpart($this->template, '###CALMONTH###');
249
        $subParts['week'] = $this->templateService->getSubpart($subParts['month'], '###CALWEEK###');
250
        for ($i = $firstMonth; $i <= $lastMonth; $i++) {
251
            $markerArray = [
252
                '###DAYMON_NAME###' => strftime('%a', strtotime('last Monday')),
253
                '###DAYTUE_NAME###' => strftime('%a', strtotime('last Tuesday')),
254
                '###DAYWED_NAME###' => strftime('%a', strtotime('last Wednesday')),
255
                '###DAYTHU_NAME###' => strftime('%a', strtotime('last Thursday')),
256
                '###DAYFRI_NAME###' => strftime('%a', strtotime('last Friday')),
257
                '###DAYSAT_NAME###' => strftime('%a', strtotime('last Saturday')),
258
                '###DAYSUN_NAME###' => strftime('%a', strtotime('last Sunday')),
259
                '###MONTHNAME###'  => strftime('%B', strtotime($year . '-' . $i . '-1')) . ' ' . $year,
260
                '###CALYEAR###' => ($i == $firstMonth) ? '<div class="year">' . $year . '</div>' : ''
261
            ];
262
            // Fill the month markers.
263
            $subPartContentMonth = $this->templateService->substituteMarkerArray($subParts['month'], $markerArray);
264
            // Reset week content of new month.
265
            $subPartContentWeek = '';
266
            $firstOfMonth = strtotime($year . '-' . $i . '-1');
267
            $lastOfMonth = strtotime('last day of', ($firstOfMonth));
268
            $firstOfMonthStart = strtotime('last Monday', $firstOfMonth);
269
            // There are never more than 6 weeks in a month.
270
            for ($j = 0; $j <= 5; $j++) {
271
                $firstDayOfWeek = strtotime('+ ' . $j . ' Week', $firstOfMonthStart);
272
                $weekArray = [
273
                    '###DAYMON###' => '&nbsp;',
274
                    '###DAYTUE###' => '&nbsp;',
275
                    '###DAYWED###' => '&nbsp;',
276
                    '###DAYTHU###' => '&nbsp;',
277
                    '###DAYFRI###' => '&nbsp;',
278
                    '###DAYSAT###' => '&nbsp;',
279
                    '###DAYSUN###' => '&nbsp;',
280
                ];
281
                // Every week has seven days. ;-)
282
                for ($k = 0; $k <= 6; $k++) {
283
                    $currentDayTime = strtotime('+ ' . $k . ' Day', $firstDayOfWeek);
284
                    if (
285
                        $currentDayTime >= $firstOfMonth
286
                        && $currentDayTime <= $lastOfMonth
287
                    ) {
288
                        $dayLinks = '';
289
                        $dayLinksText = [];
290
                        $dayLinksList = '';
291
                        $currentMonth = date('n', $currentDayTime);
292
                        if (is_array($calendarIssuesByMonth[$currentMonth])) {
293
                            foreach ($calendarIssuesByMonth[$currentMonth] as $id => $day) {
294
                                if ($id == date('j', $currentDayTime)) {
295
                                    $dayLinks = $id;
296
                                    foreach ($day as $issue) {
297
                                        $dayLinkLabel = empty($issue['title']) ? strftime('%x', $currentDayTime) : $issue['title'];
298
                                        $linkConf = [
299
                                            'useCacheHash' => 1,
300
                                            'parameter' => $this->conf['targetPid'],
301
                                            'additionalParams' => '&' . $this->prefixId . '[id]=' . urlencode($issue['uid']),
302
                                            'ATagParams' => ' class="title"',
303
                                        ];
304
                                        $dayLinksText[] = $this->cObj->typoLink($dayLinkLabel, $linkConf);
305
                                        // Save issue for list view.
306
                                        $this->allIssues[$currentDayTime][] = $this->cObj->typoLink($dayLinkLabel, $linkConf);
307
                                    }
308
                                }
309
                            }
310
                            if (!empty($dayLinksText)) {
311
                                $dayLinksList = '<ul>';
312
                                foreach ($dayLinksText as $link) {
313
                                    $dayLinksList .= '<li>' . $link . '</li>';
314
                                }
315
                                $dayLinksList .= '</ul>';
316
                            }
317
                            $dayLinkDiv = '<div class="issues"><h4>' . strftime('%d', $currentDayTime) . '</h4><div>' . $dayLinksList . '</div></div>';
318
                        }
319
                        switch (strftime('%w', strtotime('+ ' . $k . ' Day', $firstDayOfWeek))) {
320
                            case '0':
321
                                $weekArray['###DAYSUN###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dayLinkDiv does not seem to be defined for all execution paths leading up to this point.
Loading history...
322
                                break;
323
                            case '1':
324
                                $weekArray['###DAYMON###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
325
                                break;
326
                            case '2':
327
                                $weekArray['###DAYTUE###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
328
                                break;
329
                            case '3':
330
                                $weekArray['###DAYWED###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
331
                                break;
332
                            case '4':
333
                                $weekArray['###DAYTHU###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
334
                                break;
335
                            case '5':
336
                                $weekArray['###DAYFRI###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
337
                                break;
338
                            case '6':
339
                                $weekArray['###DAYSAT###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
340
                                break;
341
                        }
342
                    }
343
                }
344
                // Fill the weeks.
345
                $subPartContentWeek .= $this->templateService->substituteMarkerArray($subParts['week'], $weekArray);
346
            }
347
            // Fill the week markers with the week entries.
348
            $subPartContent .= $this->templateService->substituteSubpart($subPartContentMonth, '###CALWEEK###', $subPartContentWeek);
349
        }
350
        return $subPartContent;
351
    }
352
353
    /**
354
     * The Years Method
355
     *
356
     * @access public
357
     *
358
     * @param string $content: The PlugIn content
359
     * @param array $conf: The PlugIn configuration
360
     *
361
     * @return string The content that is displayed on the website
362
     */
363
    public function years($content, $conf)
364
    {
365
        $this->init($conf);
366
        // Load current document.
367
        $this->loadDocument();
368
        if ($this->doc === null) {
369
            // Quit without doing anything if required variables are not set.
370
            return $content;
371
        }
372
        // Load template file.
373
        $this->getTemplate('###TEMPLATEYEAR###');
374
        // Get subpart templates
375
        $subparts['year'] = $this->templateService->getSubpart($this->template, '###LISTYEAR###');
376
        // Get the title of the anchor file
377
        $titleAnchor = $this->doc->getTitle($this->doc->uid);
378
379
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
380
            ->getQueryBuilderForTable('tx_dlf_documents');
381
382
        // Get all children of anchor. This should be the year anchor documents
383
        $result = $queryBuilder
384
            ->select(
385
                'tx_dlf_documents.uid AS uid',
386
                'tx_dlf_documents.title AS title',
387
                'tx_dlf_documents.mets_label AS label',
388
                'tx_dlf_documents.mets_orderlabel AS orderlabel'
389
            )
390
            ->from('tx_dlf_documents')
391
            ->where(
392
                $queryBuilder->expr()->eq('tx_dlf_documents.structure', Helper::getUidFromIndexName('year', 'tx_dlf_structures', $this->doc->cPid)),
0 ignored issues
show
introduced by
The property cPid is declared write-only in Kitodo\Dlf\Common\Document.
Loading history...
393
                $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($this->doc->uid)),
394
                Helper::whereExpression('tx_dlf_documents')
395
            )
396
            ->orderBy('tx_dlf_documents.volume_sorting')
397
            ->execute();
398
399
        $years = [];
400
        // Process results.
401
        while ($resArray = $result->fetch()) {
402
            $years[] = [
403
                'title' => !empty($resArray['label']) ? $resArray['label'] : (!empty($resArray['orderlabel']) ? $resArray['orderlabel'] : $resArray['title']),
404
                'uid' => $resArray['uid']
405
            ];
406
        }
407
        $subYearPartContent = '';
408
        if (count($years) > 0) {
409
            foreach ($years as $year) {
410
                $linkConf = [
411
                    'useCacheHash' => 1,
412
                    'parameter' => $this->conf['targetPid'],
413
                    'additionalParams' => '&' . $this->prefixId . '[id]=' . urlencode($year['uid']),
414
                    'title' => $titleAnchor . ': ' . $year['title']
415
                ];
416
                $yearArray = [
417
                    '###YEARNAME###' => $this->cObj->typoLink($year['title'], $linkConf),
418
                ];
419
                $subYearPartContent .= $this->templateService->substituteMarkerArray($subparts['year'], $yearArray);
420
            }
421
        }
422
        // Link to years overview (should be itself here)
423
        $linkConf = [
424
            'useCacheHash' => 1,
425
            'parameter' => $this->conf['targetPid'],
426
            'additionalParams' => '&' . $this->prefixId . '[id]=' . $this->doc->uid,
427
        ];
428
        $allYearsLink = $this->cObj->typoLink($this->pi_getLL('allYears', '', true) . ' ' . $this->doc->getTitle($this->doc->uid), $linkConf);
429
        // Fill markers.
430
        $markerArray = [
431
            '###LABEL_CHOOSE_YEAR###' => $this->pi_getLL('label.please_choose_year'),
432
            '###CALALLYEARS###' => $allYearsLink
433
        ];
434
        $this->template = $this->templateService->substituteMarkerArray($this->template, $markerArray);
435
        // Fill the week markers
436
        return $this->templateService->substituteSubpart($this->template, '###LISTYEAR###', $subYearPartContent);
437
    }
438
}
439