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.
Passed
Pull Request — master (#772)
by Alexander
03:06 queued 41s
created

TableOfContents::getMenuEntry()   F

Complexity

Conditions 20
Paths 140

Size

Total Lines 71
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 45
c 0
b 0
f 0
dl 0
loc 71
rs 3.8333
cc 20
nc 140
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use TYPO3\CMS\Core\Utility\MathUtility;
19
20
/**
21
 * Plugin 'Table Of Contents' for the 'dlf' extension
22
 *
23
 * @author Sebastian Meyer <[email protected]>
24
 * @package TYPO3
25
 * @subpackage dlf
26
 * @access public
27
 */
28
class TableOfContents extends \Kitodo\Dlf\Common\AbstractPlugin
29
{
30
    public $scriptRelPath = 'Classes/Plugin/TableOfContents.php';
31
32
    /**
33
     * This holds the active entries according to the currently selected page
34
     *
35
     * @var array
36
     * @access protected
37
     */
38
    protected $activeEntries = [];
39
40
    /**
41
     * This builds an array for one menu entry
42
     *
43
     * @access protected
44
     *
45
     * @param array $entry: The entry's array from \Kitodo\Dlf\Common\Document->getLogicalStructure
46
     * @param bool $recursive: Whether to include the child entries
47
     *
48
     * @return array HMENU array for menu entry
49
     */
50
    protected function getMenuEntry(array $entry, $recursive = false)
51
    {
52
        $entryArray = [];
53
        // Set "title", "volume", "type" and "pagination" from $entry array.
54
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
55
        $entryArray['volume'] = $entry['volume'];
56
        $entryArray['orderlabel'] = $entry['orderlabel'];
57
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->conf['pages']);
58
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
59
        $entryArray['_OVERRIDE_HREF'] = '';
60
        $entryArray['doNotLinkIt'] = 1;
61
        $entryArray['ITEM_STATE'] = 'NO';
62
        // Build menu links based on the $entry['points'] array.
63
        if (
64
            !empty($entry['points'])
65
            && MathUtility::canBeInterpretedAsInteger($entry['points'])
66
        ) {
67
            $entryArray['_OVERRIDE_HREF'] = $this->pi_linkTP_keepPIvars_url(['page' => $entry['points']], true, false, $this->conf['targetPid']);
68
            $entryArray['doNotLinkIt'] = 0;
69
            if ($this->conf['basketButton']) {
70
                $entryArray['basketButtonHref'] = '<a href="' . $this->pi_linkTP_keepPIvars_url(['addToBasket' => 'toc', 'logId' => $entry['id'], 'startpage' => $entry['points']], true, false, $this->conf['targetBasket']) . '">' . htmlspecialchars($this->pi_getLL('basketButton', '')) . '</a>';
71
            }
72
        } elseif (
73
            !empty($entry['points'])
74
            && is_string($entry['points'])
75
        ) {
76
            $entryArray['_OVERRIDE_HREF'] = $this->pi_linkTP_keepPIvars_url(['id' => $entry['points'], 'page' => 1], true, false, $this->conf['targetPid']);
77
            $entryArray['doNotLinkIt'] = 0;
78
            if ($this->conf['basketButton']) {
79
                $entryArray['basketButtonHref'] = '<a href="' . $this->pi_linkTP_keepPIvars_url(['addToBasket' => 'toc', 'logId' => $entry['id'], 'startpage' => $entry['points']], true, false, $this->conf['targetBasket']) . '">' . htmlspecialchars($this->pi_getLL('basketButton', '')) . '</a>';
80
            }
81
        } elseif (!empty($entry['targetUid'])) {
82
            $entryArray['_OVERRIDE_HREF'] = $this->pi_linkTP_keepPIvars_url(['id' => $entry['targetUid'], 'page' => 1], true, false, $this->conf['targetPid']);
83
            $entryArray['doNotLinkIt'] = 0;
84
            if ($this->conf['basketButton']) {
85
                $entryArray['basketButtonHref'] = '<a href="' . $this->pi_linkTP_keepPIvars_url(['addToBasket' => 'toc', 'logId' => $entry['id'], 'startpage' => $entry['targetUid']], true, false, $this->conf['targetBasket']) . '">' . htmlspecialchars($this->pi_getLL('basketButton', '')) . '</a>';
86
            }
87
        }
88
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
89
        if (in_array($entry['id'], $this->activeEntries)) {
90
            $entryArray['ITEM_STATE'] = 'CUR';
91
        }
92
        // Build sub-menu if available and called recursively.
93
        if (
94
            $recursive == true
95
            && !empty($entry['children'])
96
        ) {
97
            // Build sub-menu only if one of the following conditions apply:
98
            // 1. "expAll" is set for menu
99
            // 2. Current menu node is in rootline
100
            // 3. Current menu node points to another file
101
            // 4. Current menu node has no corresponding images
102
            if (
103
                !empty($this->conf['menuConf.']['expAll'])
104
                || $entryArray['ITEM_STATE'] == 'CUR'
105
                || is_string($entry['points'])
106
                || empty($this->doc->smLinks['l2p'][$entry['id']])
107
            ) {
108
                $entryArray['_SUB_MENU'] = [];
109
                foreach ($entry['children'] as $child) {
110
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
111
                    if (in_array($child['id'], $this->activeEntries)) {
112
                        $entryArray['ITEM_STATE'] = 'ACT';
113
                    }
114
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
115
                }
116
            }
117
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
118
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
119
        }
120
        return $entryArray;
121
    }
122
123
    /**
124
     * The main method of the PlugIn
125
     *
126
     * @access public
127
     *
128
     * @param string $content: The PlugIn content
129
     * @param array $conf: The PlugIn configuration
130
     *
131
     * @return string The content that is displayed on the website
132
     */
133
    public function main($content, $conf)
134
    {
135
        $this->init($conf);
136
        // Check for typoscript configuration to prevent fatal error.
137
        if (empty($this->conf['menuConf.']) && empty($this->conf['previewConf.'])) {
138
            $this->logger->warning('Incomplete plugin configuration');
139
            return $content;
140
        }
141
        // Load template file.
142
        $this->getTemplate();
143
        $TSconfig = [];
144
        $TSconfig['special'] = 'userfunction';
145
        if (empty($this->conf['previewConf.'])) {
146
            $TSconfig['special.']['userFunc'] = TableOfContents::class . '->makeMenuArray';
147
            $TSconfig = Helper::mergeRecursiveWithOverrule($this->conf['menuConf.'], $TSconfig);
148
        } else {
149
            $TSconfig['special.']['userFunc'] = TableOfContents::class . '->makeMenuFor3DObjects';
150
            $TSconfig = Helper::mergeRecursiveWithOverrule($this->conf['previewConf.'], $TSconfig);
151
        }
152
        $TSconfig = Helper::mergeRecursiveWithOverrule($this->conf['menuConf.'], $TSconfig);
153
        $markerArray['###TOCMENU###'] = $this->cObj->cObjGetSingle('HMENU', $TSconfig);
154
        $content .= $this->templateService->substituteMarkerArray($this->template, $markerArray);
155
        return $this->pi_wrapInBaseClass($content);
156
    }
157
158
    /**
159
     * This builds a menu array for HMENU
160
     *
161
     * @access public
162
     *
163
     * @param string $content: The PlugIn content
164
     * @param array $conf: The PlugIn configuration
165
     *
166
     * @return array HMENU array
167
     */
168
    public function makeMenuArray($content, $conf)
169
    {
170
        $this->init($conf);
171
        // Load current document.
172
        $this->loadDocument();
173
        if ($this->doc === null) {
174
            // Quit without doing anything if required variables are not set.
175
            return [];
176
        } else {
177
            if (!empty($this->piVars['logicalPage'])) {
178
                $this->piVars['page'] = $this->doc->getPhysicalPage($this->piVars['logicalPage']);
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...
179
                // The logical page parameter should not appear again
180
                unset($this->piVars['logicalPage']);
181
            }
182
            // Set default values for page if not set.
183
            // $this->piVars['page'] may be integer or string (physical structure @ID)
184
            if (
185
                (int) $this->piVars['page'] > 0
186
                || empty($this->piVars['page'])
187
            ) {
188
                $this->piVars['page'] = MathUtility::forceIntegerInRange((int) $this->piVars['page'], 1, $this->doc->numPages, 1);
189
            } else {
190
                $this->piVars['page'] = array_search($this->piVars['page'], $this->doc->physicalStructure);
191
            }
192
            $this->piVars['double'] = MathUtility::forceIntegerInRange($this->piVars['double'], 0, 1, 0);
193
        }
194
        $menuArray = [];
195
        // Does the document have physical elements or is it an external file?
196
        if (
197
            !empty($this->doc->physicalStructure)
198
            || !MathUtility::canBeInterpretedAsInteger($this->doc->uid)
199
        ) {
200
            // Get all logical units the current page or track is a part of.
201
            if (
202
                !empty($this->piVars['page'])
203
                && !empty($this->doc->physicalStructure)
204
            ) {
205
                $this->activeEntries = array_merge((array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[0]], (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page']]]);
206
                if (
207
                    !empty($this->piVars['double'])
208
                    && $this->piVars['page'] < $this->doc->numPages
209
                ) {
210
                    $this->activeEntries = array_merge($this->activeEntries, (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page'] + 1]]);
211
                }
212
            }
213
            // Go through table of contents and create all menu entries.
214
            foreach ($this->doc->tableOfContents as $entry) {
215
                $menuArray[] = $this->getMenuEntry($entry, true);
216
            }
217
        } else {
218
            // Go through table of contents and create top-level menu entries.
219
            foreach ($this->doc->tableOfContents as $entry) {
220
                $menuArray[] = $this->getMenuEntry($entry, false);
221
            }
222
            // Build table of contents from database.
223
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
224
                ->getQueryBuilderForTable('tx_dlf_documents');
225
226
            $excludeOtherWhere = '';
227
            if ($this->conf['excludeOther']) {
228
                $excludeOtherWhere = 'tx_dlf_documents.pid=' . intval($this->conf['pages']);
229
            }
230
            // Check if there are any metadata to suggest.
231
            $result = $queryBuilder
232
                ->select(
233
                    'tx_dlf_documents.uid AS uid',
234
                    'tx_dlf_documents.title AS title',
235
                    'tx_dlf_documents.volume AS volume',
236
                    'tx_dlf_documents.mets_label AS mets_label',
237
                    'tx_dlf_documents.mets_orderlabel AS mets_orderlabel',
238
                    'tx_dlf_structures_join.index_name AS type'
239
                )
240
                ->innerJoin(
241
                    'tx_dlf_documents',
242
                    'tx_dlf_structures',
243
                    'tx_dlf_structures_join',
244
                    $queryBuilder->expr()->eq(
245
                        'tx_dlf_structures_join.uid',
246
                        'tx_dlf_documents.structure'
247
                    )
248
                )
249
                ->from('tx_dlf_documents')
250
                ->where(
251
                    $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($this->doc->uid)),
252
                    $queryBuilder->expr()->eq('tx_dlf_structures_join.pid', intval($this->doc->pid)),
253
                    $excludeOtherWhere
254
                )
255
                ->addOrderBy('tx_dlf_documents.volume_sorting')
256
                ->addOrderBy('tx_dlf_documents.mets_orderlabel')
257
                ->execute();
258
259
            $allResults = $result->fetchAll();
260
261
            if (count($allResults) > 0) {
262
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
263
                $menuArray[0]['_SUB_MENU'] = [];
264
                foreach ($allResults as $resArray) {
265
                    $entry = [
266
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
267
                        'type' => $resArray['type'],
268
                        'volume' => $resArray['volume'],
269
                        'orderlabel' => $resArray['mets_orderlabel'],
270
                        'pagination' => '',
271
                        'targetUid' => $resArray['uid']
272
                    ];
273
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
274
                }
275
            }
276
        }
277
        return $menuArray;
278
    }
279
280
     /**
281
     * This builds a menu for list of 3D objects
282
     *
283
     * @access public
284
     *
285
     * @param string $content: The PlugIn content
286
     * @param array $conf: The PlugIn configuration
287
     *
288
     * @return array HMENU array
289
     */
290
    public function makeMenuFor3DObjects($content, $conf)
291
    {
292
        $this->init($conf);
293
        // Load current document.
294
        $this->loadDocument();
295
        if ($this->doc === null) {
296
            // Quit without doing anything if required variables are not set.
297
            return [];
298
        } else {
299
            if (!empty($this->piVars['logicalPage'])) {
300
                $this->piVars['page'] = $this->doc->getPhysicalPage($this->piVars['logicalPage']);
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...
301
                // The logical page parameter should not appear again
302
                unset($this->piVars['logicalPage']);
303
            }
304
            // Set default values for page if not set.
305
            // $this->piVars['page'] may be integer or string (physical structure @ID)
306
            if (
307
                (int) $this->piVars['page'] > 0
308
                || empty($this->piVars['page'])
309
            ) {
310
                $this->piVars['page'] = MathUtility::forceIntegerInRange((int) $this->piVars['page'], 1, $this->doc->numPages, 1);
311
            } else {
312
                $this->piVars['page'] = array_search($this->piVars['page'], $this->doc->physicalStructure);
313
            }
314
            $this->piVars['double'] = MathUtility::forceIntegerInRange($this->piVars['double'], 0, 1, 0);
315
        }
316
        $menuArray = [];
317
        // Is the document an external file?
318
        if (!MathUtility::canBeInterpretedAsInteger($this->doc->uid)) {
319
            // Go through table of contents and create all menu entries.
320
            foreach ($this->doc->tableOfContents as $entry) {
321
                $menuArray[] = $this->getMenuEntryWithImage($entry, true);
322
            }
323
        }
324
        return $menuArray;
325
    }
326
327
    protected function getMenuEntryWithImage(array $entry, $recursive = false)
328
    {
329
        $entryArray = [];
330
        // Set "title", "volume", "type" and "pagination" from $entry array.
331
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
332
        $entryArray['orderlabel'] = $entry['orderlabel'];
333
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->conf['pages']);
334
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
335
        $entryArray['doNotLinkIt'] = 1;
336
337
        if ($entry['children'] == NULL) {
338
            $entryArray['description'] = $entry['description'];
339
            $entryArray['image'] = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[1]]['files']['THUMBS']);
340
            $entryArray['doNotLinkIt'] = 0;
341
            // index.php?tx_dlf%5Bid%5D=http%3A%2F%2Flink_to_METS_file.xml
342
            $entryArray['_OVERRIDE_HREF'] = '/index.php?id=' . GeneralUtility::_GET('id') . '&tx_dlf%5Bid%5D=' . urlencode($entry['points']);
343
        }
344
345
        $entryArray['ITEM_STATE'] = 'NO';
346
347
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
348
        if (in_array($entry['id'], $this->activeEntries)) {
349
            $entryArray['ITEM_STATE'] = 'CUR';
350
        }
351
        // Build sub-menu if available and called recursively.
352
        if (
353
            $recursive == true
354
            && !empty($entry['children'])
355
        ) {
356
            // Build sub-menu only if one of the following conditions apply:
357
            // 1. "expAll" is set for menu
358
            // 2. Current menu node is in rootline
359
            // 3. Current menu node points to another file
360
            // 4. Current menu node has no corresponding images
361
            if (
362
                !empty($this->conf['previewConf.']['expAll'])
363
                || $entryArray['ITEM_STATE'] == 'CUR'
364
                || is_string($entry['points'])
365
                || empty($this->doc->smLinks['l2p'][$entry['id']])
366
            ) {
367
                $entryArray['_SUB_MENU'] = [];
368
                foreach ($entry['children'] as $child) {
369
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
370
                    if (in_array($child['id'], $this->activeEntries)) {
371
                        $entryArray['ITEM_STATE'] = 'ACT';
372
                    }
373
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntryWithImage($child, true);
374
                }
375
            }
376
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
377
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
378
        }
379
        return $entryArray;
380
    }
381
}
382