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
07:45
created

ToolboxController   F

Complexity

Total Complexity 89

Size/Duplication

Total Lines 481
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 232
c 5
b 0
f 0
dl 0
loc 481
rs 2
wmc 89

13 Methods

Rating   Name   Duplication   Size   Complexity  
A imagemanipulationtool() 0 7 2
A getImage() 0 27 5
B pdfdownloadtool() 0 31 8
B fulltextdownloadtool() 0 38 10
A mainAction() 0 16 2
B fulltexttool() 0 38 10
A getWorkLink() 0 23 5
B getPageLink() 0 36 10
B imagedownloadtool() 0 33 8
C searchindocumenttool() 0 53 12
B annotationtool() 0 34 8
A getEncryptedCoreName() 0 9 2
B getCurrentDocumentId() 0 27 7

How to fix   Complexity   

Complex Class

Complex classes like ToolboxController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ToolboxController, and based on these observations, apply Extract Interface, too.

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
        $this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf');
29
30
        // Load current document.
31
        $this->loadDocument($this->requestData);
32
33
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
34
        $this->view->assign('double', $this->requestData['double']);
35
36
        $tools = explode(',', $this->settings['tools']);
37
        // Add the tools to the toolbox.
38
        foreach ($tools as $tool) {
39
            $tool = trim(str_replace('tx_dlf_', '', $tool));
40
            $this->$tool();
41
            $this->view->assign($tool, true);
42
        }
43
    }
44
45
    /**
46
     * Renders the annotation tool
47
     *
48
     * @return void
49
     */
50
    public function annotationtool()
51
    {
52
        if (
53
            $this->document === null
54
            || $this->document->getDoc()->numPages < 1
55
        ) {
56
            // Quit without doing anything if required variables are not set.
57
            return;
58
        } else {
59
            if (!empty($this->requestData['logicalPage'])) {
60
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
61
                // The logical page parameter should not appear again
62
                unset($this->requestData['logicalPage']);
63
            }
64
            // Set default values if not set.
65
            // $this->requestData['page'] may be integer or string (physical structure @ID)
66
            if (
67
                (int) $this->requestData['page'] > 0
68
                || empty($this->requestData['page'])
69
            ) {
70
                $this->requestData['page'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
71
            } else {
72
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
73
            }
74
        }
75
76
        $annotationContainers = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['annotationContainers'];
77
        if (
78
            $annotationContainers != null
79
            && sizeof($annotationContainers) > 0
80
        ) {
81
            $this->view->assign('annotationTool', true);
82
        } else {
83
            $this->view->assign('annotationTool', false);
84
        }
85
    }
86
87
    /**
88
     * Renders the fulltext download tool
89
     *
90
     * @return void
91
     */
92
    public function fulltextdownloadtool()
93
    {
94
        if (
95
            $this->document === null
96
            || $this->document->getDoc()->numPages < 1
97
            || empty($this->extConf['fileGrpFulltext'])
98
        ) {
99
            // Quit without doing anything if required variables are not set.
100
            return;
101
        } else {
102
            if (!empty($this->requestData['logicalPage'])) {
103
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
104
                // The logical page parameter should not appear again
105
                unset($this->requestData['logicalPage']);
106
            }
107
            // Set default values if not set.
108
            // $this->requestData['page'] may be integer or string (physical structure @ID)
109
            if (
110
                (int) $this->requestData['page'] > 0
111
                || empty($this->requestData['page'])
112
            ) {
113
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
114
            } else {
115
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
116
            }
117
        }
118
        // Get text download.
119
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
120
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
121
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext])) {
122
                $fullTextFile = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext];
123
                break;
124
            }
125
        }
126
        if (!empty($fullTextFile)) {
127
            $this->view->assign('fulltextDownload', true);
128
        } else {
129
            $this->view->assign('fulltextDownload', false);
130
        }
131
    }
132
133
    /**
134
     * Renders the fulltext tool
135
     *
136
     * @return void
137
     */
138
    public function fulltexttool()
139
    {
140
        if (
141
            $this->document === null
142
            || $this->document->getDoc()->numPages < 1
143
            || empty($this->extConf['fileGrpFulltext'])
144
        ) {
145
            // Quit without doing anything if required variables are not set.
146
            return;
147
        } else {
148
            if (!empty($this->requestData['logicalPage'])) {
149
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
150
                // The logical page parameter should not appear again
151
                unset($this->requestData['logicalPage']);
152
            }
153
            // Set default values if not set.
154
            // $this->requestData['page'] may be integer or string (physical structure @ID)
155
            if (
156
                (int) $this->requestData['page'] > 0
157
                || empty($this->requestData['page'])
158
            ) {
159
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
160
            } else {
161
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
162
            }
163
        }
164
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
165
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
166
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext])) {
167
                $fullTextFile = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext];
168
                break;
169
            }
170
        }
171
        if (!empty($fullTextFile)) {
172
            $this->view->assign('fulltext', true);
173
            $this->view->assign('activateFullTextInitially', MathUtility::forceIntegerInRange($this->settings['activateFullTextInitially'], 0, 1, 0));
174
        } else {
175
            $this->view->assign('fulltext', false);
176
        }
