Failed Conditions
Push — release-11.5.x ( 71e6eb...3bfdb1 )
by Markus
27:37
created

DefaultResultParser::canParse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 10
c 1
b 0
f 0
cc 3
nc 2
nop 1
crap 3.072
1
<?php
2
3
declare(strict_types=1);
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
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\Parser;
19
20
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\SearchResultCollection;
21
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
22
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * The DefaultResultParser is able to parse normal(ungroupd results)
27
 */
28
class DefaultResultParser extends AbstractResultParser
29
{
30
    /**
31
     * @param SearchResultSet $resultSet
32
     * @param bool $useRawDocuments
33
     * @return SearchResultSet
34
     */
35 70
    public function parse(SearchResultSet $resultSet, bool $useRawDocuments = true): SearchResultSet
36
    {
37 70
        $searchResults = GeneralUtility::makeInstance(SearchResultCollection::class);
38 70
        $parsedData = $resultSet->getResponse()->getParsedData();
39
40
        // @extensionScannerIgnoreLine
41 70
        $resultSet->setMaximumScore($parsedData->response->maxScore ?? 0.0);
42
        // @extensionScannerIgnoreLine
43 70
        $resultSet->setAllResultCount($parsedData->response->numFound ?? 0);
44
45
        // @extensionScannerIgnoreLine
46 70
        if (!is_array($parsedData->response->docs ?? null)) {
47
            return $resultSet;
48
        }
49
50
        // @extensionScannerIgnoreLine
51 70
        $documents = $parsedData->response->docs;
52 70
        if (!$useRawDocuments) {
53 10
            $documents = $this->documentEscapeService->applyHtmlSpecialCharsOnAllFields($documents);
54
        }
55
56 70
        foreach ($documents as $searchResult) {
57 60
            $searchResultObject = $this->searchResultBuilder->fromApacheSolrDocument($searchResult);
58 60
            $searchResults[] = $searchResultObject;
59
        }
60
61 70
        $resultSet->setSearchResults($searchResults);
62 70
        return $resultSet;
63
    }
64
65
    /**
66
     * @param SearchResultSet $resultSet
67
     * @return bool
68
     */
69 70
    public function canParse(SearchResultSet $resultSet): bool
70
    {
71
        // These parsers should not be used when grouping is enabled
72 70
        $configuration = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration();
73 70
        if ($configuration instanceof TypoScriptConfiguration && $configuration->getIsSearchGroupingEnabled()) {
74
            return false;
75
        }
76
77 70
        return true;
78
    }
79
}
80