Passed
Pull Request — release-11.2.x (#3594)
by Markus
14:43 queued 10:47
created

RelevanceViewHelper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 63.16%

Importance

Changes 0
Metric Value
wmc 3
eloc 18
dl 0
loc 42
ccs 12
cts 19
cp 0.6316
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A renderStatic() 0 21 2
A initializeArguments() 0 6 1
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Document;
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\SearchResult;
19
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
20
use ApacheSolrForTypo3\Solr\ViewHelpers\AbstractSolrFrontendViewHelper;
21
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23
24
/**
25
 * Class RelevanceViewHelper
26
 *
27
 * @author Frans Saris <[email protected]>
28
 * @author Timo Hund <[email protected]>
29
 */
30
class RelevanceViewHelper extends AbstractSolrFrontendViewHelper
31
{
32
    use CompileWithRenderStatic;
33
34
    /**
35
     * Initializes the arguments
36
     */
37
    public function initializeArguments()
38
    {
39
        parent::initializeArguments();
40
        $this->registerArgument('resultSet', SearchResultSet::class, 'The context searchResultSet', true);
41
        $this->registerArgument('document', SearchResult::class, 'The document to highlight', true);
42
        $this->registerArgument('maximumScore', 'float', 'The maximum score that should be used for percentage calculation, if nothing is passed the maximum from the resultSet is used', false);
43
    }
44
45
    /**
46
     * @param array $arguments
47
     * @param \Closure $renderChildrenClosure
48
     * @param RenderingContextInterface $renderingContext
49
     * @return string
50 2
     */
51
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
52
    {
53 2
        /** @var $document SearchResult */
54
        $document = $arguments['document'];
55
56 2
        /** @var $resultSet SearchResultSet */
57
        $resultSet = $arguments['resultSet'];
58 2
59 2
        $maximumScore = $arguments['maximumScore'] ?? $resultSet->getMaximumScore();
60
        $content = 0;
61 2
62
        if ($maximumScore <= 0) {
63
            return $content;
64
        }
65 2
66 2
        $documentScore = $document->getScore();
67 2
        $score = (float)$documentScore;
68 2
        $multiplier = 100 / $maximumScore;
69 2
        $scorePercentage = round($score * $multiplier);
70 2
        $content = $scorePercentage;
71
        return $content;
72
    }
73
}
74