-
Notifications
You must be signed in to change notification settings - Fork 336
/
DataProvider.php
144 lines (128 loc) · 4.06 KB
/
DataProvider.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
<?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\ElasticsuiteCatalog
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
* @copyright 2020 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteCatalog\Model\Autocomplete\Product;
use Magento\Search\Model\Autocomplete\DataProviderInterface;
use Smile\ElasticsuiteCatalog\Helper\Autocomplete as ConfigurationHelper;
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Fulltext\Collection as ProductCollection;
use Smile\ElasticsuiteCore\Api\Search\ContextInterface;
/**
* Catalog product autocomplete data provider.
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
*/
class DataProvider implements DataProviderInterface
{
/**
* Autocomplete type
*/
const AUTOCOMPLETE_TYPE = "product";
/**
* Autocomplete result item factory
*
* @var ItemFactory
*/
private $itemFactory;
/**
* @var ConfigurationHelper
*/
private $configurationHelper;
/**
* @var string Autocomplete result type
*/
private $type;
/**
* @var ProductCollection
*/
private $productCollection;
/**
* @var ContextInterface
*/
private $searchContext;
/**
* Constructor.
*
* @param ItemFactory $itemFactory Suggest item factory.
* @param Collection\Provider $productCollectionProvider Product collection provider.
* @param ConfigurationHelper $configurationHelper Autocomplete configuration helper.
* @param ContextInterface $searchContext Query search context.
* @param string $type Autocomplete provider type.
*/
public function __construct(
ItemFactory $itemFactory,
Collection\Provider $productCollectionProvider,
ConfigurationHelper $configurationHelper,
ContextInterface $searchContext,
$type = self::AUTOCOMPLETE_TYPE
) {
$this->itemFactory = $itemFactory;
$this->configurationHelper = $configurationHelper;
$this->searchContext = $searchContext;
$this->type = $type;
$this->productCollection = $this->prepareProductCollection($productCollectionProvider->getProductCollection());
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* {@inheritDoc}
*/
public function getItems()
{
$result = [];
if ($this->configurationHelper->isEnabled($this->getType())) {
foreach ($this->productCollection as $product) {
$result[] = $this->itemFactory->create(['product' => $product, 'type' => $this->getType()]);
}
}
return $result;
}
/**
* Init suggested products collection.
*
* @param ProductCollection $productCollection Product collection
*
* @return ProductCollection
*/
private function prepareProductCollection(ProductCollection $productCollection)
{
$productCollection->setPageSize($this->getResultsPageSize());
$productCollection->setOrder('relevance', \Magento\Framework\Data\Collection::SORT_ORDER_ASC);
if ($searchQuery = $this->searchContext->getCurrentSearchQuery()) {
if ($searchQuery->getId()) {
$productCollection->addSortFilterParameters(
'relevance',
'search_query.position',
'search_query',
['search_query.query_id' => $searchQuery->getId()]
);
}
}
return $productCollection;
}
/**
* Retrieve number of products to display in autocomplete results
*
* @return int
*/
private function getResultsPageSize()
{
return $this->configurationHelper->getMaxSize($this->getType());
}
}