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 ( 5e069e...ee43a7 )
by
unknown
03:56
created

AbstractController::setPage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
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\Domain\Model\Document;
16
use Kitodo\Dlf\Domain\Repository\DocumentRepository;
17
use Psr\Log\LoggerAwareInterface;
18
use Psr\Log\LoggerAwareTrait;
19
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
20
use TYPO3\CMS\Core\Localization\LanguageService;
21
use TYPO3\CMS\Core\Pagination\PaginationInterface;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Core\Utility\MathUtility;
24
use TYPO3\CMS\Core\Pagination\PaginatorInterface;
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
     * Configure URL for proxy.
181
     *
182
     * @access protected
183
     *
184
     * @param string $url URL for proxy configuration
185
     *
186
     * @return void
187
     */
188
    protected function configureProxyUrl(&$url) {
189
        $this->uriBuilder->reset()
190
            ->setTargetPageUid($GLOBALS['TSFE']->id)
191
            ->setCreateAbsoluteUri(!empty($this->settings['forceAbsoluteUrl']))
192
            ->setArguments([
193
                'eID' => 'tx_dlf_pageview_proxy',
194
                'url' => $url,
195
                'uHash' => GeneralUtility::hmac($url, 'PageViewProxy')
196
                ])
197
            ->build();
198
    }
199
200
    /**
201
     * Checks if doc is missing or is empty (no pages)
202
     *
203
     * @return boolean
204
     */
205
    protected function isDocMissingOrEmpty()
206
    {
207
        return $this->isDocMissing() || $this->document->getDoc()->numPages < 1;
208
    }
209
210
    /**
211
     * Checks if doc is missing
212
     *
213
     * @return boolean
214
     */
215
    protected function isDocMissing()
216
    {
217
        return $this->document === null || $this->document->getDoc() === null;
218
    }
219
220
    /**
221
     * Returns the LanguageService
222
     *
223
     * @return LanguageService
224
     */
225
    protected function getLanguageService(): LanguageService
226
    {
227
        return $GLOBALS['LANG'];
228
    }
229
230
    /**
231
     * Safely gets Parameters from request
232
     * if they exist
233
     *
234
     * @param string $parameterName
235
     *
236
     * @return null|string|array
237
     */
238
    protected function getParametersSafely($parameterName)
239
    {
240
        if ($this->request->hasArgument($parameterName)) {
241
            return $this->request->getArgument($parameterName);
242
        }
243
        return null;
244
    }
245
246
    /**
247
     * Sanitize input variables.
248
     *
249
     * @access protected
250
     *
251
     * @return void
252
     */
253
    protected function sanitizeRequestData()
254
    {
255
        // tx_dlf[id] may only be an UID or URI.
256
        if (
257
            !empty($this->requestData['id'])
258
            && !MathUtility::canBeInterpretedAsInteger($this->requestData['id'])
259
            && !GeneralUtility::isValidUrl($this->requestData['id'])
260
        ) {
261
            $this->logger->warning('Invalid ID or URI "' . $this->requestData['id'] . '" for document loading');
262
            unset($this->requestData['id']);
263
        }
264
265
        // tx_dlf[page] may only be a positive integer or valid XML ID.
266
        if (
267
            !empty($this->requestData['page'])
268
            && !MathUtility::canBeInterpretedAsInteger($this->requestData['page'])
269
            && !Helper::isValidXmlId($this->requestData['page'])
0 ignored issues
show
Bug introduced by
The type Kitodo\Dlf\Controller\Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
270
        ) {
271
            $this->requestData['page'] = 1;
272
        }
273
274
        // tx_dlf[double] may only be 0 or 1.
275
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
276
    }
277
278
    /**
279
     * Sets page value.
280
     *
281
     * @access protected
282
     *
283
     * @return void
284
     */
285
    protected function setPage() {
286
        if (!empty($this->requestData['logicalPage'])) {
287
            $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
288
            // The logical page parameter should not appear again
289
            unset($this->requestData['logicalPage']);
290
        }
291
292
        $this->setDefaultPage();
293
    }
294
295
    /**
296
     * Sets default page value.
297
     *
298
     * @access protected
299
     *
300
     * @return void
301
     */
302
    protected function setDefaultPage() {
303
        // Set default values if not set.
304
        // $this->requestData['page'] may be integer or string (physical structure @ID)
305
        if (
306
            (int) $this->requestData['page'] > 0
307
            || empty($this->requestData['page'])
308
        ) {
309
            $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
310
        } else {
311
            $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
312
        }
313
    }
314
315
    /**
316
     * This is the constructor
317
     *
318
     * @access public
319
     *
320
     * @return void
321
     */
322
    public function __construct()
323
    {
324
        $this->initialize();
325
    }
326
327
    /**
328
     * build simple pagination
329
     *
330
     * @param PaginationInterface $pagination
331
     * @param PaginatorInterface $paginator
332
     * @return array
333
     */
334
    protected function buildSimplePagination(PaginationInterface $pagination, PaginatorInterface $paginator)
335
    {
336
        $firstPage = $pagination->getFirstPageNumber();
337
        $lastPage = $pagination->getLastPageNumber();
338
339
        return [
340
            'lastPageNumber' => $lastPage,
341
            'firstPageNumber' => $firstPage,
342
            'nextPageNumber' => ($pagination->getNextPageNumber()),
343
            'previousPageNumber' => $pagination->getPreviousPageNumber(),
344
            'startRecordNumber' => $pagination->getStartRecordNumber(),
345
            'endRecordNumber' => $pagination->getEndRecordNumber(),
346
            'currentPageNumber' => $paginator->getCurrentPageNumber(),
347
            'pages' => range($firstPage, $lastPage)
348
        ];
349
    }
350
}
351