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 (#715)
by Alexander
03:39
created

ToolboxController::searchindocumenttool()   C

Complexity

Conditions 12
Paths 25

Size

Total Lines 54
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
c 2
b 0
f 0
dl 0
loc 54
rs 6.9666
cc 12
nc 25
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Configuration\ExtensionConfiguration;
16
use TYPO3\CMS\Core\Utility\MathUtility;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19
class ToolboxController extends AbstractController
20
{
21
    /**
22
     * The main method of the plugin
23
     *
24
     * @return void
25
     */
26
    public function mainAction()
27
    {
28
        $requestData = GeneralUtility::_GPmerged('tx_dlf');
29
        unset($requestData['__referrer'], $requestData['__trustedProperties']);
30
31
        $this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf');
32
33
        // Load current document.
34
        $this->loadDocument($requestData);
35
36
        $requestData['double'] = MathUtility::forceIntegerInRange($requestData['double'], 0, 1, 0);
37
        $this->view->assign('double', $requestData['double']);
38
39
        $tools = explode(',', $this->settings['tools']);
40
        // Add the tools to the toolbox.
41
        foreach ($tools as $tool) {
42
            $tool = trim(str_replace('tx_dlf_', '', $tool));
43
            $this->$tool($requestData);
44
            $this->view->assign($tool, true);
45
        }
46
    }
47
48
    /**
49
     * Renders the annotation tool
50
     * @param $requestData
51
     * @return void
52
     */
53
    public function annotationtool($requestData)
54
    {
55
        if (
56
            $this->doc === null
57
            || $this->doc->numPages < 1
58
        ) {
59
            // Quit without doing anything if required variables are not set.
60
            return;
61
        } else {
62
            if (!empty($requestData['logicalPage'])) {
63
                $requestData['page'] = $this->doc->getPhysicalPage($requestData['logicalPage']);
64
                // The logical page parameter should not appear again
65
                unset($requestData['logicalPage']);
66
            }
67
            // Set default values if not set.
68
            // $requestData['page'] may be integer or string (physical structure @ID)
69
            if (
70
                (int) $requestData['page'] > 0
71
                || empty($requestData['page'])
72
            ) {
73
                $requestData['page'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int) $requestData['page'], 1, $this->doc->numPages, 1);
74
            } else {
75
                $requestData['page'] = array_search($requestData['page'], $this->doc->physicalStructure);
76
            }
77
        }
78
79
        $annotationContainers = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$requestData['page']]]['annotationContainers'];
80
        if (
81
            $annotationContainers != null
82
            && sizeof($annotationContainers) > 0
83
        ) {
84
            $this->view->assign('annotationTool', true);
85
        } else {
86
            $this->view->assign('annotationTool', false);
87
        }
88
    }
89
90
    /**
91
     * Renders the fulltext download tool
92
     * @param $requestData
93
     * @return void
94
     */
95
    public function fulltextdownloadtool($requestData)
96
    {
97
        if (
98
            $this->doc === null
99
            || $this->doc->numPages < 1
100
            || empty($this->extConf['fileGrpFulltext'])
101
        ) {
102
            // Quit without doing anything if required variables are not set.
103
            return;
104
        } else {
105
            if (!empty($requestData['logicalPage'])) {
106
                $requestData['page'] = $this->doc->getPhysicalPage($requestData['logicalPage']);
107
                // The logical page parameter should not appear again
108
                unset($requestData['logicalPage']);
109
            }
110
            // Set default values if not set.
111
            // $requestData['page'] may be integer or string (physical structure @ID)
112
            if (
113
                (int) $requestData['page'] > 0
114
                || empty($requestData['page'])
115
            ) {
116
                $requestData['page'] = MathUtility::forceIntegerInRange((int) $requestData['page'], 1, $this->doc->numPages, 1);
117
            } else {
118
                $requestData['page'] = array_search($requestData['page'], $this->doc->physicalStructure);
119
            }
120
        }
121
        // Get text download.
122
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
123
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
124
            if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$requestData['page']]]['files'][$fileGrpFulltext])) {
125
                $fullTextFile = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$requestData['page']]]['files'][$fileGrpFulltext];
126
                break;
127
            }
128
        }
129
        if (!empty($fullTextFile)) {
130
            $this->view->assign('fulltextDownload', true);
131
        } else {
132
            $this->view->assign('fulltextDownload', false);
133
        }
134
    }
