Passed
Push — master ( fb7724...8efec3 )
by Timo
04:27
created

SearchResultBuilder::getResultClassName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet;
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 2 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 TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * The SearchResultBuilder is responsible to build a SearchResult object from an Apache_Solr_Document
32
 * and should use a different class as SearchResult if configured.
33
 *
34
 * @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet
35
 */
36
class SearchResultBuilder {
37
38
    /**
39
     * This method is used to wrap the \Apache_Solr_Document instance in an instance of the configured SearchResult
40
     * class.
41
     *
42
     * @param \Apache_Solr_Document $originalDocument
43
     * @throws \InvalidArgumentException
44
     * @return SearchResult
45
     */
46
    public function fromApacheSolrDocument(\Apache_Solr_Document $originalDocument)
47
    {
48
        $searchResultClassName = $this->getResultClassName();
49
        $result = GeneralUtility::makeInstance($searchResultClassName, $originalDocument);
50
        if (!$result instanceof SearchResult) {
51
            throw new \InvalidArgumentException('Could not create result object with class: ' . (string)$searchResultClassName, 1470037679);
52
        }
53
54
        return $result;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    protected function getResultClassName()
61
    {
62
        return isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['searchResultClassName ']) ?
63
            $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['searchResultClassName '] : SearchResult::class;
64
    }
65
}