-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathPreview.php
269 lines (236 loc) · 8.87 KB
/
Preview.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCatalogOptimizer
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2019 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer;
use Magento\Catalog\Api\Data\CategoryInterface;
use Smile\ElasticsuiteCatalogOptimizer\Api\Data\OptimizerInterface;
use Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Collection\ProviderInterface;
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;
/**
* Preview Model for Optimizer
*
* @category Smile
* @package Smile\ElasticsuiteCatalogOptimizer
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class Preview
{
/**
* @var Preview\ItemFactory
*/
private $previewItemFactory;
/**
* @var OptimizerInterface
*/
private $optimizer;
/**
* @var \Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface
*/
private $containerConfiguration;
/**
* @var \Magento\Catalog\Api\Data\CategoryInterface
*/
private $category;
/**
* @var \Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Collection\ProviderFactory
*/
private $providerFactory;
/**
* @var \Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Preview\ResultsBuilder
*/
private $previewResultsBuilder;
/**
* @var null|string
*/
private $queryText = null;
/**
* @var integer
*/
private $size;
/**
* Constructor.
*
* @param OptimizerInterface $optimizer The optimizer to preview.
* @param Preview\ItemFactory $previewItemFactory Preview item factory.
* @param ApplierListFactory $applier Preview Applier
* @param Collection\ProviderFactory $providerFactory Optimizer Provider Factory
* @param ContainerConfigurationInterface $containerConfig Container Configuration
* @param Preview\ResultsBuilder $previewResultsBuilder Preview Results Builder
* @param CategoryInterface $category Category Id to preview, if any.
* @param string $queryText Query Text.
* @param int $size Preview size.
*/
public function __construct(
OptimizerInterface $optimizer,
Preview\ItemFactory $previewItemFactory,
ApplierListFactory $applier,
Collection\ProviderFactory $providerFactory,
ContainerConfigurationInterface $containerConfig,
Preview\ResultsBuilder $previewResultsBuilder,
CategoryInterface $category = null,
$queryText = null,
$size = 10
) {
$this->size = $size;
$this->previewItemFactory = $previewItemFactory;
$this->optimizer = $optimizer;
$this->queryText = $queryText;
$this->applierListFactory = $applier;
$this->providerFactory = $providerFactory;
$this->containerConfiguration = $containerConfig;
$this->previewResultsBuilder = $previewResultsBuilder;
$this->category = $category;
}
/**
* Load preview data.
*
* @return array
*/
public function getData()
{
$baseResults = $this->getBaseResults();
$baseProducts = $this->preparePreviewItems($baseResults);
$optimizedResults = $this->canApply() ? $this->getOptimizedResults() : $baseResults;
$optimizedProducts = $this->preparePreviewItems($optimizedResults);
$effectFunction = function ($document) use ($baseProducts, $optimizedProducts) {
$document['effect'] = $this->getEffectOnProduct(
array_keys($baseProducts),
array_keys($optimizedProducts),
$document['id']
);
return $document;
};
$optimizedProducts = array_map($effectFunction, $optimizedProducts);
$data = [
'base_products' => array_values($baseProducts),
'optimized_products' => array_values($optimizedProducts),
'size' => max($baseResults->count(), $optimizedResults->count()), // Should be the same.
];
return $data;
}
/**
* Load non-optimized results.
*
* @return \Magento\Framework\Search\ResponseInterface
*/
private function getBaseResults()
{
$baseApplier = $this->getApplier($this->optimizer, ProviderInterface::TYPE_EXCLUDE);
return $this->getPreviewResults($baseApplier);
}
/**
* Load optimized results.
*
* @return \Magento\Framework\Search\ResponseInterface
*/
private function getOptimizedResults()
{
$optimizedApplier = $this->getApplier($this->optimizer, ProviderInterface::TYPE_REPLACE);
return $this->getPreviewResults($optimizedApplier);
}
/**
* Indicates if the current optmizer can be applied to the search context.
*
* @return boolean
*/
private function canApply()
{
$canApply = in_array($this->containerConfiguration->getName(), $this->optimizer->getSearchContainer());
if ($canApply && $this->containerConfiguration->getName() == 'quick_search_container') {
$config = $this->optimizer->getQuickSearchContainer();
if ($config['apply_to'] == 1) {
$queries = array_column($config['query_ids'], 'query_text');
$canApply = in_array($this->queryText, $queries);
}
} elseif ($canApply && $this->containerConfiguration->getName() == 'catalog_view_container') {
$config = $this->optimizer->getCatalogViewContainer();
if ($config['apply_to'] == 1) {
$categoryIds = array_filter($config['category_ids']);
$canApply = in_array($this->category->getId(), $categoryIds);
}
}
return $canApply;
}
/**
* @param ApplierList $applier Optimizer Applier
*
* @return \Magento\Framework\Search\ResponseInterface
*/
private function getPreviewResults($applier)
{
return $this->previewResultsBuilder->getPreviewResults(
$this->containerConfiguration,
$applier,
$this->size,
$this->queryText,
$this->category
);
}
/**
* Convert an array of products to an array of preview items.
*
* @param \Magento\Framework\Search\ResponseInterface $queryResponse The Query response, with products as documents.
*
* @return Preview\Item[]
*/
private function preparePreviewItems(\Magento\Framework\Search\ResponseInterface $queryResponse)
{
$items = [];
foreach ($queryResponse->getIterator() as $document) {
$item = $this->previewItemFactory->create(['document' => $document]);
$items[$document->getId()] = $item->getData();
}
return $items;
}
/**
* Indicates if the product position have moved after optimization.
*
* @param array $baseProductIds The base product Ids
* @param array $optimizedProductIds The optimized product Ids.
* @param int $productId The current product.
*
* @return int
*/
private function getEffectOnProduct($baseProductIds, $optimizedProductIds, $productId)
{
$result = 0;
$baseProductPosition = array_search($productId, $baseProductIds);
$optimizedProductPosition = array_search($productId, $optimizedProductIds);
if ($baseProductPosition === false && $optimizedProductPosition !== false) {
$result = 1;
} elseif ($baseProductPosition !== false && $optimizedProductPosition === false) {
$result = -1;
} elseif ($baseProductPosition !== false && $optimizedProductPosition !== false) {
if ($baseProductPosition > $optimizedProductPosition) {
$result = 1;
}
if ($baseProductPosition < $optimizedProductPosition) {
$result = -1;
}
}
return $result;
}
/**
* Retrieve applier of a given type for an optimizer.
*
* @param OptimizerInterface $optimizer The optimizer
* @param string $providerType The provider type
*
* @return \Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\ApplierList
*/
private function getApplier(OptimizerInterface $optimizer, $providerType)
{
$provider = $this->providerFactory->create($providerType, ['optimizer' => $optimizer]);
return $this->applierListFactory->create(['collectionProvider' => $provider]);
}
}