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 (#715)
by Alexander
06:09 queued 03:33
created

TableOfContentsController::getMenuEntry()   F

Complexity

Conditions 20
Paths 140

Size

Total Lines 83
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 83
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
 * (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 for plugin 'Table Of Contents' for the 'dlf' extension
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
        // Read plugin TS configuration.
47
        $this->pluginConf = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_dlf_tableofcontents.'];
48
    }
49
50
    /**
51
     * This builds an array for one menu entry
52
     *
53
     * @access protected
54
     *
55
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Document->getLogicalStructure
56
     * @param bool $recursive : Whether to include the child entries
57
     *
58
     * @return array HMENU array for menu entry
59
     */
60
    protected function getMenuEntry(array $entry, $recursive = false)
61
    {
62
        $entryArray = [];
63
        // Set "title", "volume", "type" and "pagination" from $entry array.
64
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
65
        $entryArray['volume'] = $entry['volume'];
66
        $entryArray['orderlabel'] = $entry['orderlabel'];
67
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['pages']);
68
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
69
        $entryArray['_OVERRIDE_HREF'] = '';
70
        $entryArray['doNotLinkIt'] = 1;
71
        $entryArray['ITEM_STATE'] = 'NO';
72
        // Build menu links based on the $entry['points'] array.
73
        if (
74
            !empty($entry['points'])
75
            && MathUtility::canBeInterpretedAsInteger($entry['points'])
76
        ) {
77
            $entryArray['page'] = $entry['points'];
78
79
            $entryArray['doNotLinkIt'] = 0;
80
            if ($this->settings['basketButton']) {
81
                $entryArray['basketButton'] = [
82
                    'logId' => $entry['id'],
83
                    'startpage' => $entry['points']
84
                ];
85
            }
86
        } elseif (
87
            !empty($entry['points'])
88
            && is_string($entry['points'])
89
        ) {
90
            $entryArray['id'] = $entry['points'];
91
            $entryArray['page'] = 1;
92
            $entryArray['doNotLinkIt'] = 0;
93
            if ($this->settings['basketButton']) {
94
                $entryArray['basketButton'] = [
95
                    'logId' => $entry['id'],
96
                    'startpage' => $entry['points']
97
                ];
98
            }
99
        } elseif (!empty($entry['targetUid'])) {
100
            $entryArray['id'] = $entry['targetUid'];
101
            $entryArray['page'] = 1;
102
            $entryArray['doNotLinkIt'] = 0;
103
            if ($this->settings['basketButton']) {
104
                $entryArray['basketButton'] = [
105
                    'logId' => $entry['id'],
106
                    'startpage' => $entry['targetUid']
107
                ];
108
            }
109
        }
110
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
111
        if (in_array($entry['id'], $this->activeEntries)) {
112
            $entryArray['ITEM_STATE'] = 'CUR';
113
        }
114
        // Build sub-menu if available and called recursively.
115
        if (
116
            $recursive === true
117
            && !empty($entry['children'])
118
        ) {
119
            // Build sub-menu only if one of the following conditions apply:
120
            // 1. "expAll" is set for menu
121
            // 2. Current menu node is in rootline
122
            // 3. Current menu node points to another file
123
            // 4. Current menu node has no corresponding images
124
            if (
125
                !empty($this->pluginConf['menuConf.']['expAll'])
126
                || $entryArray['ITEM_STATE'] == 'CUR'
127
                || is_string($entry['points'])
128
                || empty($this->doc->smLinks['l2p'][$entry['id']])
129
            ) {
130
                $entryArray['_SUB_MENU'] = [];
131
                foreach ($entry['children'] as $child) {
132
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
133
                    if (in_array($child['id'], $this->activeEntries)) {
134
                        $entryArray['ITEM_STATE'] = 'ACT';
135
                    }
136
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
137
                }
138
            }
139
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
140
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
141
        }
142
        return $entryArray;
143
    }
144
145
    /**
146
     * The main method of the plugin
147
     *
148
     * @return void
149
     */
150
    public function mainAction()
151
    {
152
        $requestData = GeneralUtility::_GPmerged('tx_dlf');
153
154
        // Check for typoscript configuration to prevent fatal error.
155
        if (empty($this->settings['menuConf'])) {
156
            $this->logger->warning('Incomplete plugin configuration');
157
        }
158
159
        $this->view->assign('toc', $this->makeMenuArray($requestData));
160
    }
161
162
    /**
163
     * This builds a menu array for HMENU
164
     *
165
     * @access public
166
     * @param array $requestData
167
     * @return array HMENU array
168
     */
169
    public function makeMenuArray($requestData)
170
    {
171
        // Load current document.
172
        $this->loadDocument($requestData);
173
        if ($this->doc === null) {
174
            // Quit without doing anything if required variables are not set.
175
            return [];
176
        } else {
177
            if (!empty($requestData['logicalPage'])) {
178
                $requestData['page'] = $this->doc->getPhysicalPage($requestData['logicalPage']);
179
                // The logical page parameter should not appear again
180
                unset($requestData['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) $requestData['page'] > 0
186
                || empty($requestData['page'])
187
            ) {
188
                $requestData['page'] = MathUtility::forceIntegerInRange((int) $requestData['page'],
189
                    1, $this->doc->numPages, 1);
190
            } else {
191
                $requestData['page'] = array_search($requestData['page'], $this->doc->physicalStructure);
192
            }
193
            $requestData['double'] = MathUtility::forceIntegerInRange($requestData['double'],
194
                0, 1, 0);
195
        }
196
        $menuArray = [];
197
        // Does the document have physical elements or is it an external file?
198
        if (
199
            !empty($this->doc->physicalStructure)
200
            || !MathUtility::canBeInterpretedAsInteger($this->doc->uid)
201
        ) {
202
            // Get all logical units the current page or track is a part of.
203
            if (
204
                !empty($requestData['page'])
205
                && !empty($this->doc->physicalStructure)
206
            ) {
207
                $this->activeEntries = array_merge((array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[0]],
208
                    (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$requestData['page']]]);
209
                if (
210
                    !empty($requestData['double'])
211
                    && $requestData['page'] < $this->doc->numPages
212
                ) {
213
                    $this->activeEntries = array_merge($this->activeEntries,
214
                        (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$requestData['page'] + 1]]);
215
                }
216
            }
217
            // Go through table of contents and create all menu entries.
218
            foreach ($this->doc->tableOfContents as $entry) {
219
                $menuArray[] = $this->getMenuEntry($entry, true);
220
            }
221
        } else {
222
            // Go through table of contents and create top-level menu entries.
223
            foreach ($this->doc->tableOfContents as $entry) {
224
                $menuArray[] = $this->getMenuEntry($entry, false);
225
            }
226
            // Build table of contents from database.
227
            $result = $this->documentRepository->getTableOfContentsFromDb($this->doc->uid, $this->doc->pid, $this->settings);
228
229
            $allResults = $result->fetchAll();
230
231
            if (count($allResults) > 0) {
232
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
233
                $menuArray[0]['_SUB_MENU'] = [];
234
                foreach ($allResults as $resArray) {
235
                    $entry = [
236
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
237
                        'type' => $resArray['type'],
238
                        'volume' => $resArray['volume'],
239
                        'orderlabel' => $resArray['mets_orderlabel'],
240
                        'pagination' => '',
241
                        'targetUid' => $resArray['uid']
242
                    ];
243
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
244
                }
245
            }
246
        }
247
        return $menuArray;
248
    }
249
}
250