Passed
Push — task/2976_TYPO3.11_compatibili... ( d7dfd0...4be1cc )
by Rafael
03:40
created

LastSearchesWriterProcessor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 5
eloc 12
dl 0
loc 35
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getLastSearchesService() 0 3 1
A process() 0 19 4
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 3 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 2
    public function process(SearchResultSet $resultSet) {
46
47 2
        if ($resultSet->getAllResultCount() === 0) {
48
            // when the search does not produce a result we do not store the last searches
49 1
            return $resultSet;
50
        }
51
52 1
        if (!isset($GLOBALS['TSFE'])) {
53
            return $resultSet;
54
        }
55
56 1
        $query = $resultSet->getUsedSearchRequest()->getRawUserQuery();
57
58 1
        if (is_string($query)) {
59 1
            $lastSearchesService = $this->getLastSearchesService($resultSet);
60 1
            $lastSearchesService->addToLastSearches($query);
61
        }
62
63 1
        return $resultSet;
64
    }
65
66
    /**
67
     * @param SearchResultSet $resultSet
68
     * @return LastSearchesService
69
     */
70 1
    protected function getLastSearchesService(SearchResultSet $resultSet) {
71 1
        return GeneralUtility::makeInstance(LastSearchesService::class,
72 1
            /** @scrutinizer ignore-type */ $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration());
73
    }
74
}
75