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 ( 03fdba...557885 )
by
unknown
03:55
created

ToolboxController::renderTools()   C

Complexity

Conditions 17
Paths 17

Size

Total Lines 38
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 34
c 1
b 0
f 0
nc 17
nop 0
dl 0
loc 38
rs 5.2166

How to fix   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\AbstractDocument;
15
use Kitodo\Dlf\Common\Helper;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Core\Utility\MathUtility;
18
19
/**
20
 * Controller class for plugin 'Toolbox'.
21
 *
22
 * @package TYPO3
23
 * @subpackage dlf
24
 *
25
 * @access public
26
 */
27
class ToolboxController extends AbstractController
28
{
29
30
    /**
31
     * @access private
32
     * @var AbstractDocument This holds the current document
33
     */
34
    private AbstractDocument $currentDocument;
35
36
    /**
37
     * The main method of the plugin
38
     *
39
     * @access public
40
     *
41
     * @return void
42
     */
43
    public function mainAction(): void
44
    {
45
        // Load current document.
46
        $this->loadDocument();
47
48
        $this->view->assign('double', $this->requestData['double']);
49
50
        if (!$this->isDocMissingOrEmpty()) {
51
            $this->currentDocument = $this->document->getCurrentDocument();
52
        }
53
54
        $this->renderTools();
55
        $this->view->assign('viewData', $this->viewData);
56
    }
57
58
    /**
59
     * Renders tool in the toolbox.
60
     *
61
     * @access private
62
     *
63
     * @return void
64
     */
65
    private function renderTools(): void
66
    {
67
        if (!empty($this->settings['tools'])) {
68
69
            $tools = explode(',', $this->settings['tools']);
70
71
            foreach ($tools as $tool) {
72
                switch ($tool) {
73
                    case 'tx_dlf_annotationtool':
74
                    case 'annotationtool':
75
                        $this->renderToolByName('renderAnnotationTool');
76
                        break;
77
                    case 'tx_dlf_fulltextdownloadtool':
78
                    case 'fulltextdownloadtool':
79
                        $this->renderToolByName('renderFulltextDownloadTool');
80
                        break;
81
                    case 'tx_dlf_fulltexttool':
82
                    case 'fulltexttool':
83
                        $this->renderToolByName('renderFulltextTool');
84
                        break;
85
                    case 'tx_dlf_imagedownloadtool':
86
                    case 'imagedownloadtool':
87
                        $this->renderToolByName('renderImageDownloadTool');
88
                        break;
89
                    case 'tx_dlf_imagemanipulationtool':
90
                    case 'imagemanipulationtool':
91
                        $this->renderToolByName('renderImageManipulationTool');
92
                        break;
93
                    case 'tx_dlf_pdfdownloadtool':
94
                    case 'pdfdownloadtool':
95
                        $this->renderToolByName('renderPdfDownloadTool');
96
                        break;
97
                    case 'tx_dlf_searchindocumenttool':
98
                    case 'searchindocumenttool':
99
                        $this->renderToolByName('renderSearchInDocumentTool');
100
                        break;
101
                    default:
102
                        $this->logger->warning('Incorrect tool configuration: "' . $this->settings['tools'] . '". Tool "' . $tool . '" does not exist.');
103
                }
104
            }
105
        }
106
    }
107
108
    /**
109
     * Renders tool by the name in the toolbox.
110
     *
111
     * @access private
112
     *
113
     * @param string $tool name
114
     *
115
     * @return void
116
     */
117
    private function renderToolByName(string $tool): void
118
    {
119
        $this->$tool();
120
        $this->view->assign($tool, true);
121
    }
122
123
    /**
124
     * Renders the annotation tool
125
     *
126
     * @access private
127
     *
128
     * @return void
129
     */
130
    private function renderAnnotationTool(): void
131
    {
132
        if ($this->isDocMissingOrEmpty()) {
133
            // Quit without doing anything if required variables are not set.
134
            return;
135
        }
136
137
        $this->setPage();
138
139
        $annotationContainers = $this->currentDocument->physicalStructureInfo[$this->currentDocument->physicalStructure[$this->requestData['page']]]['annotationContainers'];
140
        if (
141
            $annotationContainers != null
142
            && sizeof($annotationContainers) > 0
143
        ) {
144
            $this->view->assign('annotationTool', true);
145
        } else {
146
            $this->view->assign('annotationTool', false);
147
        }
148
    }
149
150
    /**
151
     * Renders the fulltext download tool
152
     *
153
     * @access private
154
     *
155
     * @return void
156
     */
157
    private function renderFulltextDownloadTool(): void
158
    {
159
        if (
160
            $this->isDocMissingOrEmpty()
161
            || empty($this->extConf['fileGrpFulltext'])
162
        ) {
163
            // Quit without doing anything if required variables are not set.
164
            return;
165
        }
166
167
        $this->setPage();
168
169
        // Get text download.
170
        $this->view->assign('fulltextDownload', !$this->isFullTextEmpty());
171
    }
172
173
    /**
174
     * Renders the fulltext tool
175
     *
176
     * @access private
177
     *
178
     * @return void
179
     */
180
    private function renderFulltextTool(): void
181
    {
182
        if (
183
            $this->isDocMissingOrEmpty()
184
            || empty($this->extConf['fileGrpFulltext'])
185
        ) {
186
            // Quit without doing anything if required variables are not set.
187
            return;
188
        }
189
190
        $this->setPage();
191
192
        if (!$this->isFullTextEmpty()) {
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(): void
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;
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(int $page): array
238
    {
239
        $image = [];
240
        // Get @USE value of METS fileGrp.
241
        $fileGrps = GeneralUtility::trimExplode(',', $this->settings['fileGrpsImageDownload']);
242
        while ($fileGrp = @array_pop($fileGrps)) {
243
            // Get image link.
244
            $physicalStructureInfo = $this->currentDocument->physicalStructureInfo[$this->currentDocument->physicalStructure[$page]];
245
            $fileId = $physicalStructureInfo['files'][$fileGrp];
246
            if (!empty($fileId)) {
247
                $image['url'] = $this->currentDocument->getDownloadLocation($fileId);
248
                $image['mimetype'] = $this->currentDocument->getFileMimeType($fileId);
249
                switch ($image['mimetype']) {
250
                    case 'image/jpeg':
251
                        $image['mimetypeLabel']  = ' (JPG)';
252
                        break;
253
                    case 'image/tiff':
254
                        $image['mimetypeLabel']  = ' (TIFF)';
255
                        break;
256
                    default:
257
                        $image['mimetypeLabel']  = '';
258
                }
259
                break;
260
            } else {
261
                $this->logger->warning('File not found in fileGrp "' . $fileGrp . '"');
262
            }
263
        }
264
        return $image;
265
    }
266
267
    /**
268
     * Renders the image manipulation tool
269
     *
270
     * @access private
271
     *
272
     * @return void
273
     */
274
    private function renderImageManipulationTool(): void
275
    {
276
        // Set parent element for initialization.
277
        $parentContainer = !empty($this->settings['parentContainer']) ? $this->settings['parentContainer'] : '.tx-dlf-imagemanipulationtool';
278
279
        $this->view->assign('imageManipulation', true);
280
        $this->view->assign('parentContainer', $parentContainer);
281
    }
282
283
    /**
284
     * Renders the PDF download tool
285
     *
286
     * @access private
287
     *
288
     * @return void
289
     */
290
    private function renderPdfDownloadTool(): void
291
    {
292
        if (
293
            $this->isDocMissingOrEmpty()
294
            || empty($this->extConf['fileGrpDownload'])
295
        ) {
296
            // Quit without doing anything if required variables are not set.
297
            return;
298
        }
299
300
        $this->setPage();
301
302
        // Get single page downloads.
303
        $this->view->assign('pageLinks', $this->getPageLink());
304
        // Get work download.
305
        $this->view->assign('workLink', $this->getWorkLink());
306
    }
307
308
    /**
309
     * Get page's download link
310
     *
311
     * @access private
312
     *
313
     * @return array Link to downloadable page
314
     */
315
    private function getPageLink(): array
316
    {
317
        $firstPageLink = '';
318
        $secondPageLink = '';
319
        $pageLinkArray = [];
320
        $pageNumber = $this->requestData['page'];
321
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']);
322
        // Get image link.
323
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
324
            $firstFileGroupDownload = $this->currentDocument->physicalStructureInfo[$this->currentDocument->physicalStructure[$pageNumber]]['files'][$fileGrpDownload];
325
            if (!empty($firstFileGroupDownload)) {
326
                $firstPageLink = $this->currentDocument->getFileLocation($firstFileGroupDownload);
327
                // Get second page, too, if double page view is activated.
328
                $secondFileGroupDownload = $this->currentDocument->physicalStructureInfo[$this->currentDocument->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload];
329
                if (
330
                    $this->requestData['double']
331
                    && $pageNumber < $this->currentDocument->numPages
332
                    && !empty($secondFileGroupDownload)
333
                ) {
334
                    $secondPageLink = $this->currentDocument->getFileLocation($secondFileGroupDownload);
335
                }
336
                break;
337
            }
338
        }
339
        if (
340
            empty($firstPageLink)
341
            && empty($secondPageLink)
342
        ) {
343
            $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"');
344
        }
345
346
        if (!empty($firstPageLink)) {
347
            $pageLinkArray[0] = $firstPageLink;
348
        }
349
        if (!empty($secondPageLink)) {
350
            $pageLinkArray[1] = $secondPageLink;
351
        }
352
        return $pageLinkArray;
353
    }
354
355
    /**
356
     * Get work's download link
357
     *
358
     * @access private
359
     *
360
     * @return string Link to downloadable work
361
     */
362
    private function getWorkLink(): string
363
    {
364
        $workLink = '';
365
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']);
366
        // Get work link.
367
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
368
            $fileGroupDownload = $this->currentDocument->physicalStructureInfo[$this->currentDocument->physicalStructure[0]]['files'][$fileGrpDownload];
369
            if (!empty($fileGroupDownload)) {
370
                $workLink = $this->currentDocument->getFileLocation($fileGroupDownload);
371
                break;
372
            } else {
373
                $details = $this->currentDocument->getLogicalStructure($this->currentDocument->toplevelId);
374
                if (!empty($details['files'][$fileGrpDownload])) {
375
                    $workLink = $this->currentDocument->getFileLocation($details['files'][$fileGrpDownload]);
376
                    break;
377
                }
378
            }
379
        }
380
        if (empty($workLink)) {
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
     * @access private
390
     *
391
     * @return void
392
     */
393
    private function renderSearchInDocumentTool(): void
394
    {
395
        if (
396
            $this->isDocMissingOrEmpty()
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
        }
403
404
        $this->setPage();
405
406
        // Quit if no fulltext file is present
407
        if ($this->isFullTextEmpty()) {
408
            return;
409
        }
410
411
        // Fill markers.
412
        $viewArray = [
413
            'LABEL_QUERY_URL' => $this->settings['queryInputName'],
414
            'LABEL_START' => $this->settings['startInputName'],
415
            'LABEL_ID' => $this->settings['idInputName'],
416
            'LABEL_PID' => $this->settings['pidInputName'],
417
            'LABEL_PAGE_URL' => $this->settings['pageInputName'],
418
            'LABEL_HIGHLIGHT_WORD' => $this->settings['highlightWordInputName'],
419
            'LABEL_ENCRYPTED' => $this->settings['encryptedInputName'],
420
            'CURRENT_DOCUMENT' => $this->getCurrentDocumentId(),
421
            'SOLR_ENCRYPTED' => $this->getEncryptedCoreName() ? : ''
422
        ];
423
424
        $this->view->assign('searchInDocument', $viewArray);
425
    }
426
427
    /**
428
     * Get current document id. As default the uid will be used.
429
     * In case there is defined documentIdUrlSchema then the id will
430
     * extracted from this URL.
431
     *
432
     * @access private
433
     *
434
     * @return string with current document id
435
     */
436
    private function getCurrentDocumentId(): string
437
    {
438
        $id = $this->document->getUid();
439
440
        if ($id !== null && $id > 0) {
441
            // we found the document uid
442
            return (string) $id;
443
        } else {
444
            $id = $this->requestData['id'];
445
            if (!GeneralUtility::isValidUrl($id)) {
446
                // we found no valid URI --> something unexpected we cannot search within.
447
                return '';
448
            }
449
        }
450
451
        // example: https://host.de/items/*id*/record
452
        if (!empty($this->settings['documentIdUrlSchema'])) {
453
            $arr = explode('*', $this->settings['documentIdUrlSchema']);
454
455
            if (count($arr) == 2) {
456
                $id = explode($arr[0], $id)[0];
457
            } else if (count($arr) == 3) {
458
                $sub = substr($id, strpos($id, $arr[0]) + strlen($arr[0]), strlen($id));
459
                $id = substr($sub, 0, strpos($sub, $arr[2]));
460
            }
461
        }
462
        return $id;
463
    }
464
465
    /**
466
     * Get the encrypted Solr core name
467
     *
468
     * @access private
469
     *
470
     * @return string with encrypted core name
471
     */
472
    private function getEncryptedCoreName(): string
473
    {
474
        // Get core name.
475
        $name = Helper::getIndexNameFromUid($this->settings['solrcore'], 'tx_dlf_solrcores');
476
        // Encrypt core name.
477
        if (!empty($name)) {
478
            $name = Helper::encrypt($name);
479
        }
480
        return $name;
481
    }
482
483
    /**
484
     * Check if the full text is empty.
485
     *
486
     * @access private
487
     *
488
     * @return bool true if empty, false otherwise
489
     */
490
    private function isFullTextEmpty(): bool
491
    {
492
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
493
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
494
            $fullTextFile = $this->currentDocument->physicalStructureInfo[$this->currentDocument->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext];
495
            if (!empty($fullTextFile)) {
496
                break;
497
            }
498
        }
499
        return empty($fullTextFile);
500
    }
501
}
502