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 (#876)
by Beatrycze
04:07
created

TableOfContentsController::mainAction()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 3
b 0
f 0
nc 3
nop 0
dl 0
loc 18
rs 9.9332
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 Kitodo\Dlf\Common\MetsDocument;
16
use TYPO3\CMS\Core\Utility\MathUtility;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19
/**
20
 * Controller class for plugin 'Table Of Contents'.
21
 *
22
 * @author Sebastian Meyer <[email protected]>
23
 * @package TYPO3
24
 * @subpackage dlf
25
 * @access public
26
 */
27
class TableOfContentsController extends AbstractController
28
{
29
    /**
30
     * This holds the active entries according to the currently selected page
31
     *
32
     * @var array
33
     * @access protected
34
     */
35
    protected $activeEntries = [];
36
37
    /**
38
     * The main method of the plugin
39
     *
40
     * @return void
41
     */
42
    public function mainAction()
43
    {
44
        // Load current document.
45
        $this->loadDocument($this->requestData);
46
        if (
47
            $this->document === null
48
            || $this->document->getDoc() === null
49
        ) {
50
            // Quit without doing anything if required variables are not set.
51
            return;
52
        } else {
53
            if (!empty($this->requestData['logicalPage'])) {
54
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
55
                // The logical page parameter should not appear again
56
                unset($this->requestData['logicalPage']);
57
            }
58
59
            $this->view->assign('toc', $this->makeMenuArray());
60
        }
61
    }
62
63
    /**
64
     * This builds a menu array for HMENU
65
     *
66
     * @access protected
67
     * @return array HMENU array
68
     */
69
    protected function makeMenuArray()
70
    {
71
        // Set default values for page if not set.
72
        // $this->requestData['page'] may be integer or string (physical structure @ID)
73
        if (
74
            (int) $this->requestData['page'] > 0
75
            || empty($this->requestData['page'])
76
        ) {
77
            $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
78
        } else {
79
            $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
80
        }
81
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
82
        $menuArray = [];
83
        // Does the document have physical elements or is it an external file?
84
        if (
85
            !empty($this->document->getDoc()->physicalStructure)
86
            || !MathUtility::canBeInterpretedAsInteger($this->requestData['id'])
87
        ) {
88
            // Get all logical units the current page or track is a part of.
89
            if (
90
                !empty($this->requestData['page'])
91
                && !empty($this->document->getDoc()->physicalStructure)
92
            ) {
93
                $this->activeEntries = array_merge((array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[0]],
94
                    (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]]);
95
                if (
96
                    !empty($this->requestData['double'])
97
                    && $this->requestData['page'] < $this->document->getDoc()->numPages
98
                ) {
99
                    $this->activeEntries = array_merge($this->activeEntries,
100
                        (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page'] + 1]]);
101
                }
102
            }
103
            // Go through table of contents and create all menu entries.
104
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
105
                $menuArray[] = $this->getMenuEntry($entry, true);
106
            }
107
        } else {
108
            // Go through table of contents and create top-level menu entries.
109
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
110
                $menuArray[] = $this->getMenuEntry($entry, false);
111
            }
112
            // Build table of contents from database.
113
            $result = $this->documentRepository->getTableOfContentsFromDb($this->document->getUid(), $this->document->getPid(), $this->settings);
114
115
            $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

115
            /** @scrutinizer ignore-call */ 
116
            $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...
116
117
            if (count($allResults) > 0) {
118
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
119
                $menuArray[0]['_SUB_MENU'] = [];
120
                foreach ($allResults as $resArray) {
121
                    $entry = [
122
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
123
                        'type' => $resArray['type'],
124
                        'volume' => $resArray['volume'],
125
                        'orderlabel' => $resArray['mets_orderlabel'],
126
                        'pagination' => '',
127
                        'targetUid' => $resArray['uid']
128
                    ];
129
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
130
                }
131
            }
132
        }
133
        return $menuArray;
134
    }
135
136
    /**
137
     * This builds an array for one menu entry
138
     *
139
     * @access protected
140
     *
141
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
142
     * @param bool $recursive : Whether to include the child entries
143
     *
144
     * @return array HMENU array for menu entry
145
     */
146
    protected function getMenuEntry(array $entry, $recursive = false)
147
    {
148
        $entry = $this->resolveMenuEntry($entry);
149
150
        $entryArray = [];
151
        // Set "title", "volume", "type" and "pagination" from $entry array.
152
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
153
        $entryArray['volume'] = $entry['volume'];
154
        $entryArray['orderlabel'] = $entry['orderlabel'];
155
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
156
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
157
        $entryArray['_OVERRIDE_HREF'] = '';
158
        $entryArray['doNotLinkIt'] = 1;
159
        $entryArray['ITEM_STATE'] = 'NO';
160
        // Build menu links based on the $entry['points'] array.
161
        if (
162
            !empty($entry['points'])
163
            && MathUtility::canBeInterpretedAsInteger($entry['points'])
164
        ) {
165
            $entryArray['page'] = $entry['points'];
166
167
            $entryArray['doNotLinkIt'] = 0;
168
            if ($this->settings['basketButton']) {
169
                $entryArray['basketButton'] = [
170
                    'logId' => $entry['id'],
171
                    'startpage' => $entry['points']
172
                ];
173
            }
174
        } elseif (
175
            !empty($entry['points'])
176
            && is_string($entry['points'])
177
        ) {
178
            $entryArray['id'] = $entry['points'];
179
            $entryArray['page'] = 1;
180
            $entryArray['doNotLinkIt'] = 0;
181
            if ($this->settings['basketButton']) {
182
                $entryArray['basketButton'] = [
183
                    'logId' => $entry['id'],
184
                    'startpage' => $entry['points']
185
                ];
186
            }
187
        } elseif (!empty($entry['targetUid'])) {
188
            $entryArray['id'] = $entry['targetUid'];
189
            $entryArray['page'] = 1;
190
            $entryArray['doNotLinkIt'] = 0;
191
            if ($this->settings['basketButton']) {
192
                $entryArray['basketButton'] = [
193
                    'logId' => $entry['id'],
194
                    'startpage' => $entry['targetUid']
195
                ];
196
            }
197
        }
198
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
199
        if (in_array($entry['id'], $this->activeEntries)) {
200
            $entryArray['ITEM_STATE'] = 'CUR';
201
        }
202
        // Build sub-menu if available and called recursively.
203
        if (
204
            $recursive === true
205
            && !empty($entry['children'])
206
        ) {
207
            // Build sub-menu only if one of the following conditions apply:
208
            // 1. Current menu node is in rootline
209
            // 2. Current menu node points to another file
210
            // 3. Current menu node has no corresponding images
211
            if (
212
                $entryArray['ITEM_STATE'] == 'CUR'
213
                || is_string($entry['points'])
214
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
215
            ) {
216
                $entryArray['_SUB_MENU'] = [];
217
                foreach ($entry['children'] as $child) {
218
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
219
                    if (in_array($child['id'], $this->activeEntries)) {
220
                        $entryArray['ITEM_STATE'] = 'ACT';
221
                    }
222
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
223
                }
224
            }
225
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
226
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
227
        }
228
        return $entryArray;
229
    }
230
231
    /**
232
     * If $entry references an external METS file (as mptr),
233
     * try to resolve its database UID and return an updated $entry.
234
     *
235
     * This is so that when linking from a child document back to its parent,
236
     * that link is via UID, so that subsequently the parent's TOC is built from database.
237
     *
238
     * @param array $entry
239
     * @return array
240
     */
241
    protected function resolveMenuEntry($entry)
242
    {
243
        // If the menu entry points to the parent document,
244
        // resolve to the parent UID set on indexation.
245
        $doc = $this->document->getDoc();
246
        if (
247
            $doc instanceof MetsDocument
248
            && $entry['points'] === $doc->parentHref
249
            && !empty($this->document->getPartof())
250
        ) {
251
            unset($entry['points']);
252
            $entry['targetUid'] = $this->document->getPartof();
253
        }
254
255
        return $entry;
256
    }
257
}
258