Passed
Push — master ( 5f60a5...391298 )
by Timo
24:49
created

FrequentlySearchedController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 69.44%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 63
ccs 25
cts 36
cp 0.6944
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A enrichFrequentSearchesInfo() 0 16 4
A indexAction() 0 13 1
A getInitializedCache() 0 12 2
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
 * @package ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller
29
 */
30
class FrequentlySearchedController extends AbstractWidgetController
31
{
32
    /**
33
     * Initializes the cache for this command.
34
     *
35
     * @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...
36
     */
37 33
    protected function getInitializedCache()
38
    {
39 33
        $cacheIdentifier = 'tx_solr';
40
        try {
41 33
            $cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache($cacheIdentifier);
42
        } catch (NoSuchCacheException $e) {
43
            /** @var t3lib_cache_Factory $typo3CacheFactory */
44
            $typo3CacheFactory = $GLOBALS['typo3CacheFactory'];
45
            $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']);
46
        }
47
48 33
        return $cacheInstance;
49
    }
50
51
    /**
52
     * Last searches
53
     */
54 33
    public function indexAction()
55
    {
56 33
        $tsfe = $GLOBALS['TSFE'];
57 33
        $cache = $this->getInitializedCache();
58 33
        $configuration = $this->controllerContext->getTypoScriptConfiguration();
59
60 33
        $frequentSearchesService = GeneralUtility::makeInstance(FrequentSearchesService::class, $configuration, $cache, $tsfe);
0 ignored issues
show
Bug introduced by
$configuration of type ApacheSolrForTypo3\Solr\...TypoScriptConfiguration is incompatible with the type array<integer,mixed> expected by parameter $constructorArguments of TYPO3\CMS\Core\Utility\G...Utility::makeInstance(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        $frequentSearchesService = GeneralUtility::makeInstance(FrequentSearchesService::class, /** @scrutinizer ignore-type */ $configuration, $cache, $tsfe);
Loading history...
Bug introduced by
$cache of type TYPO3\CMS\Core\Cache\AbstractFrontend is incompatible with the type array<integer,mixed> expected by parameter $constructorArguments of TYPO3\CMS\Core\Utility\G...Utility::makeInstance(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        $frequentSearchesService = GeneralUtility::makeInstance(FrequentSearchesService::class, $configuration, /** @scrutinizer ignore-type */ $cache, $tsfe);
Loading history...
61
62 33
        $frequentSearches = $frequentSearchesService->getFrequentSearchTerms();
63 33
        $minimumSize = $configuration->getSearchFrequentSearchesMinSize();
64 33
        $maximumSize = $configuration->getSearchFrequentSearchesMaxSize();
65
66 33
        $this->view->assign('contentArguments', ['frequentSearches' => $this->enrichFrequentSearchesInfo($frequentSearches, $minimumSize, $maximumSize)]);
67 33
    }
68
69
    /**
70
     * Enrich the frequentSearches
71
     *
72
     * @param array Frequent search terms as array with terms as keys and hits as the value
73
     * @param integer $minimumSize
74
     * @param integer $maximumSize
75
     * @return array An array with content for the frequent terms markers
76
     */
77 33
    protected function enrichFrequentSearchesInfo(array $frequentSearchTerms, $minimumSize, $maximumSize)
78
    {
79 33
        $frequentSearches = [];
80 33
        if (count($frequentSearchTerms)) {
81 33
            $maximumHits = max(array_values($frequentSearchTerms));
82 33
            $minimumHits = min(array_values($frequentSearchTerms));
83 33
            $spread = $maximumHits - $minimumHits;
84 33
            $step = ($spread == 0) ? 1 : ($maximumSize - $minimumSize) / $spread;
85
86 33
            foreach ($frequentSearchTerms as $term => $hits) {
87 33
                $size = round($minimumSize + (($hits - $minimumHits) * $step));
88 33
                $frequentSearches[] = ['q' => htmlspecialchars_decode($term), 'hits' => $hits, 'style' => 'font-size: ' . $size . 'px', 'class' => 'tx-solr-frequent-term-' . $size, 'size' => $size];
89
            }
90
        }
91
92 33
        return $frequentSearches;
93
    }
94
}
95