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 ( 67cfe3...9c7e21 )
by
unknown
04:03
created

ToolboxController   F

Complexity

Total Complexity 74

Size/Duplication

Total Lines 459
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 194
dl 0
loc 459
rs 2.48
c 8
b 0
f 0
wmc 74

15 Methods

Rating   Name   Duplication   Size   Complexity  
A renderPdfDownloadTool() 0 16 3
A renderFulltextDownloadTool() 0 24 6
A renderAnnotationTool() 0 17 4
A renderImageDownloadTool() 0 19 4
A renderToolByName() 0 3 1
A getImage() 0 27 5
A renderImageManipulationTool() 0 7 2
A mainAction() 0 12 2
A getEncryptedCoreName() 0 9 2
A getWorkLink() 0 23 5
B getCurrentDocumentId() 0 27 7
B renderTool() 0 26 9
B getPageLink() 0 36 10
B renderSearchInDocumentTool() 0 38 8
A renderFulltextTool() 0 24 6

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\Utility\MathUtility;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18
/**
19
 * Controller class for plugin 'Toolbox'.
20
 *
21
 * @author Sebastian Meyer <[email protected]>
22
 * @package TYPO3
23
 * @subpackage dlf
24
 * @access public
25
 */
26
class ToolboxController extends AbstractController
27
{
28
29
    /**
30
     * This holds the current document
31
     *
32
     * @var \Kitodo\Dlf\Common\Doc
33
     * @access private
34
     */
35
    private $doc;
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();
46
47
        $this->view->assign('double', $this->requestData['double']);
48
49
        if (!$this->isDocMissingOrEmpty()) {
50
            $this->doc = $this->document->getDoc();
51
        }
52
53
        $this->renderTool();
54
    }
55
56
    /**
57
     * Renders tool in the toolbox.
58
     *
59
     * @access private
60
     *
61
     * @return void
62
     */
63
    private function renderTool() {
64
        if (!empty($this->settings['tool'])) {
65
            switch ($this->settings['tool']) {
66
                case 'tx_dlf_annotationtool':
67
                    $this->renderToolByName('renderAnnotationTool');
68
                    break;
69
                case 'tx_dlf_fulltextdownloadtool':
70
                    $this->renderToolByName('renderFulltextDownloadTool');
71
                    break;
72
                case 'tx_dlf_fulltexttool':
73
                    $this->renderToolByName('renderFulltextTool');
74
                    break;
75
                case 'tx_dlf_imagedownloadtool':
76
                    $this->renderToolByName('renderImageDownloadTool');
77
                    break;
78
                case 'tx_dlf_imagemanipulationtool':
79
                    $this->renderToolByName('renderImageManipulationTool');
80
                    break;
81
                case 'tx_dlf_pdfdownloadtool':
82
                    $this->renderToolByName('renderPdfDownloadTool');
83
                    break;
84
                case 'tx_dlf_searchindocumenttool':
85
                    $this->renderToolByName('renderSearchInDocumentTool');
86
                    break;
87
                default:
88
                    $this->logger->warn('Incorrect tool configuration: "' . $this->settings['tool'] . '". This tool does not exist.');
0 ignored issues
show
Bug introduced by
The method warn() 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

88
                    $this->logger->/** @scrutinizer ignore-call */ 
89
                                   warn('Incorrect tool configuration: "' . $this->settings['tool'] . '". This tool does not exist.');

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...
Bug introduced by
The method warn() does not exist on Psr\Log\LoggerInterface. Did you maybe mean warning()? ( Ignorable by Annotation )

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

88
                    $this->logger->/** @scrutinizer ignore-call */ 
89
                                   warn('Incorrect tool configuration: "' . $this->settings['tool'] . '". This tool does not exist.');

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