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 SprykerFeature\Client\SelfServicePortal\Search\Expander; |
9
|
|
|
|
10
|
|
|
use Elastica\Query\BoolQuery; |
11
|
|
|
use Elastica\Query\MatchAll; |
12
|
|
|
use Elastica\Query\Terms; |
13
|
|
|
use Generated\Shared\Search\PageIndexMap; |
14
|
|
|
use Generated\Shared\Transfer\SspAssetStorageTransfer; |
15
|
|
|
use Generated\Shared\Transfer\SspModelStorageTransfer; |
16
|
|
|
use Spryker\Client\CompanyUser\CompanyUserClientInterface; |
17
|
|
|
use Spryker\Client\SearchExtension\Dependency\Plugin\QueryInterface; |
18
|
|
|
use SprykerFeature\Client\SelfServicePortal\Storage\Reader\SspAssetStorageReaderInterface; |
19
|
|
|
use SprykerFeature\Client\SelfServicePortal\Storage\Reader\SspModelStorageReaderInterface; |
20
|
|
|
|
21
|
|
|
class SspAssetQueryExpander implements SspAssetQueryExpanderInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected const PARAMETER_SSP_ASSET_REFERENCE = 'ssp-asset-reference'; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
|
|
protected SspAssetStorageReaderInterface $sspAssetStorageReader, |
30
|
|
|
protected SspModelStorageReaderInterface $sspModelStorageReader, |
31
|
|
|
protected CompanyUserClientInterface $companyUserClient |
32
|
|
|
) { |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param \Spryker\Client\SearchExtension\Dependency\Plugin\QueryInterface $searchQuery |
37
|
|
|
* @param array<string, mixed> $requestParameters |
38
|
|
|
* |
39
|
|
|
* @return \Spryker\Client\SearchExtension\Dependency\Plugin\QueryInterface |
40
|
|
|
*/ |
41
|
|
|
public function expandQuery( |
42
|
|
|
QueryInterface $searchQuery, |
43
|
|
|
array $requestParameters = [] |
44
|
|
|
): QueryInterface { |
45
|
|
|
$sspAssetReference = $requestParameters[static::PARAMETER_SSP_ASSET_REFERENCE] ?? null; |
46
|
|
|
if (!$sspAssetReference || !is_string($sspAssetReference)) { |
47
|
|
|
return $searchQuery; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$companyUserTransfer = $this->companyUserClient->findCompanyUser(); |
51
|
|
|
if (!$companyUserTransfer) { |
52
|
|
|
return $searchQuery; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$sspAssetStorageTransfer = $this->sspAssetStorageReader->findSspAssetStorageByReference($sspAssetReference, $companyUserTransfer); |
56
|
|
|
if (!$sspAssetStorageTransfer) { |
57
|
|
|
$this->applyNoResultsFilter($searchQuery); |
58
|
|
|
|
59
|
|
|
return $searchQuery; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$modelIds = $this->extractModelIds($sspAssetStorageTransfer); |
63
|
|
|
if ($modelIds === []) { |
64
|
|
|
$this->applyNoResultsFilter($searchQuery); |
65
|
|
|
|
66
|
|
|
return $searchQuery; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$sspModelStorageTransfers = $this->sspModelStorageReader->getSspModelStoragesByIds($modelIds); |
70
|
|
|
if ($sspModelStorageTransfers === []) { |
71
|
|
|
$this->applyNoResultsFilter($searchQuery); |
72
|
|
|
|
73
|
|
|
return $searchQuery; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$whitelistIds = $this->extractWhitelistIds($sspModelStorageTransfers); |
77
|
|
|
if ($whitelistIds === []) { |
78
|
|
|
$this->applyNoResultsFilter($searchQuery); |
79
|
|
|
|
80
|
|
|
return $searchQuery; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->applyWhitelistFilter($searchQuery, $whitelistIds); |
84
|
|
|
|
85
|
|
|
return $searchQuery; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param \Spryker\Client\SearchExtension\Dependency\Plugin\QueryInterface $searchQuery |
90
|
|
|
* @param list<int> $whitelistIds |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
protected function applyWhitelistFilter(QueryInterface $searchQuery, array $whitelistIds): void |
95
|
|
|
{ |
96
|
|
|
$query = $searchQuery->getSearchQuery(); |
97
|
|
|
$boolQuery = $this->getBoolQuery($query); |
98
|
|
|
|
99
|
|
|
$whitelistQuery = new Terms(PageIndexMap::PRODUCT_LISTS_WHITELISTS, $whitelistIds); |
100
|
|
|
$boolQuery->addFilter($whitelistQuery); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function applyNoResultsFilter(QueryInterface $searchQuery): void |
104
|
|
|
{ |
105
|
|
|
$query = $searchQuery->getSearchQuery(); |
106
|
|
|
$boolQuery = $this->getBoolQuery($query); |
107
|
|
|
|
108
|
|
|
$noResultsQuery = new MatchAll(); |
109
|
|
|
$boolQuery->addMustNot($noResultsQuery); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param \Generated\Shared\Transfer\SspAssetStorageTransfer $sspAssetStorageTransfer |
114
|
|
|
* |
115
|
|
|
* @return list<int> |
116
|
|
|
*/ |
117
|
|
|
protected function extractModelIds(SspAssetStorageTransfer $sspAssetStorageTransfer): array |
118
|
|
|
{ |
119
|
|
|
$modelIds = []; |
120
|
|
|
|
121
|
|
|
foreach ($sspAssetStorageTransfer->getSspModels() as $sspModelTransfer) { |
122
|
|
|
$idSspModel = $sspModelTransfer->getIdSspModel(); |
123
|
|
|
if ($idSspModel) { |
124
|
|
|
$modelIds[] = $idSspModel; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $modelIds; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param list<\Generated\Shared\Transfer\SspModelStorageTransfer> $sspModelStorageTransfers |
133
|
|
|
* |
134
|
|
|
* @return list<int> |
135
|
|
|
*/ |
136
|
|
|
protected function extractWhitelistIds(array $sspModelStorageTransfers): array |
137
|
|
|
{ |
138
|
|
|
$whitelistIds = []; |
139
|
|
|
|
140
|
|
|
foreach ($sspModelStorageTransfers as $sspModelStorageTransfer) { |
141
|
|
|
$whitelistIds[] = $this->extractWhitelistIdsFromModel($sspModelStorageTransfer); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$whitelistIds = array_merge(...$whitelistIds); |
145
|
|
|
|
146
|
|
|
return array_values(array_unique($whitelistIds)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param \Generated\Shared\Transfer\SspModelStorageTransfer $sspModelStorageTransfer |
151
|
|
|
* |
152
|
|
|
* @return list<int> |
153
|
|
|
*/ |
154
|
|
|
protected function extractWhitelistIdsFromModel(SspModelStorageTransfer $sspModelStorageTransfer): array |
155
|
|
|
{ |
156
|
|
|
$whitelistIds = []; |
157
|
|
|
|
158
|
|
|
foreach ($sspModelStorageTransfer->getWhitelists() as $productListStorageTransfer) { |
159
|
|
|
$whitelistId = $productListStorageTransfer->getIdProductList(); |
160
|
|
|
if ($whitelistId) { |
161
|
|
|
$whitelistIds[] = $whitelistId; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $whitelistIds; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param mixed $query |
170
|
|
|
* |
171
|
|
|
* @return \Elastica\Query\BoolQuery |
172
|
|
|
*/ |
173
|
|
|
protected function getBoolQuery($query): BoolQuery |
174
|
|
|
{ |
175
|
|
|
$boolQuery = $query->getQuery(); |
176
|
|
|
if (!$boolQuery instanceof BoolQuery) { |
177
|
|
|
$boolQuery = new BoolQuery(); |
178
|
|
|
$query->setQuery($boolQuery); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $boolQuery; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|