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 — dev-extbase-fluid (#737)
by
unknown
02:53
created

TableOfContentsController   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 112
dl 0
loc 232
rs 9.2
c 2
b 0
f 0
wmc 40

5 Methods

Rating   Name   Duplication   Size   Complexity  
C makeMenuArray() 0 79 16
A mainAction() 0 10 2
A injectDocumentRepository() 0 3 1
A __construct() 0 4 1
F getMenuEntry() 0 83 20

How to fix   Complexity   

Complex Class

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

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
use Kitodo\Dlf\Domain\Repository\DocumentRepository;
18
19
20
/**
21
 * Controller for 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 TableOfContentsController extends AbstractController
29
{
30
    /**
31
     * This holds the active entries according to the currently selected page
32
     *
33
     * @var array
34
     * @access protected
35
     */
36
    protected $activeEntries = [];
37
38
    /**
39
     * @var array
40
     */
41
    protected $pluginConf;
42
43
    protected $documentRepository;
44
45
    /**
46
     * @param DocumentRepository $documentRepository
47
     */
48
    public function injectDocumentRepository(DocumentRepository $documentRepository)
49
    {
50
        $this->documentRepository = $documentRepository;
51
    }
52
53
    /**
54
     * SearchController constructor.
55
     */
56
    public function __construct()
57
    {
58
        // Read plugin TS configuration.
59
        $this->pluginConf = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_dlf_tableofcontents.'];
60
    }
61
62
    /**
63
     * This builds an array for one menu entry
64
     *
65
     * @access protected
66
     *
67
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Document->getLogicalStructure
68
     * @param bool $recursive : Whether to include the child entries
69
     *
70
     * @return array HMENU array for menu entry
71
     */
72
    protected function getMenuEntry(array $entry, $recursive = false)
73
    {
74
        $entryArray = [];
75
        // Set "title", "volume", "type" and "pagination" from $entry array.
76
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
77
        $entryArray['volume'] = $entry['volume'];
78
        $entryArray['orderlabel'] = $entry['orderlabel'];
79
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['pages']);
80
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
81
        $entryArray['_OVERRIDE_HREF'] = '';
82
        $entryArray['doNotLinkIt'] = 1;
83
        $entryArray['ITEM_STATE'] = 'NO';
84
        // Build menu links based on the $entry['points'] array.
85
        if (
86
            !empty($entry['points'])
87
            && MathUtility::canBeInterpretedAsInteger($entry['points'])
88
        ) {
89
            $entryArray['page'] = $entry['points'];
90
91
            $entryArray['doNotLinkIt'] = 0;
92
            if ($this->settings['basketButton']) {
93
                $entryArray['basketButton'] = [
94
                    'logId' => $entry['id'],
95
                    'startpage' => $entry['points']
96
                ];
97
            }
98
        } elseif (
99
            !empty($entry['points'])
100
            && is_string($entry['points'])
101
        ) {
102
            $entryArray['id'] = $entry['points'];
103
            $entryArray['page'] = 1;
104
            $entryArray['doNotLinkIt'] = 0;
105
            if ($this->settings['basketButton']) {
106
                $entryArray['basketButton'] = [
107
                    'logId' => $entry['id'],
108
                    'startpage' => $entry['points']
109
                ];
110
            }
111
        } elseif (!empty($entry['targetUid'])) {
112
            $entryArray['id'] = $entry['targetUid'];
113
            $entryArray['page'] = 1;
114
            $entryArray['doNotLinkIt'] = 0;
115
            if ($this->settings['basketButton']) {
116
                $entryArray['basketButton'] = [
117
                    'logId' => $entry['id'],
118
                    'startpage' => $entry['targetUid']
119
                ];
120
            }
121
        }
122
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
123
        if (in_array($entry['id'], $this->activeEntries)) {
124
            $entryArray['ITEM_STATE'] = 'CUR';
125
        }
126
        // Build sub-menu if available and called recursively.
127
        if (
128
            $recursive === true
129
            && !empty($entry['children'])
130
        ) {
131
            // Build sub-menu only if one of the following conditions apply:
132
            // 1. "expAll" is set for menu
133
            // 2. Current menu node is in rootline
134
            // 3. Current menu node points to another file
135
            // 4. Current menu node has no corresponding images
136
            if (
137
                !empty($this->pluginConf['menuConf.']['expAll'])
138
                || $entryArray['ITEM_STATE'] == 'CUR'
139
                || is_string($entry['points'])
140
                || empty($this->doc->smLinks['l2p'][$entry['id']])
141
            ) {
142
                $entryArray['_SUB_MENU'] = [];
143
                foreach ($entry['children'] as $child) {
144
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
145
                    if (in_array($child['id'], $this->activeEntries)) {
146
                        $entryArray['ITEM_STATE'] = 'ACT';
147
                    }
148
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
149
                }
150
            }
151
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
152
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
153
        }
154
        return $entryArray;
155
    }
