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 (#773)
by Beatrycze
03:39
created

TableOfContentsController::getMenuEntryWithImage()   C

Complexity

Conditions 13
Paths 40

Size

Total Lines 54
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 13
eloc 30
c 3
b 0
f 0
nc 40
nop 2
dl 0
loc 54
rs 6.6166

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
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace Kitodo\Dlf\Controller;
13
14
use Kitodo\Dlf\Common\Helper;
15
use TYPO3\CMS\Core\Utility\MathUtility;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18
/**
19
 * Controller class for plugin 'Table Of Contents'.
20
 *
21
 * @author Sebastian Meyer <[email protected]>
22
 * @package TYPO3
23
 * @subpackage dlf
24
 * @access public
25
 */
26
class TableOfContentsController extends AbstractController
27
{
28
    /**
29
     * This holds the active entries according to the currently selected page
30
     *
31
     * @var array
32
     * @access protected
33
     */
34
    protected $activeEntries = [];
35
36
    /**
37
     * @var array
38
     */
39
    protected $pluginConf;
40
41
    /**
42
     * SearchController constructor.
43
     */
44
    public function __construct()
45
    {
46
        parent::__construct();
47
        // Read plugin TS configuration.
48
        // TODO: This is more or less obsolete - the table of document plugin does not use the TypoScript anymore
49
        $this->pluginConf = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_dlf_tableofcontents.'];
50
        $this->initialize();
51
    }
52
53
    /**
54
     * This builds an array for one menu entry
55
     *
56
     * @access protected
57
     *
58
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
59
     * @param bool $recursive : Whether to include the child entries
60
     *
61
     * @return array HMENU array for menu entry
62
     */
63
    protected function getMenuEntry(array $entry, $recursive = false)
64
    {
65
        $entryArray = [];
66
        // Set "title", "volume", "type" and "pagination" from $entry array.
67
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
68
        $entryArray['volume'] = $entry['volume'];
69
        $entryArray['orderlabel'] = $entry['orderlabel'];
70
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
71
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
72
        $entryArray['_OVERRIDE_HREF'] = '';
73
        $entryArray['doNotLinkIt'] = 1;
74
        $entryArray['ITEM_STATE'] = 'NO';
75
        // Build menu links based on the $entry['points'] array.
76
        if (
77
            !empty($entry['points'])
78
            && MathUtility::canBeInterpretedAsInteger($entry['points'])
79
        ) {
80
            $entryArray['page'] = $entry['points'];
81
82
            $entryArray['doNotLinkIt'] = 0;
83
            if ($this->settings['basketButton']) {
84
                $entryArray['basketButton'] = [
85
                    'logId' => $entry['id'],
86
                    'startpage' => $entry['points']
87
                ];
88
            }
89
        } elseif (
90
            !empty($entry['points'])
91
            && is_string($entry['points'])
92
        ) {
93
            $entryArray['id'] = $entry['points'];
94
            $entryArray['page'] = 1;
95
            $entryArray['doNotLinkIt'] = 0;
96
            if ($this->settings['basketButton']) {
97
                $entryArray['basketButton'] = [
98
                    'logId' => $entry['id'],
99
                    'startpage' => $entry['points']
100
                ];
101
            }
102
        } elseif (!empty($entry['targetUid'])) {
103
            $entryArray['id'] = $entry['targetUid'];
104
            $entryArray['page'] = 1;
105
            $entryArray['doNotLinkIt'] = 0;
106
            if ($this->settings['basketButton']) {
107
                $entryArray['basketButton'] = [
108
                    'logId' => $entry['id'],
109
                    'startpage' => $entry['targetUid']
110
                ];
111
            }
112
        }
113
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
114
        if (in_array($entry['id'], $this->activeEntries)) {
115
            $entryArray['ITEM_STATE'] = 'CUR';
116
        }
117
        // Build sub-menu if available and called recursively.
118
        if (
119
            $recursive === true
120
            && !empty($entry['children'])
121
        ) {
122
            // Build sub-menu only if one of the following conditions apply:
123
            // 1. "expAll" is set for menu
124
            // 2. Current menu node is in rootline
125
            // 3. Current menu node points to another file
126
            // 4. Current menu node has no corresponding images
127
            if (
128
                !empty($this->pluginConf['menuConf.']['expAll'])
129
                || $entryArray['ITEM_STATE'] == 'CUR'
130
                || is_string($entry['points'])
131
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
132
            ) {
133
                $entryArray['_SUB_MENU'] = [];
134
                foreach ($entry['children'] as $child) {
135
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
136
                    if (in_array($child['id'], $this->activeEntries)) {
137
                        $entryArray['ITEM_STATE'] = 'ACT';
138
                    }
139
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
140
                }
141
            }
142
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
143
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
144
        }
145
        return $entryArray;
146
    }
147
148
    /**
149
     * The main method of the plugin
150
     *
151
     * @return void
152
     */
153
    public function mainAction()
154
    {
155
        // Check for typoscript configuration to prevent fatal error.
156
        if (empty($this->settings['menuConf']) && empty($this->settings['previewConf'])) {
157
            $this->logger->warning('Incomplete plugin configuration');
158
        }
159
160
        // Load current document.
161
        $this->loadDocument($this->requestData);
162
        if (
163
            $this->document === null
164
            || $this->document->getDoc() === null
165
        ) {
166
            // Quit without doing anything if required variables are not set.
167
            return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the documented return type void.
Loading history...
168
        } else {
169
            if ($this->document->getDoc()->metadataArray['LOG_0000']['type'][0] != 'collection') {
170
                $this->view->assign('toc', $this->makeMenuArray());
171
            } else {
172
                $this->view->assign('toc', $this->makeMenuFor3DObjects());
0 ignored issues
show
Bug introduced by
The call to Kitodo\Dlf\Controller\Ta...:makeMenuFor3DObjects() has too few arguments starting with content. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
                $this->view->assign('toc', $this->/** @scrutinizer ignore-call */ makeMenuFor3DObjects());

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
173
            }
174
        }
175
    }
176
177
    /**
178
     * This builds a menu array for HMENU
179
     *
180
     * @access public
181
     * @return array HMENU array
182
     */
183
    public function makeMenuArray()
184
    {
185
        if (!empty($this->requestData['logicalPage'])) {
186
             $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
187
            // The logical page parameter should not appear again
188
            unset($this->requestData['logicalPage']);
189
        }
190
        // Set default values for page if not set.
191
        // $this->requestData['page'] may be integer or string (physical structure @ID)
192
        if (
193
            (int) $this->requestData['page'] > 0
194
            || empty($this->requestData['page'])
195
        ) {
196
            $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
197
        } else {
198
            $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
199
        }
200
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
201
        $menuArray = [];
202
        // Does the document have physical elements or is it an external file?
203
        if (
204
            !empty($this->document->getDoc()->physicalStructure)
205
            || !MathUtility::canBeInterpretedAsInteger($this->document->getDoc()->uid)
0 ignored issues
show
Bug Best Practice introduced by
The property uid does not exist on Kitodo\Dlf\Common\Doc. Since you implemented __get, consider adding a @property annotation.
Loading history...
206
        ) {
207
            // Get all logical units the current page or track is a part of.
208
            if (
209
                !empty($this->requestData['page'])
210
                && !empty($this->document->getDoc()->physicalStructure)
211
            ) {
212
                $this->activeEntries = array_merge((array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[0]],
213
                    (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]]);
214
                if (
215
                    !empty($this->requestData['double'])
216
                    && $this->requestData['page'] < $this->document->getDoc()->numPages
217
                ) {
218
                    $this->activeEntries = array_merge($this->activeEntries,
219
                        (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page'] + 1]]);
220
                }
221
            }
222
            // Go through table of contents and create all menu entries.
223
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
224
                $menuArray[] = $this->getMenuEntry($entry, true);
225
            }
226
        } else {
227
            // Go through table of contents and create top-level menu entries.
228
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
229
                $menuArray[] = $this->getMenuEntry($entry, false);
230
            }
231
            // Build table of contents from database.
232
            $result = $this->documentRepository->getTableOfContentsFromDb($this->document->getUid(), $this->document->getPid(), $this->settings);
233
234
            $allResults = $result->fetchAll();
235
236
            if (count($allResults) > 0) {
237
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
238
                $menuArray[0]['_SUB_MENU'] = [];
239
                foreach ($allResults as $resArray) {
240
                    $entry = [
241
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
242
                        'type' => $resArray['type'],
243
                        'volume' => $resArray['volume'],
244
                        'orderlabel' => $resArray['mets_orderlabel'],
245
                        'pagination' => '',
246
                        'targetUid' => $resArray['uid']
247
                    ];
248
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
249
                }
250
            }
251
        }
252
        return $menuArray;
253
    }
254
255
    /**
256
     * This builds a menu for list of 3D objects
257
     *
258
     * @access public
259
     *
260
     * @param string $content: The PlugIn content
261
     * @param array $conf: The PlugIn configuration
262
     *
263
     * @return array HMENU array
264
     */
265
    public function makeMenuFor3DObjects($content, $conf)
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

265
    public function makeMenuFor3DObjects(/** @scrutinizer ignore-unused */ $content, $conf)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $conf is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

265
    public function makeMenuFor3DObjects($content, /** @scrutinizer ignore-unused */ $conf)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
266
    {
267
        if (!empty($this->requestData['logicalPage'])) {
268
            $this->requestData['page'] = $this->doc->getPhysicalPage($this->requestData['logicalPage']);
0 ignored issues
show
Bug Best Practice introduced by
The property doc does not exist on Kitodo\Dlf\Controller\TableOfContentsController. Did you maybe forget to declare it?
Loading history...
269
            // The logical page parameter should not appear again
270
            unset($this->requestData['logicalPage']);
271
        }
272
        // Set default values for page if not set.
273
        // $this->requestData['page'] may be integer or string (physical structure @ID)
274
        if (
275
            (int) $this->requestData['page'] > 0
276
            || empty($this->requestData['page'])
277
        ) {
278
            $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
279
        } else {
280
            $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
281
        }
282
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
283
        $menuArray = [];
284
        // Is the document an external file?
285
        if (!MathUtility::canBeInterpretedAsInteger($this->document->getDoc()->uid)) {
0 ignored issues
show
Bug Best Practice introduced by
The property uid does not exist on Kitodo\Dlf\Common\Doc. Since you implemented __get, consider adding a @property annotation.
Loading history...
286
            // Go through table of contents and create all menu entries.
287
            foreach ($this->doc->tableOfContents as $entry) {
288
                $menuArray[] = $this->getMenuEntryWithImage($entry, true);
289
            }
290
        }
291
        return $menuArray;
292
    }
293
294
    protected function getMenuEntryWithImage(array $entry, $recursive = false)
295
    {
296
        $entryArray = [];
297
        // Set "title", "volume", "type" and "pagination" from $entry array.
298
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
299
        $entryArray['orderlabel'] = $entry['orderlabel'];
300
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
301
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
302
        $entryArray['doNotLinkIt'] = 1;
303
304
        if ($entry['children'] == NULL) {
305
            $entryArray['description'] = $entry['description'];
306
            $id = str_replace("LOG", "PHYS", $entry['id']);
307
            $entryArray['image'] = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$id]['files']['THUMBS']);
0 ignored issues
show
Bug Best Practice introduced by
The property doc does not exist on Kitodo\Dlf\Controller\TableOfContentsController. Did you maybe forget to declare it?
Loading history...
308
            $entryArray['doNotLinkIt'] = 0;
309
            // index.php?tx_dlf%5Bid%5D=http%3A%2F%2Flink_to_METS_file.xml
310
            $entryArray['_OVERRIDE_HREF'] = '/index.php?id=' . GeneralUtility::_GET('id') . '&tx_dlf%5Bid%5D=' . urlencode($entry['points']);
311
        }
312
313
        $entryArray['ITEM_STATE'] = 'NO';
314
315
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
316
        if (in_array($entry['id'], $this->activeEntries)) {
317
            $entryArray['ITEM_STATE'] = 'CUR';
318
        }
319
        // Build sub-menu if available and called recursively.
320
        if (
321
            $recursive == true
322
            && !empty($entry['children'])
323
        ) {
324
            // Build sub-menu only if one of the following conditions apply:
325
            // 1. "expAll" is set for menu
326
            // 2. Current menu node is in rootline
327
            // 3. Current menu node points to another file
328
            // 4. Current menu node has no corresponding images
329
            if (
330
                !empty($this->pluginConf['previewConf.']['expAll'])
331
                || $entryArray['ITEM_STATE'] == 'CUR'
332
                || is_string($entry['points'])
333
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
334
            ) {
335
                $entryArray['_SUB_MENU'] = [];
336
                foreach ($entry['children'] as $child) {
337
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
338
                    if (in_array($child['id'], $this->activeEntries)) {
339
                        $entryArray['ITEM_STATE'] = 'ACT';
340
                    }
341
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntryWithImage($child, true);
342
                }
343
            }
344
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
345
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
346
        }
347
        return $entryArray;
348
    }
349
}
350