Completed
Pull Request — master (#1346)
by Timo
33:05
created

RelevanceViewHelper::renderStatic()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 3
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\SearchResultSet;
18
use ApacheSolrForTypo3\Solr\ViewHelpers\AbstractSolrFrontendViewHelper;
19
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
21
22
/**
23
 * Class RelevanceViewHelper
24
 *
25
 * @author Frans Saris <[email protected]>
26
 * @author Timo Hund <[email protected]>
27
 * @package ApacheSolrForTypo3\Solr\ViewHelpers\Document
28
 */
29
class RelevanceViewHelper extends AbstractSolrFrontendViewHelper
30
{
31
    use CompileWithRenderStatic;
32
33
    /**
34
     * @var bool
35
     */
36
    protected $escapeOutput = false;
37
38
    /**
39
     * Initializes the arguments
40
     */
41
    public function initializeArguments()
42
    {
43
        parent::initializeArguments();
44
        $this->registerArgument('resultSet', SearchResultSet::class, 'The context searchResultSet', true);
45
        $this->registerArgument('document', \Apache_Solr_Document::class, 'The document to highlight', true);
46
    }
47
48
    /**
49
     * @param array $arguments
50
     * @param \Closure $renderChildrenClosure
51
     * @param RenderingContextInterface $renderingContext
52
     * @return string
53
     */
54
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
55
    {
56
        /** @var $document \Apache_Solr_Document */
57
        $document = $arguments['document'];
58
59
            /** @var $resultSet SearchResultSet */
60
        $resultSet = $arguments['resultSet'];
61
62
        $maximumScore = $document->__solr_grouping_groupMaximumScore ?: $resultSet->getUsedSearch()->getMaximumResultScore();
63
        $content = 0;
64
65
        if ($maximumScore <= 0) {
66
            return $content;
67
        }
68
69
        $documentScore = $document->getScore();
70
        $score = floatval($documentScore);
71
        $multiplier = 100 / $maximumScore;
72
        $scorePercentage = round($score * $multiplier);
73
        $content = $scorePercentage;
74
75
        return $content;
76
    }
77
}
78