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

VariantsProcessor   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 86
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
C process() 0 32 8
A buildVariantDocumentAndAssignToParentResult() 0 14 3
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Variants;
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 ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResult;
29
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultBuilder;
30
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
31
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetProcessor;
32
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35
/**
36
 * Builds the SearchResult objects from the solr response and assigns the created child SearchResult objects (the variants)
37
 * to the parent search result object.
38
 *
39
 * @package ApacheSolrForTypo3\Solr\Domain\Variants
40
 */
41
class VariantsProcessor implements SearchResultSetProcessor {
42
43
    /**
44
     * @var TypoScriptConfiguration
45
     */
46
    protected $typoScriptConfiguration;
47
48
    /**
49
     * @var SearchResultBuilder|null
50
     */
51
    protected $resultBuilder;
52
53
    /**
54
     * VariantsProcessor constructor.
55
     * @param TypoScriptConfiguration $configuration
56
     * @param SearchResultBuilder|null $resultBuilder
57
     */
58
    public function __construct(TypoScriptConfiguration $configuration, SearchResultBuilder $resultBuilder = null)
59
    {
60
        $this->typoScriptConfiguration = $configuration;
61
        $this->resultBuilder = is_null($resultBuilder) ? GeneralUtility::makeInstance(SearchResultBuilder::class) : $resultBuilder;
62
    }
63
64
    /**
65
     * This method is used to add documents to the expanded documents of the SearchResult
66
     * when collapsing is configured.
67
     *
68
     * @param SearchResultSet $resultSet
69
     * @return SearchResultSet
70
     */
71
    public function process(SearchResultSet $resultSet)
72
    {
73
        $response = $resultSet->getResponse();
74
        if (!is_array($response->response->docs)) {
0 ignored issues
show
Bug introduced by
The property response does not seem to exist. Did you mean _response?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
75
            return $resultSet;
76
        }
77
78
        if (!$this->typoScriptConfiguration->getSearchVariants()) {
79
            return $resultSet;
80
        }
81
82
        $variantsField = $this->typoScriptConfiguration->getSearchVariantsField();
83
        foreach ($response->response->docs as $key => $resultDocument) {
0 ignored issues
show
Bug introduced by
The property response does not seem to exist. Did you mean _response?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
84
            /** @var $resultDocument SearchResult */
85
            $variantField = $resultDocument->getField($variantsField);
86
            $variantId = isset($variantField['value']) ? $variantField['value'] : null;
87
88
            // when there is no value in the collapsing field, we can return
89
            if ($variantId === null) {
90
                continue;
91
            }
92
93
            $variantAccessKey = mb_strtolower($variantId);
94
            if (!isset($response->{'expanded'}) || !isset($response->{'expanded'}->{$variantAccessKey})) {
95
                continue;
96
            }
97
98
            $this->buildVariantDocumentAndAssignToParentResult($response, $variantAccessKey, $resultDocument);
99
        }
100
101
        return $resultSet;
102
    }
103
104
    /**
105
     * Build the SearchResult of the variant and assigns it to the parent result document.
106
     *
107
     * @param \Apache_Solr_Response $response
108
     * @param string $variantAccessKey
109
     * @param SearchResult $resultDocument
110
     */
111
    protected function buildVariantDocumentAndAssignToParentResult(\Apache_Solr_Response $response, $variantAccessKey, SearchResult $resultDocument)
112
    {
113
        foreach ($response->{'expanded'}->{$variantAccessKey}->{'docs'} as $variantDocumentArray) {
114
            $variantDocument = new \Apache_Solr_Document();
115
            foreach (get_object_vars($variantDocumentArray) as $propertyName => $propertyValue) {
116
                $variantDocument->{$propertyName} = $propertyValue;
117
            }
118
            $variantSearchResult = $this->resultBuilder->fromApacheSolrDocument($variantDocument);
119
            $variantSearchResult->setIsVariant(true);
120
            $variantSearchResult->setVariantParent($resultDocument);
121
122
            $resultDocument->addVariant($variantSearchResult);
123
        }
124
    }
125
126
}