Completed
Push — master ( e93629...dcd410 )
by
unknown
19s queued 15s
created

SearchSuggest::main()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 21
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 32
rs 8.9617
1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Eid;
14
15
use Kitodo\Dlf\Common\Helper;
16
use Kitodo\Dlf\Common\Solr;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use TYPO3\CMS\Core\Core\Environment;
20
use TYPO3\CMS\Core\Http\Response;
21
use TYPO3\CMS\Core\Information\Typo3Version;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
/**
25
 * eID search suggestions for plugin 'Search' of the 'dlf' extension
26
 *
27
 * @author Henrik Lochmann <[email protected]>
28
 * @author Sebastian Meyer <[email protected]>
29
 * @package TYPO3
30
 * @subpackage dlf
31
 * @access public
32
 */
33
class SearchSuggest
34
{
35
    /**
36
     * The main method of the eID script
37
     *
38
     * @param ServerRequestInterface $request
39
     * @return ResponseInterface XML response of search suggestions
40
     */
41
    public function main(ServerRequestInterface $request)
42
    {
43
        $output = [];
44
        // Get input parameters and decrypt core name.
45
        $parameters = $request->getParsedBody();
46
        $solrCore = (string) $parameters['solrcore'];
47
        $uHash = (string) $parameters['uHash'];
48
        if (hash_equals(GeneralUtility::hmac((string) (new Typo3Version()) . Environment::getExtensionsPath(), 'SearchSuggest'), $uHash) === false) {
49
            throw new \InvalidArgumentException('No valid parameter passed!', 1580585079);
50
        }
51
        // Perform Solr query.
52
        $solr = Solr::getInstance($solrCore);
53
        if ($solr->ready) {
54
            $query = $solr->service->createSelect();
55
            $query->setHandler('suggest');
56
            $query->setQuery(Solr::escapeQuery((string) $parameters['q']));
57
            $query->setRows(0);
58
            $results = $solr->service->select($query)->getResponse()->getBody();
59
            $result = json_decode($results);
60
            foreach ($result->spellcheck->suggestions as $suggestions) {
61
                if (is_object($suggestions)) {
62
                    foreach ($suggestions->suggestion as $suggestion) {
63
                        $output[] = $suggestion;
64
                    }
65
                }
66
            }
67
        }
68
        // Create response object.
69
        /** @var Response $response */
70
        $response = GeneralUtility::makeInstance(Response::class);
71
        $response->getBody()->write(json_encode($output));
72
        return $response;
73
    }
74
}
75