Passed
Pull Request — master (#123)
by
unknown
07:53 queued 02:59
created

SearchSuggest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 53
rs 10
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 41 9
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\Middleware;
14
15
use Kitodo\Dlf\Common\Solr\Solr;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Psr\Http\Server\MiddlewareInterface;
19
use Psr\Http\Server\RequestHandlerInterface;
20
use TYPO3\CMS\Core\Core\Environment;
21
use TYPO3\CMS\Core\Http\Response;
22
use TYPO3\CMS\Core\Information\Typo3Version;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * Search suggestions Middleware for plugin 'Search' of the 'dlf' extension
27
 *
28
 * @package TYPO3
29
 * @subpackage dlf
30
 *
31
 * @access public
32
 */
33
class SearchSuggest implements MiddlewareInterface
34
{
35
    /**
36
     * The process method of the middleware.
37
     *
38
     * @access public
39
     *
40
     * @param ServerRequestInterface $request
41
     * @param RequestHandlerInterface $handler
42
     *
43
     * @return ResponseInterface XML response of search suggestions
44
     */
45
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
46
    {
47
        $response = $handler->handle($request);
48
        // Get input parameters and decrypt core name.
49
        $parameters = $request->getQueryParams();
50
        // Return if not this middleware
51
        if (!isset($parameters['middleware']) || ($parameters['middleware'] != 'dlf/search-suggest')) {
52
            return $response;
53
        }
54
55
        $output = [];
56
        $solrCore = (string) $parameters['solrcore'];
57
        $uHash = (string) $parameters['uHash'];
58
        if (hash_equals(GeneralUtility::hmac((string) (new Typo3Version()) . Environment::getExtensionsPath(), 'SearchSuggest'), $uHash) === false) {
59
            throw new \InvalidArgumentException('No valid parameter passed!', 1580585079);
60
        }
61
        // Perform Solr query.
62
        $solr = Solr::getInstance($solrCore);
63
        if ($solr->ready) {
64
            $query = $solr->service->createSelect();
65
            $query->setHandler('suggest');
66
            $query->setQuery(Solr::escapeQuery((string) $parameters['q']));
67
            $query->setRows(0);
68
            $results = $solr->service->select($query)->getResponse()->getBody();
69
            $result = json_decode($results);
70
            if (is_iterable($result->spellcheck->suggestions)) {
71
                foreach ($result->spellcheck->suggestions as $suggestions) {
72
                    if (is_object($suggestions)) {
73
                        foreach ($suggestions->suggestion as $suggestion) {
74
                            $output[] = $suggestion;
75
                        }
76
                    }
77
                }
78
            }
79
        }
80
81
        // Create response object.
82
        /** @var Response $response */
83
        $response = GeneralUtility::makeInstance(Response::class);
84
        $response->getBody()->write(json_encode($output));
85
        return $response;
86
    }
87
}
88