Passed
Push — master ( 203e1d...831cab )
by Timo
27:25
created

RelevanceViewHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 49
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 6 1
A renderStatic() 0 23 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 1
    public function initializeArguments()
42
    {
43 1
        parent::initializeArguments();
44 1
        $this->registerArgument('resultSet', SearchResultSet::class, 'The context searchResultSet', true);
45 1
        $this->registerArgument('document', \Apache_Solr_Document::class, 'The document to highlight', true);
46 1
    }
47
48
    /**
49
     * @param array $arguments
50
     * @param \Closure $renderChildrenClosure
51
     * @param RenderingContextInterface $renderingContext
52
     * @return string
53
     */
54 21
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
55
    {
56
        /** @var $document \Apache_Solr_Document */
57 21
        $document = $arguments['document'];
58
59
            /** @var $resultSet SearchResultSet */
60 21
        $resultSet = $arguments['resultSet'];
61
62 21
        $maximumScore = $document->__solr_grouping_groupMaximumScore ?: $resultSet->getUsedSearch()->getMaximumResultScore();
63 21
        $content = 0;
64
65 21
        if ($maximumScore <= 0) {
66
            return $content;
67
        }
68
69 21
        $documentScore = $document->getScore();
70 21
        $score = floatval($documentScore);
71 21
        $multiplier = 100 / $maximumScore;
72 21
        $scorePercentage = round($score * $multiplier);
73 21
        $content = $scorePercentage;
74
75 21
        return $content;
76
    }
77
}
78