Passed
Push — master ( 9f83de...fb7724 )
by Timo
04:36
created

LastSearchesWriterProcessor::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.7286

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 9
cts 14
cp 0.6429
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
crap 4.7286
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Search\LastSearches;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2017 Timo Hund <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Domain\Search\LastSearches\LastSearchesService;
29
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
30
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetProcessor;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * Writes the last searches
35
 *
36
 * @author Timo Hund <[email protected]>
37
 */
38
class LastSearchesWriterProcessor implements SearchResultSetProcessor
39
{
40
41
    /**
42
     * @param SearchResultSet $resultSet
43
     * @return SearchResultSet
44
     */
45 27
    public function process(SearchResultSet $resultSet) {
46
47 27
        if ($resultSet->getAllResultCount() === 0) {
48
            // when the search does not produce a result we do not store the last searches
49 5
            return $resultSet;
50
        }
51
52 22
        if (!isset($GLOBALS['TSFE'])) {
53
            return $resultSet;
54
        }
55
56 22
        $query = $resultSet->getUsedSearchRequest()->getRawUserQuery();
57
58 22
        if (is_string($query)) {
59 20
            $lastSearchesService = $this->getLastSearchesService($resultSet);
60 20
            $lastSearchesService->addToLastSearches($query);
61
        }
62
63 22
        return $resultSet;
64
    }
65
66
    /**
67
     * @param SearchResultSet $resultSet
68
     * @return LastSearchesService
69
     */
70 20
    protected function getLastSearchesService(SearchResultSet $resultSet) {
71 20
        return GeneralUtility::makeInstance(LastSearchesService::class,
72 20
            $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration());
73 20
    }
74
}
75