Passed
Push — master ( dedf8a...bd2c53 )
by Timo
19:58 queued 20s
created

getLastSearchesService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1.0046

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1.0046
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 26
    public function process(SearchResultSet $resultSet) {
46
47 26
        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 21
        if (!isset($GLOBALS['TSFE']) || !isset($GLOBALS['TYPO3_DB'])) {
53
            return $resultSet;
54
        }
55
56 21
        $query = $resultSet->getUsedSearchRequest()->getRawUserQuery();
57
58 21
        if (is_string($query)) {
59 19
            $lastSearchesService = $this->getLastSearchesService($resultSet);
60 19
            $lastSearchesService->addToLastSearches($query);
61
        }
62
63 21
        return $resultSet;
64
    }
65
66
    /**
67
     * @param SearchResultSet $resultSet
68
     * @return LastSearchesService
69
     */
70 19
    protected function getLastSearchesService(SearchResultSet $resultSet) {
71 19
        return GeneralUtility::makeInstance(LastSearchesService::class,
72 19
            $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration(),
73 19
            $GLOBALS['TSFE'],
74 19
            $GLOBALS['TYPO3_DB']);
75
    }
76
}
77