Completed
Push — master ( bc4bd1...b40f3e )
by Timo
05:05
created

PreviewController::getInitializedPreviewView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
namespace ApacheSolrForTypo3\Tika\Controller\Backend;
3
4
use ApacheSolrForTypo3\Tika\Service\Tika\AppService;
5
use ApacheSolrForTypo3\Tika\Service\Tika\ServerService;
6
use ApacheSolrForTypo3\Tika\Service\Tika\ServiceFactory;
7
use ApacheSolrForTypo3\Tika\Service\Tika\SolrCellService;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use TYPO3\CMS\Core\Resource\ResourceFactory;
11
use TYPO3\CMS\Core\Utility\GeneralUtility;
12
use TYPO3\CMS\Fluid\View\StandaloneView;
13
14
/**
15
 * Class PreviewController
16
 * @package ApacheSolrForTypo3\Tika\Controller\Backend
17
 */
18
class PreviewController {
19
20
    /**
21
     * @param ServerRequestInterface $request
22
     * @param ResponseInterface $response
23
     * @return string
24
     */
25 2
    public function previewAction(ServerRequestInterface $request, ResponseInterface $response)
26
    {
27 2
        if (!$this->getIsAdmin()) {
28 1
            $response->getBody()->write('Only admins can see the tika preview');
29 1
            return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Psr\Http\Message\ResponseInterface which is incompatible with the documented return type string.
Loading history...
30
        }
31
32 1
        $identifier = (string)$request->getQueryParams()['identifier'];
33 1
        $file = $this->getFileResourceFactory()->getFileObjectFromCombinedIdentifier($identifier);
34
35 1
        $tikaService = $this->getConfiguredTikaService();
36 1
        $metadata = $tikaService->extractMetaData($file);
37 1
        $content = $tikaService->extractText($file);
38
39
        try {
40 1
            $language = $tikaService->detectLanguageFromFile($file);
41
        } catch (\Exception $e) {
42
            $language = 'not detectable';
43
        }
44
45 1
        $view = $this->getInitializedPreviewView();
46
47 1
        $view->assign('metadata', $metadata);
48 1
        $view->assign('content', $content);
49 1
        $view->assign('language', $language);
50
51 1
        $response->getBody()->write($view->render());
52
53 1
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Psr\Http\Message\ResponseInterface which is incompatible with the documented return type string.
Loading history...
54
    }
55
56
    /**
57
     * @return AppService|ServerService|SolrCellService
58
     */
59
    protected function getConfiguredTikaService()
60
    {
61
        return ServiceFactory::getConfiguredTika();
62
    }
63
64
    /**
65
     * @return \TYPO3\CMS\Core\Resource\ResourceFactory
66
     */
67
    protected function getFileResourceFactory(): ResourceFactory
68
    {
69
        return \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
70
    }
71
72
    /**
73
     * @return StandaloneView
74
     */
75
    protected function getInitializedPreviewView(): StandaloneView
76
    {
77
        /** @var $view StandaloneView */
78
        $view = GeneralUtility::makeInstance(StandaloneView::class);
79
        $view->getRequest()->setControllerExtensionName('tika');
80
        $templatePathAndFile = 'EXT:tika/Resources/Private/Templates/Backend/Preview.html';
81
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templatePathAndFile));
82
        return $view;
83
    }
84
85
    /**
86
     * @return boolean
87
     */
88
    protected function getIsAdmin()
89
    {
90
        return (bool)$GLOBALS['BE_USER']->isAdmin();
91
    }
92
}