135
136
    /**
137
     * Renders the fulltext tool
138
     * @param $requestData
139
     * @return void
140
     */
141
    public function fulltexttool($requestData)
142
    {
143
        if (
144
            $this->doc === null
145
            || $this->doc->numPages < 1
146
            || empty($this->extConf['fileGrpFulltext'])
147
        ) {
148
            // Quit without doing anything if required variables are not set.
149
            return;
150
        } else {
151
            if (!empty($requestData['logicalPage'])) {
152
                $requestData['page'] = $this->doc->getPhysicalPage($requestData['logicalPage']);
153
                // The logical page parameter should not appear again
154
                unset($requestData['logicalPage']);
155
            }
156
            // Set default values if not set.
157
            // $requestData['page'] may be integer or string (physical structure @ID)
158
            if (
159
                (int) $requestData['page'] > 0
160
                || empty($requestData['page'])
161
            ) {
162
                $requestData['page'] = MathUtility::forceIntegerInRange((int) $requestData['page'], 1, $this->doc->numPages, 1);
163
            } else {
164
                $requestData['page'] = array_search($requestData['page'], $this->doc->physicalStructure);
165
            }
166
        }
167
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
168
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
169
            if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$requestData['page']]]['files'][$fileGrpFulltext])) {
170
                $fullTextFile = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$requestData['page']]]['files'][$fileGrpFulltext];
171
                break;
172
            }
173
        }
174
        if (!empty($fullTextFile)) {
175
            $this->view->assign('fulltext', true);
176
            $this->view->assign('activateFullTextInitially', MathUtility::forceIntegerInRange($this->settings['activateFullTextInitially'], 0, 1, 0));
177
        } else {
178
            $this->view->assign('fulltext', false);
179
        }
180
    }
181
182
    /**
183
     * Renders the image download tool
184
     * @param $requestData
185
     * @return void
186
     */
187
    public function imagedownloadtool($requestData)
188
    {
189
        if (
190
            $this->doc === null
191
            || $this->doc->numPages < 1
192
            || empty($this->settings['fileGrpsImageDownload'])
193
        ) {
194
            // Quit without doing anything if required variables are not set.
195
            return;
196
        } else {
197
            if (!empty($requestData['logicalPage'])) {
198
                $requestData['page'] = $this->doc->getPhysicalPage($requestData['logicalPage']);
199
                // The logical page parameter should not appear again
200
                unset($requestData['logicalPage']);
201
            }
202
            // Set default values if not set.
203
            // $requestData['page'] may be integer or string (physical structure @ID)
204
            if (
205
                (int) $requestData['page'] > 0
206
                || empty($requestData['page'])
207
            ) {
208
                $requestData['page'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int) $requestData['page'], 1, $this->doc->numPages, 1);
209
            } else {
210
                $requestData['page'] = array_search($requestData['page'], $this->doc->physicalStructure);
211
            }
212
        }
213
        $imageArray = [];
214
        // Get left or single page download.
215
        $imageArray[0] = $this->getImage($requestData['page']);
216
        if ($requestData['double'] == 1) {
217
            $imageArray[1] = $this->getImage($requestData['page'] + 1);
218
        }
219
        $this->view->assign('imageDownload', $imageArray);
220
    }
221
222
    /**
223
     * Get image's URL and MIME type
224
     *
225
     * @access protected
226
     *
227
     * @param int $page: Page number
228
     *
229
     * @return array Array of image links and image format information
230
     */
231
    protected function getImage($page)
232
    {
233
        $image = [];
234
        // Get @USE value of METS fileGrp.
235
        $fileGrps = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->settings['fileGrpsImageDownload']);
236
        while ($fileGrp = @array_pop($fileGrps)) {
237
            // Get image link.
238
            if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrp])) {
239
                $image['url'] = $this->doc->getDownloadLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrp]);
240
                $image['mimetype'] = $this->doc->getFileMimeType($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrp]);
241
                switch ($image['mimetype']) {
242
                    case 'image/jpeg':
243
                        $mimetypeLabel = ' (JPG)';
244
                        break;
245
                    case 'image/tiff':
246
                        $mimetypeLabel = ' (TIFF)';
247
                        break;
248
                    default:
249
                        $mimetypeLabel = '';
250
                }
251
                $image['mimetypeLabel'] = $mimetypeLabel;
252
                break;
