Passed
Push — release-11.0.x ( 910681...ed160c )
by Rafael
11:00
created

PreviewController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 54.55%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 29
c 2
b 1
f 0
dl 0
loc 77
ccs 18
cts 33
cp 0.5455
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfiguredTikaService() 0 3 1
A previewAction() 0 31 3
A getFileResourceFactory() 0 3 1
A getIsAdmin() 0 3 2
A getInitializedPreviewView() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApacheSolrForTypo3\Tika\Controller\Backend;
6
7
/*
8
 * This file is part of the TYPO3 CMS project.
9
 *
10
 * It is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU General Public License, either version 2
12
 * of the License, or any later version.
13
 *
14
 * For the full copyright and license information, please read the
15
 * LICENSE.txt file that was distributed with this source code.
16
 *
17
 * The TYPO3 project - inspiring people to share!
18
 */
19
20
use ApacheSolrForTypo3\Tika\Service\Tika\AbstractService;
21
use ApacheSolrForTypo3\Tika\Service\Tika\AppService;
22
use ApacheSolrForTypo3\Tika\Service\Tika\ServerService;
23
use ApacheSolrForTypo3\Tika\Service\Tika\ServiceFactory;
24
use ApacheSolrForTypo3\Tika\Service\Tika\SolrCellService;
25
use Psr\Http\Client\ClientExceptionInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Throwable;
28
use TYPO3\CMS\Core\Http\HtmlResponse;
29
use TYPO3\CMS\Core\Http\Response;
30
use TYPO3\CMS\Core\Resource\ResourceFactory;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Fluid\View\StandaloneView;
33
34
/**
35
 * Class PreviewController
36
 */
37
class PreviewController
38
{
39
40
    /**
41
     * @param ServerRequestInterface $request
42
     * @return string|Response
43
     * @throws ClientExceptionInterface
44
     */
45 2
    public function previewAction(ServerRequestInterface $request)
46
    {
47 2
        $response = new HtmlResponse('');
48 2
        if (!$this->getIsAdmin()) {
49 1
            $messageText = 'Only admins can see the tika preview';
50 1
            $response->getBody()->write($messageText);
51 1
            return $response->withStatus(403, $messageText);
52
        }
53
54 1
        $identifier = (string)$request->getQueryParams()['identifier'];
55 1
        $file = $this->getFileResourceFactory()->getFileObjectFromCombinedIdentifier($identifier);
56
57 1
        $tikaService = $this->getConfiguredTikaService();
58 1
        $metadata = $tikaService->extractMetaData($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type null; however, parameter $file of ApacheSolrForTypo3\Tika\...vice::extractMetaData() does only seem to accept TYPO3\CMS\Core\Resource\FileInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

58
        $metadata = $tikaService->extractMetaData(/** @scrutinizer ignore-type */ $file);
Loading history...
Bug introduced by
It seems like $file can also be of type null; however, parameter $file of ApacheSolrForTypo3\Tika\...vice::extractMetaData() does only seem to accept TYPO3\CMS\Core\Resource\FileInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

58
        $metadata = $tikaService->extractMetaData(/** @scrutinizer ignore-type */ $file);
Loading history...
Bug introduced by
It seems like $file can also be of type null; however, parameter $file of ApacheSolrForTypo3\Tika\...vice::extractMetaData() does only seem to accept TYPO3\CMS\Core\Resource\FileInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

58
        $metadata = $tikaService->extractMetaData(/** @scrutinizer ignore-type */ $file);
Loading history...
59 1
        $content = $tikaService->extractText($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type null; however, parameter $file of ApacheSolrForTypo3\Tika\...lService::extractText() does only seem to accept TYPO3\CMS\Core\Resource\FileInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

59
        $content = $tikaService->extractText(/** @scrutinizer ignore-type */ $file);
Loading history...
Bug introduced by
It seems like $file can also be of type null; however, parameter $file of ApacheSolrForTypo3\Tika\...pService::extractText() does only seem to accept TYPO3\CMS\Core\Resource\FileInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

59
        $content = $tikaService->extractText(/** @scrutinizer ignore-type */ $file);
Loading history...
Bug introduced by
It seems like $file can also be of type null; however, parameter $file of ApacheSolrForTypo3\Tika\...rService::extractText() does only seem to accept TYPO3\CMS\Core\Resource\FileInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

59
        $content = $tikaService->extractText(/** @scrutinizer ignore-type */ $file);
Loading history...
60
61
        try {
62 1
            $language = $tikaService->detectLanguageFromFile($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type null; however, parameter $file of ApacheSolrForTypo3\Tika\...etectLanguageFromFile() does only seem to accept TYPO3\CMS\Core\Resource\FileInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

62
            $language = $tikaService->detectLanguageFromFile(/** @scrutinizer ignore-type */ $file);
Loading history...
Bug introduced by
It seems like $file can also be of type null; however, parameter $file of ApacheSolrForTypo3\Tika\...etectLanguageFromFile() does only seem to accept TYPO3\CMS\Core\Resource\FileInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

62
            $language = $tikaService->detectLanguageFromFile(/** @scrutinizer ignore-type */ $file);
Loading history...
Bug introduced by
It seems like $file can also be of type null; however, parameter $file of ApacheSolrForTypo3\Tika\...etectLanguageFromFile() does only seem to accept TYPO3\CMS\Core\Resource\FileInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

62
            $language = $tikaService->detectLanguageFromFile(/** @scrutinizer ignore-type */ $file);
Loading history...
63
        } catch (Throwable $e) {
64
            $language = 'not detectable';
65
        }
66
67 1
        $view = $this->getInitializedPreviewView();
68
69 1
        $view->assign('metadata', $metadata);
70 1
        $view->assign('content', $content);
71 1
        $view->assign('language', $language);
72
73 1
        $response->getBody()->write($view->render());
74
75 1
        return $response;
76
    }
77
78
    /**
79
     * @return AppService|ServerService|SolrCellService
80
     */
81
    protected function getConfiguredTikaService(): AbstractService
82
    {
83
        return ServiceFactory::getConfiguredTika();
84
    }
85
86
    /**
87
     * @return ResourceFactory
88
     * @noinspection PhpIncompatibleReturnTypeInspection
89
     */
90
    protected function getFileResourceFactory(): ResourceFactory
91
    {
92
        return GeneralUtility::makeInstance(ResourceFactory::class);
93
    }
94
95
    /**
96
     * @return StandaloneView
97
     */
98
    protected function getInitializedPreviewView(): StandaloneView
99
    {
100
        /** @var $view StandaloneView */
101
        $view = GeneralUtility::makeInstance(StandaloneView::class);
102
        $view->getRequest()->setControllerExtensionName('tika');
103
        $templatePathAndFile = 'EXT:tika/Resources/Private/Templates/Backend/Preview.html';
104
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templatePathAndFile));
105
        return $view;
106
    }
107
108
    /**
109
     * @return bool
110
     */
111
    protected function getIsAdmin(): bool
112
    {
113
        return !empty($GLOBALS['BE_USER']) && $GLOBALS['BE_USER']->isAdmin();
114
    }
115
}
116