Passed
Push — release-11.5.x ( e13c39...c650c5 )
by Rafael
53:19 queued 10:37
created

LastSearchesWriterProcessor::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
ccs 9
cts 10
cp 0.9
rs 9.9666
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.016
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\Domain\Search\LastSearches;
17
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
19
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetProcessor;
20
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
21
use Doctrine\DBAL\Exception as DBALException;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
/**
25
 * Writes the last searches
26
 *
27
 * @author Timo Hund <[email protected]>
28
 */
29
class LastSearchesWriterProcessor implements SearchResultSetProcessor
30
{
31
    /**
32
     * @param SearchResultSet $resultSet
33
     *
34
     * @throws DBALDriverException
35
     * @throws DBALException|\Doctrine\DBAL\DBALException
36
     * @return SearchResultSet
37
     */
38 2
    public function process(SearchResultSet $resultSet): SearchResultSet
39
    {
40 2
        if ($resultSet->getAllResultCount() === 0) {
41
            // when the search does not produce a result we do not store the last searches
42 1
            return $resultSet;
43
        }
44
45 1
        if (!isset($GLOBALS['TSFE'])) {
46
            return $resultSet;
47
        }
48
49 1
        $query = $resultSet->getUsedSearchRequest()->getRawUserQuery();
50
51 1
        if (is_string($query)) {
0 ignored issues
show
introduced by
The condition is_string($query) is always true.
Loading history...
52 1
            $lastSearchesService = $this->getLastSearchesService($resultSet);
53 1
            $lastSearchesService->addToLastSearches($query);
54
        }
55
56 1
        return $resultSet;
57
    }
58
59
    /**
60
     * @param SearchResultSet $resultSet
61
     * @return LastSearchesService
62
     */
63 1
    protected function getLastSearchesService(SearchResultSet $resultSet): LastSearchesService
64
    {
65 1
        return GeneralUtility::makeInstance(
66
            LastSearchesService::class,
67
            /** @scrutinizer ignore-type */
68 1
            $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()
69
        );
70
    }
71
}
72