177
    }
178
179
    /**
180
     * Renders the image download tool
181
     *
182
     * @return void
183
     */
184
    public function imagedownloadtool()
185
    {
186
        if (
187
            $this->document === null
188
            || $this->document->getDoc()->numPages < 1
189
            || empty($this->settings['fileGrpsImageDownload'])
190
        ) {
191
            // Quit without doing anything if required variables are not set.
192
            return;
193
        } else {
194
            if (!empty($this->requestData['logicalPage'])) {
195
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
196
                // The logical page parameter should not appear again
197
                unset($this->requestData['logicalPage']);
198
            }
199
            // Set default values if not set.
200
            // $this->requestData['page'] may be integer or string (physical structure @ID)
201
            if (
202
                (int) $this->requestData['page'] > 0
203
                || empty($this->requestData['page'])
204
            ) {
205
                $this->requestData['page'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
206
            } else {
207
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
208
            }
209
        }
210
        $imageArray = [];
211
        // Get left or single page download.
212
        $imageArray[0] = $this->getImage($this->requestData['page']);
213
        if ($this->requestData['double'] == 1) {
214
            $imageArray[1] = $this->getImage($this->requestData['page'] + 1);
215
        }
216
        $this->view->assign('imageDownload', $imageArray);
217
    }
218
219
    /**
220
     * Get image's URL and MIME type
221
     *
222
     * @access protected
223
     *
224
     * @param int $page: Page number
225
     *
226
     * @return array Array of image links and image format information
227
     */
228
    protected function getImage($page)
229
    {
230
        $image = [];
231
        // Get @USE value of METS fileGrp.
232
        $fileGrps = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->settings['fileGrpsImageDownload']);
233
        while ($fileGrp = @array_pop($fileGrps)) {
234
            // Get image link.
235
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrp])) {
236
                $image['url'] = $this->document->getDoc()->getDownloadLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrp]);
237
                $image['mimetype'] = $this->document->getDoc()->getFileMimeType($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrp]);
238
                switch ($image['mimetype']) {
239
                    case 'image/jpeg':
240
                        $mimetypeLabel = ' (JPG)';
241
                        break;
242
                    case 'image/tiff':
243
                        $mimetypeLabel = ' (TIFF)';
244
                        break;
245
                    default:
246
                        $mimetypeLabel = '';
247
                }
248
                $image['mimetypeLabel'] = $mimetypeLabel;
249
                break;
250
            } else {
251
                $this->logger->warning('File not found in fileGrp "' . $fileGrp . '"');
252
            }
253
        }
254
        return $image;
255
    }
256
257
    /**
258
     * Renders the image manipulation tool
259
     *
260
     * @return void
261
     */
262
    public function imagemanipulationtool()
263
    {
264
        // Set parent element for initialization.
265
        $parentContainer = !empty($this->settings['parentContainer']) ? $this->settings['parentContainer'] : '.tx-dlf-imagemanipulationtool';
266
267
        $this->view->assign('imageManipulation', true);
268
        $this->view->assign('parentContainer', $parentContainer);
269
    }
270
271
    /**
272
     * Renders the PDF download tool
273
     *
274
     * @return void
275
     */
276
    public function pdfdownloadtool()
277
    {
278
        if (
279
            $this->document === null
280
            || $this->document->getDoc() === null
281
            || $this->document->getDoc()->numPages < 1
282
            || empty($this->extConf['fileGrpDownload'])
283
        ) {
284
            // Quit without doing anything if required variables are not set.
285
            return;
286
        } else {
287
            if (!empty($this->requestData['logicalPage'])) {
288
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
289
                // The logical page parameter should not appear again
290
                unset($this->requestData['logicalPage']);
291
            }
292
            // Set default values if not set.
293
            // $this->requestData['page'] may be integer or string (physical structure @ID)
294
            if (
295
                (int) $this->requestData['page'] > 0
296
                || empty($this->requestData['page'])
297
            ) {
298
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
299
            } else {
300
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
301
            }
302
        }
303
        // Get single page downloads.
304
        $this->view->assign('pageLinks', $this->getPageLink());
305
        // Get work download.
306
        $this->view->assign('workLink', $this->getWorkLink());
307
    }
308
309
    /**
310
     * Get page's download link
311
     *
312
     * @access protected
313
     *
314
     * @return array Link to downloadable page
315
     */
316
    protected function getPageLink()
317
    {
318
        $page1Link = '';
319
        $page2Link = '';
320
        $pageLinkArray = [];
321
        $pageNumber = $this->requestData['page'];
322
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']);
323
        // Get image link.
324
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
325
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber]]['files'][$fileGrpDownload])) {
326
                $page1Link = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber]]['files'][$fileGrpDownload]);
327
                // Get second page, too, if double page view is activated.
328
                if (
329
                    $this->requestData['double']
330
                    && $pageNumber < $this->document->getDoc()->numPages
331
                    && !empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload])
332
                ) {
333
                    $page2Link = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload]);
334
                }
335
                break;
336
            }
337
        }