156
157
    /**
158
     * The main method of the plugin
159
     *
160
     * @return void
161
     */
162
    public function mainAction()
163
    {
164
        $requestData = GeneralUtility::_GPmerged('tx_dlf');
165
166
        // Check for typoscript configuration to prevent fatal error.
167
        if (empty($this->settings['menuConf'])) {
168
            $this->logger->warning('Incomplete plugin configuration');
1 ignored issue
show
Bug introduced by
The method warning() does not exist on null. ( Ignorable by Annotation )

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

168
            $this->logger->/** @scrutinizer ignore-call */ 
169
                           warning('Incomplete plugin configuration');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
169
        }
170
171
        $this->view->assign('toc', $this->makeMenuArray($requestData));
172
    }
173
174
    /**
175
     * This builds a menu array for HMENU
176
     *
177
     * @access public
178
     * @param array $requestData
179
     * @return array HMENU array
180
     */
181
    public function makeMenuArray($requestData)
182
    {
183
        // Load current document.
184
        $this->loadDocument($requestData);
185
        if ($this->doc === null) {
186
            // Quit without doing anything if required variables are not set.
187
            return [];
188
        } else {
189
            if (!empty($requestData['logicalPage'])) {
190
                $requestData['page'] = $this->doc->getPhysicalPage($requestData['logicalPage']);
191
                // The logical page parameter should not appear again
192
                unset($requestData['logicalPage']);
193
            }
194
            // Set default values for page if not set.
195
            // $this->piVars['page'] may be integer or string (physical structure @ID)
196
            if (
197
                (int) $requestData['page'] > 0
198
                || empty($requestData['page'])
199
            ) {
200
                $requestData['page'] = MathUtility::forceIntegerInRange((int) $requestData['page'],
201
                    1, $this->doc->numPages, 1);
202
            } else {
203
                $requestData['page'] = array_search($requestData['page'], $this->doc->physicalStructure);
204
            }
205
            $requestData['double'] = MathUtility::forceIntegerInRange($requestData['double'],
206
                0, 1, 0);
207
        }
208
        $menuArray = [];
209
        // Does the document have physical elements or is it an external file?
210
        if (
211
            !empty($this->doc->physicalStructure)
212
            || !MathUtility::canBeInterpretedAsInteger($this->doc->uid)
213
        ) {
214
            // Get all logical units the current page or track is a part of.
215
            if (
216
                !empty($requestData['page'])
217
                && !empty($this->doc->physicalStructure)
218
            ) {
219
                $this->activeEntries = array_merge((array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[0]],
220
                    (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$requestData['page']]]);
221
                if (
222
                    !empty($requestData['double'])
223
                    && $requestData['page'] < $this->doc->numPages
224
                ) {
225
                    $this->activeEntries = array_merge($this->activeEntries,
226
                        (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$requestData['page'] + 1]]);
227
                }
228
            }
229
            // Go through table of contents and create all menu entries.
230
            foreach ($this->doc->tableOfContents as $entry) {
231
                $menuArray[] = $this->getMenuEntry($entry, true);
232
            }
233
        } else {
234
            // Go through table of contents and create top-level menu entries.
235
            foreach ($this->doc->tableOfContents as $entry) {
236
                $menuArray[] = $this->getMenuEntry($entry, false);
237
            }
238
            // Build table of contents from database.
239
            $result = $this->documentRepository->getTableOfContentsFromDb($this->doc->uid, $this->doc->pid, $this->settings);
240
241
            $allResults = $result->fetchAll();
242
243
            if (count($allResults) > 0) {
244
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
245
                $menuArray[0]['_SUB_MENU'] = [];
246
                foreach ($allResults as $resArray) {
247
                    $entry = [
248
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
249
                        'type' => $resArray['type'],
250
                        'volume' => $resArray['volume'],
251
                        'orderlabel' => $resArray['mets_orderlabel'],
252
                        'pagination' => '',
253
                        'targetUid' => $resArray['uid']
254
                    ];
255
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
256
                }
257
            }
258
        }
259
        return $menuArray;
260
    }
261
}
262