Passed
Push — task/2976_TYPO3.11_compatibili... ( 803400...b598e4 )
by Rafael
33:58 queued 15:41
created

FrequentSearchesService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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