Passed
Push — feature/eco-3192/fact-finder-n... ( 3e466e...3cb71a )
by Volodymyr
02:30
created

FactFinderNgSearchToElasticaMapper::mapFacets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
107
        $total = $searchResult[static::KEY_TOTAL_HITS];
108
        $maxScore = max($searchResult[static::KEY_SCORE_FIRST_HIT], $searchResult[static::KEY_SCORE_LAST_HIT]);
109
        $elasticaHits = [];
110
        foreach ($searchResult[static::KEY_HITS] as $searchHit) {
111
            if (!count($searchHit[static::KEY_VARIANT_VALUES])) {
112
                continue;
113
            }
114
115
            $productAbstract = $this->productStorageClient
116
                ->findProductAbstractStorageDataByMapping(
117
                    static::SKU_MAPPING_TYPE,
118
                    $searchHit[static::KEY_OPTION_ID],
119
                    $this->currentLocale
120
                );
121
            if ($productAbstract === null) {
122
                continue;
123
            }
124
            $productAbstractImageStorageTransfer = $this->productImageStorageClient
125
                ->findProductImageAbstractStorageTransfer(
126
                    $productAbstract[static::KEY_ID_PRODUCT_ABSTRACT],
127
                    $this->currentLocale
128
                );
129
130
            $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

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