Passed
Push — master ( 9d2dcb...4e6543 )
by Timo
45:55 queued 43:04
created

FrequentSearchesService::getFrequentSearchTerms()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 26
ccs 13
cts 14
cp 0.9286
rs 9.7666
c 0
b 0
f 0
cc 4
nc 5
nop 0
crap 4.0058
1
<?php declare(strict_types = 1);
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\FrequentSearches;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2015-2016 Timo Schmidt <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Domain\Search\Statistics\StatisticsRepository;
28
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
29
use ApacheSolrForTypo3\Solr\Util;
30
use TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
33
34
/**
35
 * The FrequentSearchesService is used to retrieve the frequent searches from the database or cache.
36
 *
37
 * @author Dimitri Ebert <[email protected]>
38
 * @author Timo Schmidt <[email protected]>
39
 */
40
class FrequentSearchesService
41
{
42
43
    /**
44
     * Instance of the caching frontend used to cache this command's output.
45
     *
46
     * @var AbstractFrontend
47
     */
48
    protected $cache;
49
50
    /**
51
     * @var TypoScriptFrontendController
52
     */
53
    protected $tsfe;
54
55
    /**
56
     * @var StatisticsRepository
57
     */
58
    protected $statisticsRepository;
59
60
    /**
61
     * @var TypoScriptConfiguration
62
     */
63
    protected $configuration;
64
65
    /**
66
     * @param TypoScriptConfiguration $typoscriptConfiguration
67
     * @param AbstractFrontend $cache
68
     * @param TypoScriptFrontendController $tsfe
69
     * @param StatisticsRepository $statisticsRepository
70
     */
71 36
    public function __construct(TypoScriptConfiguration $typoscriptConfiguration, AbstractFrontend $cache, TypoScriptFrontendController $tsfe, StatisticsRepository $statisticsRepository = null)
72
    {
73 36
        $this->configuration = $typoscriptConfiguration;
74 36
        $this->cache = $cache;
75 36
        $this->tsfe = $tsfe;
76 36
        $this->statisticsRepository = $statisticsRepository ?? GeneralUtility::makeInstance(StatisticsRepository::class);
77 36
    }
78
79
    /**
80
     * Generates an array with terms and hits
81
     *
82
     * @return array Tags as array with terms and hits
83
     */
84 36
    public function getFrequentSearchTerms() : array
85
    {
86 36
        $frequentSearchConfiguration = $this->configuration->getSearchFrequentSearchesConfiguration();
87
88 36
        $identifier = $this->getCacheIdentifier($frequentSearchConfiguration);
89
90 36
        if ($this->cache->has($identifier)) {
91 4
            $terms = $this->cache->get($identifier);
92
        } else {
93 35
            $terms = $this->getFrequentSearchTermsFromStatistics($frequentSearchConfiguration);
94
95 35
            if ($frequentSearchConfiguration['sortBy'] === 'hits') {
96
                arsort($terms);
97
            } else {
98 35
                ksort($terms);
99
            }
100
101 35
            $lifetime = null;
102 35
            if (isset($frequentSearchConfiguration['cacheLifetime'])) {
103 34
                $lifetime = intval($frequentSearchConfiguration['cacheLifetime']);
104
            }
105
106 35
            $this->cache->set($identifier, $terms, [], $lifetime);
107
        }
108
109 36
        return $terms;
110
    }
111
112
    /**
113
     * Gets frequent search terms from the statistics tracking table.
114
     *
115
     * @param array $frequentSearchConfiguration
116
     * @return array Array of frequent search terms, keys are the terms, values are hits
117
     */
118 35
    protected function getFrequentSearchTermsFromStatistics(array $frequentSearchConfiguration) : array
119
    {
120 35
        $terms = [];
121
122 35
        if ($frequentSearchConfiguration['select.']['checkRootPageId']) {
123 1
            $checkRootPidWhere = 'root_pid = ' . $this->tsfe->tmpl->rootLine[0]['uid'];
124
        } else {
125 34
            $checkRootPidWhere = '1';
126
        }
127 35
        if ($frequentSearchConfiguration['select.']['checkLanguage']) {
128 1
            $checkLanguageWhere = ' AND language =' . Util::getLanguageUid();
129
        } else {
130 34
            $checkLanguageWhere = '';
131
        }
132
133 35
        $frequentSearchConfiguration['select.']['ADD_WHERE'] = $checkRootPidWhere .
134 35
            $checkLanguageWhere . ' ' .
135 35
            $frequentSearchConfiguration['select.']['ADD_WHERE'];
136
137 35
        $frequentSearchTerms = $this->statisticsRepository->getFrequentSearchTermsFromStatisticsByFrequentSearchConfiguration($frequentSearchConfiguration);
138
139 35
        if (!is_array($frequentSearchTerms)) {
0 ignored issues
show
introduced by
The condition is_array($frequentSearchTerms) is always true.
Loading history...
140
            return $terms;
141
        }
142
143 35
        foreach ($frequentSearchTerms as $term) {
144 35
            $cleanedTerm = html_entity_decode($term['search_term'], ENT_QUOTES, 'UTF-8');
145 35
            $terms[$cleanedTerm] = $term['hits'];
146
        }
147
148 35
        return $terms;
149
    }
150
151
    /**
152
     * @param array $frequentSearchConfiguration
153
     * @return string
154
     */
155 36
    protected function getCacheIdentifier(array $frequentSearchConfiguration) : string
156
    {
157
        // Use configuration as cache identifier
158 36
        $identifier = 'frequentSearchesTags';
159
160 36
        if ($frequentSearchConfiguration['select.']['checkRootPageId']) {
161 1
            $identifier .= '_RP' . (int)$this->tsfe->tmpl->rootLine[0]['uid'];
162
        }
163 36
        if ($frequentSearchConfiguration['select.']['checkLanguage']) {
164 1
            $identifier .= '_L' . Util::getLanguageUid();
165
        }
166
167 36
        $identifier .= '_' . md5(serialize($frequentSearchConfiguration));
168 36
        return $identifier;
169
    }
170
}
171