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

initializeArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1.008
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Debug;
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\Score\ScoreCalculationService;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
19
use ApacheSolrForTypo3\Solr\ViewHelpers\AbstractSolrFrontendViewHelper;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23
24
/**
25
 * Class DocumentScoreAnalyzerViewHelper
26
 *
27
 * @author Frans Saris <[email protected]>
28
 * @author Timo Hund <[email protected]>
29
 * @package ApacheSolrForTypo3\Solr\ViewHelpers\Debug
30
 */
31
class DocumentScoreAnalyzerViewHelper extends AbstractSolrFrontendViewHelper
32
{
33
    use CompileWithRenderStatic;
34
35
    /**
36
     * @var ScoreCalculationService
37
     */
38
    protected static $scoreService;
39
40
    /**
41
     * @var bool
42
     */
43
    protected $escapeOutput = false;
44
45
    /**
46
     * Initializes the arguments
47
     */
48 1
    public function initializeArguments()
49
    {
50 1
        parent::initializeArguments();
51 1
        $this->registerArgument('document', \Apache_Solr_Document::class, 'The solr document', true);
52 1
    }
53
54
    /**
55
     * @param array $arguments
56
     * @param \Closure $renderChildrenClosure
57
     * @param RenderingContextInterface $renderingContext
58
     * @return string
59
     */
60 20
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
61
    {
62 20
        $content = '';
63
        // only check whether a BE user is logged in, don't need to check
64
        // for enabled score analysis as we wouldn't be here if it was disabled
65 20
        if (empty($GLOBALS['TSFE']->beUserLogin)) {
66 18
            return $content;
67
        }
68
69 2
        $document = $arguments['document'];
70
71
        /** @var $resultSet SearchResultSet */
72 2
        $resultSet = self::getUsedSearchResultSetFromRenderingContext($renderingContext);
73 2
        $debugData = $resultSet->getUsedSearch()->getDebugResponse()->explain->{$document->getId()};
74
75
        /** @var $scoreService ScoreCalculationService */
76 2
        $scoreService = self::getScoreService();
77 2
        $queryFields = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchQueryQueryFields();
78 2
        $content = $scoreService->getRenderedScores($debugData, $queryFields);
79
80 2
        return '<div class="document-score-analysis">' . $content . '</div>';
81
    }
82
83
    /**
84
     * @return ScoreCalculationService
85
     */
86 2
    protected static function getScoreService()
87
    {
88 2
        if (isset(self::$scoreService)) {
89 2
            return self::$scoreService;
90
        }
91
92 2
        self::$scoreService = GeneralUtility::makeInstance(ScoreCalculationService::class);
93 2
        return self::$scoreService;
94
    }
95
}
96