338
        if (
339
            empty($page1Link)
340
            && empty($page2Link)
341
        ) {
342
            $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"');
343
        }
344
345
        if (!empty($page1Link)) {
346
            $pageLinkArray[0] = $page1Link;
347
        }
348
        if (!empty($page2Link)) {
349
            $pageLinkArray[1] = $page2Link;
350
        }
351
        return $pageLinkArray;
352
    }
353
354
    /**
355
     * Get work's download link
356
     *
357
     * @access protected
358
     *
359
     * @return string Link to downloadable work
360
     */
361
    protected function getWorkLink()
362
    {
363
        $workLink = '';
364
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']);
365
        // Get work link.
366
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
367
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[0]]['files'][$fileGrpDownload])) {
368
                $workLink = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[0]]['files'][$fileGrpDownload]);
369
                break;
370
            } else {
371
                $details = $this->document->getDoc()->getLogicalStructure($this->document->getDoc()->toplevelId);
372
                if (!empty($details['files'][$fileGrpDownload])) {
373
                    $workLink = $this->document->getDoc()->getFileLocation($details['files'][$fileGrpDownload]);
374
                    break;
375
                }
376
            }
377
        }
378
        if (!empty($workLink)) {
379
            $workLink = $workLink;
380
        } else {
381
            $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"');
382
        }
383
        return $workLink;
384
    }
385
386
    /**
387
     * Renders the searchInDocument tool
388
     *
389
     * @return void
390
     */
391
    public function searchindocumenttool()
392
    {
393
        if (
394
            $this->document === null
395
            || $this->document->getDoc()->numPages < 1
396
            || empty($this->extConf['fileGrpFulltext'])
397
            || empty($this->settings['solrcore'])
398
        ) {
399
            // Quit without doing anything if required variables are not set.
400
            return;
401
        } else {
402
            if (!empty($this->requestData['logicalPage'])) {
403
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
404
                // The logical page parameter should not appear again
405
                unset($this->requestData['logicalPage']);
406
            }
407
            // Set default values if not set.
408
            // $this->requestData['page'] may be integer or string (physical structure @ID)
409
            if (
410
                (int) $this->requestData['page'] > 0
411
                || empty($this->requestData['page'])
412
            ) {
413
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
414
            } else {
415
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
416
            }
417
        }
418
419
        // Quit if no fulltext file is present
420
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
421
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
422
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext])) {
423
                $fullTextFile = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext];
424
                break;
425
            }
426
        }
427
        if (empty($fullTextFile)) {
428
            return;
429
        }
430
431
        // Fill markers.
432
        $viewArray = [
433
            'LABEL_QUERY_URL' => $this->settings['queryInputName'],
434
            'LABEL_START' => $this->settings['startInputName'],
435
            'LABEL_ID' => $this->settings['idInputName'],
436
            'LABEL_PAGE_URL' => $this->settings['pageInputName'],
437
            'LABEL_HIGHLIGHT_WORD' => $this->settings['highlightWordInputName'],
438
            'LABEL_ENCRYPTED' => $this->settings['encryptedInputName'],
439
            'CURRENT_DOCUMENT' => $this->getCurrentDocumentId(),
440
            'SOLR_ENCRYPTED' => $this->getEncryptedCoreName() ? : ''
441
        ];
442
443
        $this->view->assign('searchInDocument', $viewArray);
444
    }
445
446
    /**
447
     * Get current document id. As default the uid will be used.
448
     * In case there is defined documentIdUrlSchema then the id will
449
     * extracted from this URL.
450
     *
451
     * @access protected
452
     *
453
     * @return string with current document id
454
     */
455
    protected function getCurrentDocumentId()
456
    {
457
        $id = $this->document->getUid();
458
459
        if ($id !== null && $id > 0) {
460
            // we found the document uid
461
            return (string) $id;
462
        } else {
463
            $id = $this->requestData['id'];
464
            if (!GeneralUtility::isValidUrl($id)) {
465
                // we found no valid URI --> something unexpected we cannot search within.
466
                return '';
467
            }
468
        }
469
470
        // example: https://host.de/items/*id*/record
471
        if (!empty($this->settings['documentIdUrlSchema'])) {
472
            $arr = explode('*', $this->settings['documentIdUrlSchema']);
473
474
            if (count($arr) == 2) {
475
                $id = explode($arr[0], $id)[0];
476
            } else if (count($arr) == 3) {
477
                $sub = substr($id, strpos($id, $arr[0]) + strlen($arr[0]), strlen($id));
478
                $id = substr($sub, 0, strpos($sub, $arr[2]));
479
            }
480
        }
481
        return $id;
482
    }
483
484
    /**
485
     * Get the encrypted Solr core name
486
     *
487
     * @access protected
488
     *
489
     * @return string with encrypted core name
490
     */
491
    protected function getEncryptedCoreName()
492
    {
493
        // Get core name.
494
        $name = Helper::getIndexNameFromUid($this->settings['solrcore'], 'tx_dlf_solrcores');
495
        // Encrypt core name.
496
        if (!empty($name)) {
497
            $name = Helper::encrypt($name);
498
        }
499
        return $name;
500
    }
501
}
502