253
            } else {
254
                $this->logger->warning('File not found in fileGrp "' . $fileGrp . '"');
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

254
                $this->logger->/** @scrutinizer ignore-call */ 
255
                               warning('File not found in fileGrp "' . $fileGrp . '"');

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...
255
            }
256
        }
257
        return $image;
258
    }
259
260
    /**
261
     * Renders the image manipulation tool
262
     * @param $requestData
263
     */
264
    public function imagemanipulationtool($requestData)
265
    {
266
        // Set parent element for initialization.
267
        $parentContainer = !empty($this->settings['parentContainer']) ? $this->settings['parentContainer'] : '.tx-dlf-imagemanipulationtool';
268
269
        $this->view->assign('imageManipulation', true);
270
        $this->view->assign('parentContainer', $parentContainer);
271
    }
272
273
    /**
274
     * Renders the PDF download tool
275
     * @param $requestData
276
     * @return void
277
     */
278
    public function pdfdownloadtool($requestData)
279
    {
280
        if (
281
            $this->doc === null
282
            || $this->doc->numPages < 1
283
            || empty($this->extConf['fileGrpDownload'])
284
        ) {
285
            // Quit without doing anything if required variables are not set.
286
            return;
287
        } else {
288
            if (!empty($requestData['logicalPage'])) {
289
                $requestData['page'] = $this->doc->getPhysicalPage($requestData['logicalPage']);
290
                // The logical page parameter should not appear again
291
                unset($requestData['logicalPage']);
292
            }
293
            // Set default values if not set.
294
            // $requestData['page'] may be integer or string (physical structure @ID)
295
            if (
296
                (int) $requestData['page'] > 0
297
                || empty($requestData['page'])
298
            ) {
299
                $requestData['page'] = MathUtility::forceIntegerInRange((int) $requestData['page'], 1, $this->doc->numPages, 1);
300
            } else {
301
                $requestData['page'] = array_search($requestData['page'], $this->doc->physicalStructure);
302
            }
303
        }
304
        // Get single page downloads.
305
        $this->view->assign('pageLinks', $this->getPageLink($requestData));
306
        // Get work download.
307
        $this->view->assign('workLink', $this->getWorkLink());
308
    }
309
310
    /**
311
     * Get page's download link
312
     *
313
     * @access protected
314
     *
315
     * @return array Link to downloadable page
316
     */
317
    protected function getPageLink($requestData)
318
    {
319
        $page1Link = '';
320
        $page2Link = '';
321
        $pageLinkArray = [];
322
        $pageNumber = $requestData['page'];
323
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']);
324
        // Get image link.
325
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
326
            if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber]]['files'][$fileGrpDownload])) {
327
                $page1Link = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber]]['files'][$fileGrpDownload]);
328
                // Get second page, too, if double page view is activated.
329
                if (
330
                    $requestData['double']
331
                    && $pageNumber < $this->doc->numPages
332
                    && !empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload])
333
                ) {
334
                    $page2Link = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload]);
335
                }
336
                break;
337
            }
338
        }
339
        if (
340
            empty($page1Link)
341
            && empty($page2Link)
342
        ) {
343
            $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"');
344
        }
345
346
        if (!empty($page1Link)) {
347
            $pageLinkArray[0] = $page1Link;
348
        }
349
        if (!empty($page2Link)) {
350
            $pageLinkArray[1] = $page2Link;
351
        }
352
        return $pageLinkArray;
353
    }
354
355
    /**
356
     * Get work's download link
357
     *
358
     * @access protected
359
     *
360
     * @return string Link to downloadable work
361
     */
362
    protected function getWorkLink()
363
    {
364
        $workLink = '';
365
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']);
366
        // Get work link.
367
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
368
            if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[0]]['files'][$fileGrpDownload])) {
369
                $workLink = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[0]]['files'][$fileGrpDownload]);
370
                break;
371
            } else {
372
                $details = $this->doc->getLogicalStructure($this->doc->toplevelId);
373
                if (!empty($details['files'][$fileGrpDownload])) {
374
                    $workLink = $this->doc->getFileLocation($details['files'][$fileGrpDownload]);
375
                    break;
376
                }
377
            }
378
        }
379
        if (!empty($workLink)) {
380
            $workLink = $workLink;
381
        } else {
382
            $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"');
383
        }
384
        return $workLink;
385
    }
386
387
    /**
388
     * Renders the searchInDocument tool
389
     * @param $requestData
390
     * @return void
391
     */
