Passed
Push — master ( d5568c...ac874d )
by Timo
28:49 queued 03:56
created

FrequentlySearchedController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1.0023

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 18
ccs 13
cts 15
cp 0.8667
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1.0023
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\Utility\GeneralUtility;
22
23
/**
24
 * Class FrequentlySearchedController
25
 *
26
 * @author Frans Saris <[email protected]>
27
 * @author Timo Hund <[email protected]>
28
 */
29
class FrequentlySearchedController extends AbstractWidgetController
30
{
31
    /**
32
     * Initializes the cache for this command.
33
     *
34
     * @return \TYPO3\CMS\Core\Cache\AbstractFrontend
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Cache\AbstractFrontend was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
     */
36 34
    protected function getInitializedCache()
37
    {
38 34
        $cacheIdentifier = 'tx_solr';
39
        try {
40 34
            $cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache($cacheIdentifier);
41
        } catch (NoSuchCacheException $e) {
42
            /** @var t3lib_cache_Factory $typo3CacheFactory */
43
            $typo3CacheFactory = $GLOBALS['typo3CacheFactory'];
44
            $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']);
45
        }
46
47 34
        return $cacheInstance;
48
    }
49
50
    /**
51
     * Last searches
52
     */
53 34
    public function indexAction()
54
    {
55 34
        $tsfe = $GLOBALS['TSFE'];
56 34
        $cache = $this->getInitializedCache();
57 34
        $configuration = $this->controllerContext->getTypoScriptConfiguration();
58
59 34
        $frequentSearchesService = GeneralUtility::makeInstance(
60 34
            FrequentSearchesService::class,
61 34
            /** @scrutinizer ignore-type */ $configuration,
62 34
            /** @scrutinizer ignore-type */ $cache,
63 34
            /** @scrutinizer ignore-type */ $tsfe
64
        );
65
66 34
        $frequentSearches = $frequentSearchesService->getFrequentSearchTerms();
67 34
        $minimumSize = $configuration->getSearchFrequentSearchesMinSize();
68 34
        $maximumSize = $configuration->getSearchFrequentSearchesMaxSize();
69
70 34
        $this->view->assign('contentArguments', ['frequentSearches' => $this->enrichFrequentSearchesInfo($frequentSearches, $minimumSize, $maximumSize)]);
71 34
    }
72
73
    /**
74
     * Enrich the frequentSearches
75
     *
76
     * @param array Frequent search terms as array with terms as keys and hits as the value
77
     * @param integer $minimumSize
78
     * @param integer $maximumSize
79
     * @return array An array with content for the frequent terms markers
80
     */
81 34
    protected function enrichFrequentSearchesInfo(array $frequentSearchTerms, $minimumSize, $maximumSize)
82
    {
83 34
        $frequentSearches = [];
84 34
        if (count($frequentSearchTerms)) {
85 34
            $maximumHits = max(array_values($frequentSearchTerms));
86 34
            $minimumHits = min(array_values($frequentSearchTerms));
87 34
            $spread = $maximumHits - $minimumHits;
88 34
            $step = ($spread == 0) ? 1 : ($maximumSize - $minimumSize) / $spread;
89
90 34
            foreach ($frequentSearchTerms as $term => $hits) {
91 34
                $size = round($minimumSize + (($hits - $minimumHits) * $step));
92 34
                $frequentSearches[] = ['q' => htmlspecialchars_decode($term), 'hits' => $hits, 'style' => 'font-size: ' . $size . 'px', 'class' => 'tx-solr-frequent-term-' . $size, 'size' => $size];
93
            }
94
        }
95
96 34
        return $frequentSearches;
97
    }
98
}
99