Passed
Push — master ( 203e1d...831cab )
by Timo
27:25
created

FrequentlySearchedController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1.0005

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 11
cts 12
cp 0.9167
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
crap 1.0005
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller;
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\FrequentSearches\FrequentSearchesService;
18
use ApacheSolrForTypo3\Solr\Widget\AbstractWidgetController;
19
use TYPO3\CMS\Core\Cache\CacheManager;
20
use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
21
use TYPO3\CMS\Core\Database\DatabaseConnection;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
/**
25
 * Class FrequentlySearchedController
26
 *
27
 * @author Frans Saris <[email protected]>
28
 * @author Timo Hund <[email protected]>
29
 * @package ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller
30
 */
31
class FrequentlySearchedController extends AbstractWidgetController
32
{
33
    /**
34
     * Initializes the cache for this command.
35
     *
36
     * @return \TYPO3\CMS\Core\Cache\AbstractFrontend
37
     */
38 24
    protected function getInitializedCache()
39
    {
40 24
        $cacheIdentifier = 'tx_solr';
41
        try {
42 24
            $cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache($cacheIdentifier);
43
        } catch (NoSuchCacheException $e) {
44
            /** @var t3lib_cache_Factory $typo3CacheFactory */
45
            $typo3CacheFactory = $GLOBALS['typo3CacheFactory'];
46
            $cacheInstance = $typo3CacheFactory->create($cacheIdentifier, $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$cacheIdentifier]['frontend'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$cacheIdentifier]['backend'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$cacheIdentifier]['options']);
47
        }
48
49 24
        return $cacheInstance;
50
    }
51
52
    /**
53
     * Last searches
54
     */
55 24
    public function indexAction()
56
    {
57 24
        $databaseConnection = $GLOBALS['TYPO3_DB'];
58 24
        $tsfe = $GLOBALS['TSFE'];
59 24
        $cache = $this->getInitializedCache();
60 24
        $configuration = $this->controllerContext->getTypoScriptConfiguration();
61
62 24
        $frequentSearchesService = GeneralUtility::makeInstance(FrequentSearchesService::class, $configuration, $cache, $tsfe, $databaseConnection);
63
64 24
        $frequentSearches = $frequentSearchesService->getFrequentSearchTerms();
65 24
        $minimumSize = $configuration->getSearchFrequentSearchesMinSize();
66 24
        $maximumSize = $configuration->getSearchFrequentSearchesMaxSize();
67
68 24
        $this->view->assign('contentArguments', ['frequentSearches' => $this->enrichFrequentSearchesInfo($frequentSearches, $minimumSize, $maximumSize)]);
69 24
    }
70
71
    /**
72
     * Enrich the frequentSearches
73
     *
74
     * @param array Frequent search terms as array with terms as keys and hits as the value
75
     * @param integer $minimumSize
76
     * @param integer $maximumSize
77
     * @return array An array with content for the frequent terms markers
78
     */
79 24
    protected function enrichFrequentSearchesInfo(array $frequentSearchTerms, $minimumSize, $maximumSize)
80
    {
81 24
        $frequentSearches = [];
82 24
        if (count($frequentSearchTerms)) {
83 24
            $maximumHits = max(array_values($frequentSearchTerms));
84 24
            $minimumHits = min(array_values($frequentSearchTerms));
85 24
            $spread = $maximumHits - $minimumHits;
86 24
            $step = ($spread == 0) ? 1 : ($maximumSize - $minimumSize) / $spread;
87
88 24
            foreach ($frequentSearchTerms as $term => $hits) {
89 24
                $size = round($minimumSize + (($hits - $minimumHits) * $step));
90 24
                $frequentSearches[] = ['q' => htmlspecialchars_decode($term), 'hits' => $hits, 'style' => 'font-size: ' . $size . 'px', 'class' => 'tx-solr-frequent-term-' . $size, 'size' => $size];
91
            }
92
        }
93
94 24
        return $frequentSearches;
95
    }
96
}
97