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

RelevanceViewHelper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 6 1
A renderStatic() 0 21 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Document;
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\ResultSet\Result\SearchResult;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
19
use ApacheSolrForTypo3\Solr\ViewHelpers\AbstractSolrFrontendViewHelper;
20
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
22
23
/**
24
 * Class RelevanceViewHelper
25
 *
26
 * @author Frans Saris <[email protected]>
27
 * @author Timo Hund <[email protected]>
28
 */
29
class RelevanceViewHelper extends AbstractSolrFrontendViewHelper
30
{
31
    use CompileWithRenderStatic;
32
33
    /**
34
     * Initializes the arguments
35
     */
36 1
    public function initializeArguments()
37
    {
38 1
        parent::initializeArguments();
39 1
        $this->registerArgument('resultSet', SearchResultSet::class, 'The context searchResultSet', true);
40 1
        $this->registerArgument('document', SearchResult::class, 'The document to highlight', true);
41 1
        $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);
42 1
    }
43
44
    /**
45
     * @param array $arguments
46
     * @param \Closure $renderChildrenClosure
47
     * @param RenderingContextInterface $renderingContext
48
     * @return string
49
     */
50 30
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
51
    {
52
        /** @var $document SearchResult */
53 30
        $document = $arguments['document'];
54
55
            /** @var $resultSet SearchResultSet */
56 30
        $resultSet = $arguments['resultSet'];
57
58 30
        $maximumScore = $arguments['maximumScore'] ?? $resultSet->getMaximumScore();
59 30
        $content = 0;
60
61 30
        if ($maximumScore <= 0) {
62
            return $content;
63
        }
64
65 30
        $documentScore = $document->getScore();
66 30
        $score = floatval($documentScore);
67 30
        $multiplier = 100 / $maximumScore;
68 30
        $scorePercentage = round($score * $multiplier);
69 30
        $content = $scorePercentage;
70 30
        return $content;
71
    }
72
}
73