FactFinderNgSearchToElasticaMapper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 63
c 1
b 0
f 0
dl 0
loc 162
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A map() 0 18 2
A mapSortItems() 0 3 1
A __construct() 0 11 1
A mapSearchResultToElasticaResponseArray() 0 8 1
A mapElasticaHits() 0 57 4
A mapFacets() 0 3 1
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Client\FactFinderNg\Mapper\Elastica;
9
10
use Elastica\Query;
11
use Elastica\Response;
12
use Elastica\ResultSet;
13
use Elastica\ResultSet\DefaultBuilder;
14
use ErrorException;
15
use Generated\Shared\Transfer\StoreTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\StoreTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToPriceProductStorageClientInterface;
17
use SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToProductImageStorageClientInterface;
18
use SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToProductStorageClientInterface;
19
20
class FactFinderNgSearchToElasticaMapper extends AbstractFactFinderToElasticaMapper implements FactFinderToElasticaMapperInterface
21
{
22
    /**
23
     * @var \Elastica\ResultSet\DefaultBuilder
24
     */
25
    protected $elasticaDefaultBuilder;
26
27
    /**
28
     * @var \SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToProductStorageClientInterface
29
     */
30
    protected $productStorageClient;
31
32
    /**
33
     * @var \SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToProductImageStorageClientInterface
34
     */
35
    protected $productImageStorageClient;
36
37
    /**
38
     * @param \Elastica\ResultSet\DefaultBuilder $elasticaDefaultBuilder
39
     * @param \SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToProductStorageClientInterface $productStorageClient
40
     * @param \SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToProductImageStorageClientInterface $productImageStorageClient
41
     * @param \SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToPriceProductStorageClientInterface $priceProductStorageClient
42
     */
43
    public function __construct(
44
        DefaultBuilder $elasticaDefaultBuilder,
45
        FactFinderNgToProductStorageClientInterface $productStorageClient,
46
        FactFinderNgToProductImageStorageClientInterface $productImageStorageClient,
47
        FactFinderNgToPriceProductStorageClientInterface $priceProductStorageClient
48
    ) {
49
        parent::__construct($priceProductStorageClient);
50
51
        $this->elasticaDefaultBuilder = $elasticaDefaultBuilder;
52
        $this->productStorageClient = $productStorageClient;
53
        $this->productImageStorageClient = $productImageStorageClient;
54
    }
55
56
    /**
57
     * @param array $searchResult
58
     * @param \Elastica\Query $elasticaQuery
59
     * @param string $currentLocale
60
     * @param \Generated\Shared\Transfer\StoreTransfer $currentStore
61
     *
62
     * @return \Elastica\ResultSet
63
     */
64
    public function map(
65
        array $searchResult,
66
        Query $elasticaQuery,
67
        string $currentLocale,
68
        StoreTransfer $currentStore
69
    ): ResultSet {
70
        $this->currentLocale = $currentLocale;
71
        $this->currentStore = $currentStore;
72
73
        try {
74
            $elasticaResponseArray = $this->mapSearchResultToElasticaResponseArray($searchResult);
75
        } catch (ErrorException $e) {
76
            $elasticaResponseArray = [];
77
        }
78
79
        $elasticaResponse = new Response($elasticaResponseArray, 200);
80
81
        return $this->elasticaDefaultBuilder->buildResultSet($elasticaResponse, $elasticaQuery);
82
    }
83
84
    /**
85
     * @param array $searchResult
86
     *
87
     * @return array
88
     */
89
    protected function mapSearchResultToElasticaResponseArray(array $searchResult): array
90
    {
91
        $elasticaResponseArray = [];
92
        $elasticaResponseArray[static::KEY_HITS] = $this->mapElasticaHits($searchResult);
93
        $elasticaResponseArray[static::KEY_SORT_ITEMS] = $this->mapSortItems($searchResult);
94
        $elasticaResponseArray[static::KEY_FACETS] = $this->mapFacets($searchResult);
95
96
        return $elasticaResponseArray;
97
    }
98
99
    /**
100
     * @param array $searchResult
101
     *
102
     * @return array
103
     */
104
    protected function mapElasticaHits(array $searchResult): array
105
    {
106
        $total = $searchResult[static::KEY_TOTAL_HITS];
107
        $maxScore = max($searchResult[static::KEY_SCORE_FIRST_HIT], $searchResult[static::KEY_SCORE_LAST_HIT]);
108
        $elasticaHits = [];
109
        foreach ($searchResult[static::KEY_HITS] as $searchHit) {
110
            if (!count($searchHit[static::KEY_VARIANT_VALUES])) {
111
                continue;
112
            }
113
114
            $productAbstract = $this->productStorageClient
115
                ->findProductAbstractStorageDataByMapping(
116
                    static::SKU_MAPPING_TYPE,
117
                    $searchHit[static::KEY_OPTION_ID],
118
                    $this->currentLocale
119
                );
120
            if ($productAbstract === null) {
121
                continue;
122
            }
123
            $productAbstractImageStorageTransfer = $this->productImageStorageClient
124
                ->findProductImageAbstractStorageTransfer(
125
                    $productAbstract[static::KEY_ID_PRODUCT_ABSTRACT],
126
                    $this->currentLocale
127
                );
128
129
            $elasticaImages = $this->mapElasticaImages($productAbstractImageStorageTransfer);
0 ignored issues
show
Bug introduced by
It seems like $productAbstractImageStorageTransfer can also be of type null; however, parameter $productAbstractImageStorageTransfer of SprykerEco\Client\FactFi...er::mapElasticaImages() does only seem to accept Generated\Shared\Transfe...actImageStorageTransfer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
            $elasticaImages = $this->mapElasticaImages(/** @scrutinizer ignore-type */ $productAbstractImageStorageTransfer);
Loading history...
130
            $elasticaPrices = $this->mapElasticaPrices($productAbstract);
131
132
            $elasticaHit = [
133
                static::KEY_INDEX => $this->currentLocale . static::KEY_SEARCH,
134
                static::KEY_TYPE => static::KEY_PAGE,
135
                static::KEY_ID => $productAbstract[static::KEY_ID_PRODUCT_ABSTRACT],
136
                static::KEY_SCORE => $searchHit[static::KEY_SCORE],
137
                static::KEY_SOURCE =>
138
                    [
139
                        static::KEY_SEARCH_RESULT_DATA =>
140
                            [
141
                                static::KEY_IMAGES => $elasticaImages,
142
                                static::KEY_ID_PRODUCT_LABELS => [],
143
                                static::KEY_PRICE => 0,
144
                                static::KEY_ABSTRACT_NAME => $productAbstract[static::KEY_NAME],
145
                                static::KEY_ID_PRODUCT_ABSTRACT => $productAbstract[static::KEY_ID_PRODUCT_ABSTRACT],
146
                                static::KEY_OPTION_TYPE => static::KEY_PRODUCT_ABSTRACT,
147
                                static::KEY_PRICES => $elasticaPrices,
148
                                static::KEY_ABSTRACT_SKU => $productAbstract[static::KEY_SKU],
149
                                static::KEY_URL => $productAbstract[static::KEY_URL],
150
                            ],
151
                    ],
152
            ];
153
154
            $elasticaHits[] = $elasticaHit;
155
        }
156
157
        return [
158
            static::KEY_TOTAL => $total,
159
            static::KEY_MAX_SCORE => $maxScore,
160
            static::KEY_HITS => $elasticaHits,
161
        ];
162
    }
163
164
    /**
165
     * @param array $searchResult
166
     *
167
     * @return array
168
     */
169
    protected function mapSortItems(array $searchResult): array
170
    {
171
        return $searchResult[static::KEY_SORT_ITEMS] ?? [];
172
    }
173
174
    /**
175
     * @param array $searchResult
176
     *
177
     * @return array
178
     */
179
    protected function mapFacets(array $searchResult): array
180
    {
181
        return $searchResult[static::KEY_FACETS] ?? [];
182
    }
183
}
184