Passed
Push — master ( 17ae05...2920d4 )
by Timo
27:57
created

SearchResultBuilder::fromApacheSolrDocument()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0185
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2017 Timo Hund <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * The SearchResultBuilder is responsible to build a SearchResult object from an Apache_Solr_Document
33
 * and should use a different class as SearchResult if configured.
34
 *
35
 * @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet
36
 */
37
class SearchResultBuilder {
38
39
    /**
40
     * This method is used to wrap the original solr document instance in an instance of the configured SearchResult
41
     * class.
42
     *
43
     * @param Document $originalDocument
44
     * @throws \InvalidArgumentException
45
     * @return SearchResult
46
     */
47 37
    public function fromApacheSolrDocument(Document $originalDocument)
48
    {
49
50 37
        $searchResultClassName = $this->getResultClassName();
51 37
        $result = GeneralUtility::makeInstance($searchResultClassName, /** @scrutinizer ignore-type */ $originalDocument->getFields() ?? []);
52
53 37
        if (!$result instanceof SearchResult) {
54
            throw new \InvalidArgumentException('Could not create result object with class: ' . (string)$searchResultClassName, 1470037679);
55
        }
56
57 37
        return $result;
58
    }
59
60
    /**
61
     * @return string
62
     */
63 37
    protected function getResultClassName()
64
    {
65 37
        return isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['searchResultClassName ']) ?
66 37
            $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['searchResultClassName '] : SearchResult::class;
67
    }
68
}