392
    public function searchindocumenttool($requestData)
393
    {
394
        if (
395
            $this->doc === null
396
            || $this->doc->numPages < 1
397
            || empty($this->extConf['fileGrpFulltext'])
398
            || empty($this->settings['solrcore'])
399
        ) {
400
            // Quit without doing anything if required variables are not set.
401
            return;
402
        } else {
403
            if (!empty($requestData['logicalPage'])) {
404
                $requestData['page'] = $this->doc->getPhysicalPage($requestData['logicalPage']);
405
                // The logical page parameter should not appear again
406
                unset($requestData['logicalPage']);
407
            }
408
            // Set default values if not set.
409
            // $requestData['page'] may be integer or string (physical structure @ID)
410
            if (
411
                (int) $requestData['page'] > 0
412
                || empty($requestData['page'])
413
            ) {
414
                $requestData['page'] = MathUtility::forceIntegerInRange((int) $requestData['page'], 1, $this->doc->numPages, 1);
415
            } else {
416
                $requestData['page'] = array_search($requestData['page'], $this->doc->physicalStructure);
417
            }
418
        }
419
420
        // Quit if no fulltext file is present
421
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->settings['fileGrpFulltext']);
422
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
423
            if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$requestData['page']]]['files'][$fileGrpFulltext])) {
424
                $fullTextFile = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$requestData['page']]]['files'][$fileGrpFulltext];
425
                break;
426
            }
427
        }
428
        if (empty($fullTextFile)) {
429
            return;
430
        }
431
432
        // Fill markers.
433
        $viewArray = [
434
            'ACTION_URL' => $this->getActionUrl(),
435
            'LABEL_QUERY_URL' => $this->settings['queryInputName'],
436
            'LABEL_START' => $this->settings['startInputName'],
437
            'LABEL_ID' => $this->settings['idInputName'],
438
            'LABEL_PAGE_URL' => $this->settings['pageInputName'],
439
            'LABEL_HIGHLIGHT_WORD' => $this->settings['highlightWordInputName'],
440
            'LABEL_ENCRYPTED' => $this->settings['encryptedInputName'],
441
            'CURRENT_DOCUMENT' => $this->getCurrentDocumentId(),
442
            'SOLR_ENCRYPTED' => $this->getEncryptedCoreName() ? : ''
443
        ];
444
445
        $this->view->assign('searchInDocument', $viewArray);
446
    }
447
448
    /**
449
     * Get the action url for search form
450
     *
451
     * @access protected
452
     *
453
     * @return string with action url for search form
454
     */
455
    protected function getActionUrl()
456
    {
457
        // Configure @action URL for form.
458
        $uri = $this->uriBuilder->reset()
459
            ->setTargetPageUid($GLOBALS['TSFE']->id)
460
            ->setCreateAbsoluteUri(true)
461
            ->build();
462
463
        $actionUrl = $uri;
464
465
        if (!empty($this->settings['searchUrl'])) {
466
            $actionUrl = $this->settings['searchUrl'];
467
        }
468
        return $actionUrl;
469
    }
470
471
    /**
472
     * Get current document id
473
     *
474
     * @access protected
475
     *
476
     * @return string with current document id
477
     */
478
    protected function getCurrentDocumentId()
479
    {
480
        $id = $this->doc->uid;
481
482
        if (!empty($this->settings['documentIdUrlSchema'])) {
483
            $arr = explode('*', $this->settings['documentIdUrlSchema']);
484
485
            if (count($arr) == 2) {
486
                $id = explode($arr[0], $id)[0];
487
            } else if (count($arr) == 3) {
488
                $sub = substr($id, strpos($id, $arr[0]) + strlen($arr[0]), strlen($id));
489
                $id = substr($sub, 0, strpos($sub, $arr[2]));
490
            }
491
        }
492
        return $id;
493
    }
494
495
    /**
496
     * Get the encrypted Solr core name
497
     *
498
     * @access protected
499
     *
500
     * @return string with encrypted core name
501
     */
502
    protected function getEncryptedCoreName()
503
    {
504
        // Get core name.
505
        $name = Helper::getIndexNameFromUid($this->settings['solrcore'], 'tx_dlf_solrcores');
506
        // Encrypt core name.
507
        if (!empty($name)) {
508
            $name = Helper::encrypt($name);
509
        }
510
        return $name;
511
    }
512
}
513