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 (#821)
by
unknown
04:02
created

TableOfContentsController::resolveMenuEntry()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
nc 2
nop 1
dl 0
loc 15
rs 10
c 1
b 0
f 0
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\Doc;
15
use Kitodo\Dlf\Common\Helper;
16
use Kitodo\Dlf\Common\MetsDocument;
17
use TYPO3\CMS\Core\Utility\MathUtility;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20
/**
21
 * Controller class for plugin 'Table Of Contents'.
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
     * This builds an array for one menu entry
40
     *
41
     * @access protected
42
     *
43
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
44
     * @param bool $recursive : Whether to include the child entries
45
     *
46
     * @return array HMENU array for menu entry
47
     */
48
    protected function getMenuEntry(array $entry, $recursive = false)
49
    {
50
        $entry = $this->resolveMenuEntry($entry);
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->settings['storagePid']);
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['page'] = $entry['points'];
68
69
            $entryArray['doNotLinkIt'] = 0;
70
            if ($this->settings['basketButton']) {
71
                $entryArray['basketButton'] = [
72
                    'logId' => $entry['id'],
73
                    'startpage' => $entry['points']
74
                ];
75
            }
76
        } elseif (
77
            !empty($entry['points'])
78
            && is_string($entry['points'])
79
        ) {
80
            $entryArray['id'] = $entry['points'];
81
            $entryArray['page'] = 1;
82
            $entryArray['doNotLinkIt'] = 0;
83
            if ($this->settings['basketButton']) {
84
                $entryArray['basketButton'] = [
85
                    'logId' => $entry['id'],
86
                    'startpage' => $entry['points']
87
                ];
88
            }
89
        } elseif (!empty($entry['targetUid'])) {
90
            $entryArray['id'] = $entry['targetUid'];
91
            $entryArray['page'] = 1;
92
            $entryArray['doNotLinkIt'] = 0;
93
            if ($this->settings['basketButton']) {
94
                $entryArray['basketButton'] = [
95
                    'logId' => $entry['id'],
96
                    'startpage' => $entry['targetUid']
97
                ];
98
            }
99
        }
100
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
101
        if (in_array($entry['id'], $this->activeEntries)) {
102
            $entryArray['ITEM_STATE'] = 'CUR';
103
        }
104
        // Build sub-menu if available and called recursively.
105
        if (
106
            $recursive === true
107
            && !empty($entry['children'])
108
        ) {
109
            // Build sub-menu only if one of the following conditions apply:
110
            // 1. Current menu node is in rootline
111
            // 2. Current menu node points to another file
112
            // 3. Current menu node has no corresponding images
113
            if (
114
                $entryArray['ITEM_STATE'] == 'CUR'
115
                || is_string($entry['points'])
116
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
117
            ) {
118
                $entryArray['_SUB_MENU'] = [];
119
                foreach ($entry['children'] as $child) {
120
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
121
                    if (in_array($child['id'], $this->activeEntries)) {
122
                        $entryArray['ITEM_STATE'] = 'ACT';
123
                    }
124
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
125
                }
126
            }
127
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
128
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
129
        }
130
        return $entryArray;
131
    }
132
133
    /**
134
     * If $entry references an external METS file (as mptr),
135
     * try to resolve its database UID and return an updated $entry.
136
     *
137
     * This is so that when linking from a child document back to its parent,
138
     * that link is via UID, so that subsequently the parent's TOC is built from database.
139
     *
140
     * @param array $entry
141
     * @return string|int
142
     */
143
    protected function resolveMenuEntry($entry)
144
    {
145
        // If the menu entry points to the parent document,
146
        // resolve to the parent UID set on indexation.
147
        $doc = $this->document->getDoc();
148
        if (
149
            $doc instanceof MetsDocument
150
            && $entry['points'] === $doc->parentHref
151
            && !empty($this->document->getPartof())
152
        ) {
153
            unset($entry['points']);
154
            $entry['targetUid'] = $this->document->getPartof();
155
        }
156
157
        return $entry;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $entry returns the type array which is incompatible with the documented return type integer|string.
Loading history...
158
    }
159
160
    /**
161
     * The main method of the plugin
162
     *
163
     * @return void
164
     */
165
    public function mainAction()
166
    {
167
        $this->view->assign('toc', $this->makeMenuArray());
168
    }
169
170
    /**
171
     * This builds a menu array for HMENU
172
     *
173
     * @access public
174
     * @return array HMENU array
175
     */
176
    public function makeMenuArray()
177
    {
178
        // Load current document.
179
        $this->loadDocument($this->requestData);
180
        if (
181
            $this->document === null
182
            || $this->document->getDoc() === null
183
        ) {
184
            // Quit without doing anything if required variables are not set.
185
            return [];
186
        } else {
187
            if (!empty($this->requestData['logicalPage'])) {
188
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
189
                // The logical page parameter should not appear again
190
                unset($this->requestData['logicalPage']);
191
            }
192
            // Set default values for page if not set.
193
            // $this->piVars['page'] may be integer or string (physical structure @ID)
194
            if (
195
                (int) $this->requestData['page'] > 0
196
                || empty($this->requestData['page'])
197
            ) {
198
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'],
199
                    1, $this->document->getDoc()->numPages, 1);
200
            } else {
201
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
202
            }
203
            $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'],
204
                0, 1, 0);
205
        }
206
        $menuArray = [];
207
        // Does the document have physical elements or is it an external file?
208
        if (
209
            !empty($this->document->getDoc()->physicalStructure)
210
            || !MathUtility::canBeInterpretedAsInteger($this->requestData['id'])
211
        ) {
212
            // Get all logical units the current page or track is a part of.
213
            if (
214
                !empty($this->requestData['page'])
215
                && !empty($this->document->getDoc()->physicalStructure)
216
            ) {
217
                $this->activeEntries = array_merge((array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[0]],
218
                    (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]]);
219
                if (
220
                    !empty($this->requestData['double'])
221
                    && $this->requestData['page'] < $this->document->getDoc()->numPages
222
                ) {
223
                    $this->activeEntries = array_merge($this->activeEntries,
224
                        (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page'] + 1]]);
225
                }
226
            }
227
            // Go through table of contents and create all menu entries.
228
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
229
                $menuArray[] = $this->getMenuEntry($entry, true);
230
            }
231
        } else {
232
            // Go through table of contents and create top-level menu entries.
233
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
234
                $menuArray[] = $this->getMenuEntry($entry, false);
235
            }
236
            // Build table of contents from database.
237
            $result = $this->documentRepository->getTableOfContentsFromDb($this->document->getUid(), $this->document->getPid(), $this->settings);
238
239
            $allResults = $result->fetchAll();
0 ignored issues
show
Bug introduced by
The method fetchAll() does not exist on TYPO3\CMS\Extbase\Persistence\QueryResultInterface. ( Ignorable by Annotation )

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

239
            /** @scrutinizer ignore-call */ 
240
            $allResults = $result->fetchAll();

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...
240
241
            if (count($allResults) > 0) {
242
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
243
                $menuArray[0]['_SUB_MENU'] = [];
244
                foreach ($allResults as $resArray) {
245
                    $entry = [
246
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
247
                        'type' => $resArray['type'],
248
                        'volume' => $resArray['volume'],
249
                        'orderlabel' => $resArray['mets_orderlabel'],
250
                        'pagination' => '',
251
                        'targetUid' => $resArray['uid']
252
                    ];
253
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
254
                }
255
            }
256
        }
257
        return $menuArray;
258
    }
259
}
260