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
Push — master ( 4635ba...67cfe3 )
by
unknown
03:40 queued 12s
created

AbstractController::setDefaultPage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 10
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\Domain\Model\Document;
17
use Kitodo\Dlf\Domain\Repository\DocumentRepository;
18
use Psr\Log\LoggerAwareInterface;
19
use Psr\Log\LoggerAwareTrait;
20
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
21
use TYPO3\CMS\Core\Database\ConnectionPool;
22
use TYPO3\CMS\Core\Localization\LanguageService;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Core\Utility\MathUtility;
25
26
/**
27
 * Abstract controller class for most of the plugin controller.
28
 *
29
 * @author Sebastian Meyer <[email protected]>
30
 * @package TYPO3
31
 * @subpackage dlf
32
 * @access public
33
 */
34
abstract class AbstractController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController implements LoggerAwareInterface
35
{
36
    use LoggerAwareTrait;
37
38
    /**
39
     * @var DocumentRepository
40
     */
41
    protected $documentRepository;
42
43
    /**
44
     * @param DocumentRepository $documentRepository
45
     */
46
    public function injectDocumentRepository(DocumentRepository $documentRepository)
47
    {
48
        $this->documentRepository = $documentRepository;
49
    }
50
51
    /**
52
     * This holds the current document
53
     *
54
     * @var \Kitodo\Dlf\Domain\Model\Document
55
     * @access protected
56
     */
57
    protected $document;
58
59
    /**
60
     * @var array
61
     * @access protected
62
     */
63
    protected $extConf;
64
65
    /**
66
     * This holds the request parameter
67
     *
68
     * @var array
69
     * @access protected
70
     */
71
    protected $requestData;
72
73
    /**
74
     * This holds some common data for the fluid view
75
     *
76
     * @var array
77
     * @access protected
78
     */
79
    protected $viewData;
80
81
    /**
82
     * Initialize the plugin controller
83
     *
84
     * @access protected
85
     * @return void
86
     */
87
    protected function initialize()
88
    {
89
        $this->requestData = GeneralUtility::_GPmerged('tx_dlf');
90
91
        // Sanitize user input to prevent XSS attacks.
92
        $this->sanitizeRequestData();
93
94
        // Get extension configuration.
95
        $this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf');
96
97
        $this->viewData = [
98
            'pageUid' => $GLOBALS['TSFE']->id,
99
            'uniqueId'=> uniqid(),
100
            'requestData' => $this->requestData
101
        ];
102
    }
103
104
    /**
105
     * Loads the current document into $this->document
106
     *
107
     * @access protected
108
     *
109
     * @param  int $documentId: The document's UID (fallback: $this->requestData[id])
110
     *
111
     * @return void
112
     */
113
    protected function loadDocument(int $documentId = 0)
114
    {
115
        // Get document ID from request data if not passed as parameter.
116
        if ($documentId === 0 && !empty($this->requestData['id'])) {
117
            $documentId = $this->requestData['id'];
118
        }
119
120
        // Try to get document format from database
121
        if (!empty($documentId)) {
122
123
            $doc = null;
124
125
            if (MathUtility::canBeInterpretedAsInteger($documentId)) {
126
                // find document from repository by uid
127
                $this->document = $this->documentRepository->findOneByIdAndSettings($documentId);
128
                if ($this->document) {
129
                    $doc = Doc::getInstance($this->document->getLocation(), $this->settings, true);
130
                } else {
131
                    $this->logger->error('Invalid UID "' . $documentId . '" or PID "' . $this->settings['storagePid'] . '" for document loading');
132
                }
133
            } else if (GeneralUtility::isValidUrl($documentId)) {
134
135
                $doc = Doc::getInstance($documentId, $this->settings, true);
136
137
                if ($doc !== null) {
138
                    if ($doc->recordId) {
139
                        $this->document = $this->documentRepository->findOneByRecordId($doc->recordId);
0 ignored issues
show
Bug introduced by
The method findOneByRecordId() does not exist on Kitodo\Dlf\Domain\Repository\DocumentRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

139
                        /** @scrutinizer ignore-call */ 
140
                        $this->document = $this->documentRepository->findOneByRecordId($doc->recordId);
Loading history...
140
                    }
141
142
                    if ($this->document === null) {
143
                        // create new dummy Document object
144
                        $this->document = GeneralUtility::makeInstance(Document::class);
145
                    }
146
147
                    // Make sure configuration PID is set when applicable
148
                    if ($doc->cPid == 0) {
149
                        $doc->cPid = max(intval($this->settings['storagePid']), 0);
150
                    }
151
152
                    $this->document->setLocation($documentId);
153
                } else {
154
                    $this->logger->error('Invalid location given "' . $documentId . '" for document loading');
155
                }
156
            }
157
158
            if ($this->document !== null && $doc !== null) {
159
                $this->document->setDoc($doc);
160
            }
161
162
        } elseif (!empty($this->requestData['recordId'])) {
163
164
            $this->document = $this->documentRepository->findOneByRecordId($this->requestData['recordId']);
165
166
            if ($this->document !== null) {
167
                $doc = Doc::getInstance($this->document->getLocation(), $this->settings, true);
0 ignored issues
show
Bug introduced by
The method getLocation() 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

167
                $doc = Doc::getInstance($this->document->/** @scrutinizer ignore-call */ getLocation(), $this->settings, true);

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...
168
                if ($this->document !== null && $doc !== null) {
169
                    $this->document->setDoc($doc);
0 ignored issues
show
Bug introduced by
The method setDoc() 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

169
                    $this->document->/** @scrutinizer ignore-call */ 
170
                                     setDoc($doc);

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...
170
                } else {
171
                    $this->logger->error('Failed to load document with record ID "' . $this->requestData['recordId'] . '"');
172
                }
173
            }
174
        } else {
175
            $this->logger->error('Invalid ID "' . $documentId . '" or PID "' . $this->settings['storagePid'] . '" for document loading');
176
        }
177
    }
178
179
    /**
180
     * Checks if doc is missing or is empty (no pages)
181
     *
182
     * @return boolean
183
     */
184
    protected function isDocMissingOrEmpty()
185
    {
186
        return $this->isDocMissing() || $this->document->getDoc()->numPages < 1;
187
    }
188
189
    /**
190
     * Checks if doc is missing
191
     *
192
     * @return boolean
193
     */
194
    protected function isDocMissing()
195
    {
196
        return $this->document === null || $this->document->getDoc() === null;
197
    }
198
199
    /**
200
     * Returns the LanguageService
201
     *
202
     * @return LanguageService
203
     */
204
    protected function getLanguageService(): LanguageService
205
    {
206
        return $GLOBALS['LANG'];
207
    }
208
209
    /**
210
     * Safely gets Parameters from request
211
     * if they exist
212
     *
213
     * @param string $parameterName
214
     *
215
     * @return null|string|array
216
     */
217
    protected function getParametersSafely($parameterName)
218
    {
219
        if ($this->request->hasArgument($parameterName)) {
220
            return $this->request->getArgument($parameterName);
221
        }
222
        return null;
223
    }
224
225
    /**
226
     * Sanitize input variables.
227
     *
228
     * @access protected
229
     *
230
     * @return void
231
     */
232
    protected function sanitizeRequestData()
233
    {
234
        // tx_dlf[id] may only be an UID or URI.
235
        if (
236
            !empty($this->requestData['id'])
237
            && !MathUtility::canBeInterpretedAsInteger($this->requestData['id'])
238
            && !GeneralUtility::isValidUrl($this->requestData['id'])
239
        ) {
240
            $this->logger->warning('Invalid ID or URI "' . $this->requestData['id'] . '" for document loading');
241
            unset($this->requestData['id']);
242
        }
243
244
        // tx_dlf[page] may only be a positive integer or valid XML ID.
245
        if (
246
            !empty($this->requestData['page'])
247
            && !MathUtility::canBeInterpretedAsInteger($this->requestData['page'])
248
            && !Helper::isValidXmlId($this->requestData['page'])
249
        ) {
250
            $this->requestData['page'] = 1;
251
        }
252
253
        // tx_dlf[double] may only be 0 or 1.
254
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
255
    }
256
257
    /**
258
     * Sets page value.
259
     *
260
     * @access protected
261
     *
262
     * @return void
263
     */
264
    protected function setPage() {
265
        if (!empty($this->requestData['logicalPage'])) {
266
            $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
267
            // The logical page parameter should not appear again
268
            unset($this->requestData['logicalPage']);
269
        }
270
271
        $this->setDefaultPage();
272
    }
273
274
    /**
275
     * Sets default page value.
276
     *
277
     * @access protected
278
     *
279
     * @return void
280
     */
281
    protected function setDefaultPage() {
282
        // Set default values if not set.
283
        // $this->requestData['page'] may be integer or string (physical structure @ID)
284
        if (
285
            (int) $this->requestData['page'] > 0
286
            || empty($this->requestData['page'])
287
        ) {
288
            $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
289
        } else {
290
            $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
291
        }
292
    }
293
294
    /**
295
     * This is the constructor
296
     *
297
     * @access public
298
     *
299
     * @return void
300
     */
301
    public function __construct()
302
    {
303
        $this->initialize();
304
    }
305
}
306