1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Client\CatalogPriceProductConnector\Plugin; |
||
9 | |||
10 | use Elastica\ResultSet; |
||
11 | use Spryker\Client\Search\Dependency\Plugin\ResultFormatterPluginInterface; |
||
12 | use Spryker\Client\Search\Plugin\Elasticsearch\ResultFormatter\AbstractElasticsearchResultFormatterPlugin; |
||
13 | |||
14 | /** |
||
15 | * @method \Spryker\Client\CatalogPriceProductConnector\CatalogPriceProductConnectorFactory getFactory() |
||
16 | */ |
||
17 | class CurrencyAwareSuggestionByTypeResultFormatter extends AbstractElasticsearchResultFormatterPlugin |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
18 | { |
||
19 | /** |
||
20 | * @var \Spryker\Client\Search\Dependency\Plugin\ResultFormatterPluginInterface|\Spryker\Client\SearchExtension\Dependency\Plugin\ResultFormatterPluginInterface |
||
21 | */ |
||
22 | protected $rawCatalogSearchResultFormatterPlugin; |
||
23 | |||
24 | /** |
||
25 | * @param \Spryker\Client\Search\Dependency\Plugin\ResultFormatterPluginInterface|\Spryker\Client\SearchExtension\Dependency\Plugin\ResultFormatterPluginInterface $rawCatalogSearchResultFormatterPlugin |
||
26 | */ |
||
27 | public function __construct(ResultFormatterPluginInterface $rawCatalogSearchResultFormatterPlugin) |
||
28 | { |
||
29 | $this->rawCatalogSearchResultFormatterPlugin = $rawCatalogSearchResultFormatterPlugin; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param \Elastica\ResultSet $searchResult |
||
34 | * @param array<string, mixed> $requestParameters |
||
35 | * |
||
36 | * @return array |
||
37 | */ |
||
38 | protected function formatSearchResult(ResultSet $searchResult, array $requestParameters) |
||
39 | { |
||
40 | $results = $this->rawCatalogSearchResultFormatterPlugin->formatResult($searchResult, $requestParameters); |
||
41 | |||
42 | if (!isset($results['product_abstract'])) { |
||
43 | return $results; |
||
44 | } |
||
45 | |||
46 | if (!$this->isPriceProductDimensionEnabled()) { |
||
47 | return $this->formatSearchResultWithoutPriceDimensions($results); |
||
48 | } |
||
49 | |||
50 | $priceProductClient = $this->getFactory()->getPriceProductClient(); |
||
51 | $priceProductStorageClient = $this->getFactory()->getPriceProductStorageClient(); |
||
52 | foreach ($results['product_abstract'] as &$product) { |
||
53 | $priceProductTransfersFromStorage = $priceProductStorageClient->getPriceProductAbstractTransfers($product['id_product_abstract']); |
||
54 | $currentProductPriceTransfer = $priceProductClient->resolveProductPriceTransfer($priceProductTransfersFromStorage); |
||
55 | $product['price'] = $currentProductPriceTransfer->getPrice(); |
||
56 | $product['prices'] = $currentProductPriceTransfer->getPrices(); |
||
57 | } |
||
58 | |||
59 | return $results; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Fallback method to work with PriceProduct module without price dimensions support. |
||
64 | * |
||
65 | * @param array $result |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | protected function formatSearchResultWithoutPriceDimensions(array $result) |
||
70 | { |
||
71 | $priceProductClient = $this->getFactory()->getPriceProductClient(); |
||
72 | foreach ($result as &$product) { |
||
73 | $currentProductPriceTransfer = $priceProductClient->resolveProductPrice($product['prices']); |
||
74 | $product['price'] = $currentProductPriceTransfer->getPrice(); |
||
75 | $product['prices'] = $currentProductPriceTransfer->getPrices(); |
||
76 | } |
||
77 | |||
78 | return $result; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return bool |
||
83 | */ |
||
84 | protected function isPriceProductDimensionEnabled(): bool |
||
85 | { |
||
86 | return defined('\Spryker\Shared\PriceProduct\PriceProductConstants::PRICE_DIMENSION_DEFAULT'); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @api |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getName() |
||
95 | { |
||
96 | return $this->rawCatalogSearchResultFormatterPlugin->getName(); |
||
97 | } |
||
98 | } |
||
99 |