Passed
Push — master ( efbfe4...f2d6e9 )
by Timo
23:10
created

getFrequentSearchTermsFromStatistics()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5.0042

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 17
cts 18
cp 0.9444
rs 8.439
c 0
b 0
f 0
cc 5
eloc 20
nc 12
nop 1
crap 5.0042
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 2 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 TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
32
33
/**
34
 * The FrequentSearchesService is used to retrieve the frequent searches from the database or cache.
35
 *
36
 * @author Dimitri Ebert <[email protected]>
37
 * @author Timo Schmidt <[email protected]>
38
 */
39
class FrequentSearchesService
40
{
41
42
    /**
43
     * Instance of the caching frontend used to cache this command's output.
44
     *
45
     * @var AbstractFrontend
46
     */
47
    protected $cache;
48
49
    /**
50
     * @var TypoScriptFrontendController
51
     */
52
    protected $tsfe;
53
54
    /**
55
     * @var StatisticsRepository
56
     */
57
    protected $statisticsRepository;
58
59
    /**
60
     * @var TypoScriptConfiguration
61
     */
62
    protected $configuration;
63
64
    /**
65
     * @param TypoScriptConfiguration $typoscriptConfiguration
66
     * @param AbstractFrontend $cache
67
     * @param TypoScriptFrontendController $tsfe
68
     * @param StatisticsRepository $statisticsRepository
69
     */
70 31
    public function __construct(TypoScriptConfiguration $typoscriptConfiguration, AbstractFrontend $cache, TypoScriptFrontendController $tsfe, StatisticsRepository $statisticsRepository = null)
71
    {
72 31
        $this->configuration = $typoscriptConfiguration;
73 31
        $this->cache = $cache;
74 31
        $this->tsfe = $tsfe;
75 31
        $this->statisticsRepository = isset($statisticsRepository) ? $statisticsRepository : GeneralUtility::makeInstance(StatisticsRepository::class);
76 31
    }
77
78
    /**
79
     * Generates an array with terms and hits
80
     *
81
     * @return array Tags as array with terms and hits
82
     */
83 31
    public function getFrequentSearchTerms() : array
84
    {
85 31
        $frequentSearchConfiguration = $this->configuration->getSearchFrequentSearchesConfiguration();
86
87 31
        $identifier = $this->getCacheIdentifier($frequentSearchConfiguration);
88
89 31
        if ($this->cache->has($identifier)) {
90 4
            $terms = $this->cache->get($identifier);
91
        } else {
92 30
            $terms = $this->getFrequentSearchTermsFromStatistics($frequentSearchConfiguration);
93
94 30
            if ($frequentSearchConfiguration['sortBy'] === 'hits') {
95
                arsort($terms);
96
            } else {
97 30
                ksort($terms);
98
            }
99
100 30
            $lifetime = null;
101 30
            if (isset($frequentSearchConfiguration['cacheLifetime'])) {
102 29
                $lifetime = intval($frequentSearchConfiguration['cacheLifetime']);
103
            }
104
105 30
            $this->cache->set($identifier, $terms, [], $lifetime);
106
        }
107
108 31
        return $terms;
109
    }
110
111
    /**
112
     * Gets frequent search terms from the statistics tracking table.
113
     *
114
     * @param array $frequentSearchConfiguration
115
     * @return array Array of frequent search terms, keys are the terms, values are hits
116
     */
117 30
    protected function getFrequentSearchTermsFromStatistics(array $frequentSearchConfiguration) : array
118
    {
119 30
        $terms = [];
120
121 30
        if ($frequentSearchConfiguration['select.']['checkRootPageId']) {
122 1
            $checkRootPidWhere = 'root_pid = ' . $this->tsfe->tmpl->rootLine[0]['uid'];
123
        } else {
124 29
            $checkRootPidWhere = '1';
125
        }
126 30
        if ($frequentSearchConfiguration['select.']['checkLanguage']) {
127 1
            $checkLanguageWhere = ' AND language =' . $this->tsfe->sys_language_uid;
128
        } else {
129 29
            $checkLanguageWhere = '';
130
        }
131
132 30
        $frequentSearchConfiguration['select.']['ADD_WHERE'] = $checkRootPidWhere .
133 30
            $checkLanguageWhere . ' ' .
134 30
            $frequentSearchConfiguration['select.']['ADD_WHERE'];
135
136 30
        $frequentSearchTerms = $this->statisticsRepository->getFrequentSearchTermsFromStatisticsByFrequentSearchConfiguration($frequentSearchConfiguration);
137
138 30
        if (!is_array($frequentSearchTerms)) {
139
            return $terms;
140
        }
141
142 30
        foreach ($frequentSearchTerms as $term) {
143 30
            $cleanedTerm = html_entity_decode($term['search_term'], ENT_QUOTES, 'UTF-8');
144 30
            $terms[$cleanedTerm] = $term['hits'];
145
        }
146
147 30
        return $terms;
148
    }
149
150
    /**
151
     * @param array $frequentSearchConfiguration
152
     * @return string
153
     */
154 31
    protected function getCacheIdentifier(array $frequentSearchConfiguration) : string
155
    {
156
        // Use configuration as cache identifier
157 31
        $identifier = 'frequentSearchesTags';
158
159 31
        if ($frequentSearchConfiguration['select.']['checkRootPageId']) {
160 1
            $identifier .= '_RP' . (int)$this->tsfe->tmpl->rootLine[0]['uid'];
161
        }
162 31
        if ($frequentSearchConfiguration['select.']['checkLanguage']) {
163 1
            $identifier .= '_L' . (int)$this->tsfe->sys_language_uid;
164
        }
165
166 31
        $identifier .= '_' . md5(serialize($frequentSearchConfiguration));
167 31
        return $identifier;
168
    }
169
}
170