-
Notifications
You must be signed in to change notification settings - Fork 57
/
DataProvider.php
157 lines (142 loc) · 4.55 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
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future.
*
* @category Smile
* @package Smile\StoreLocator
* @author Fanny DECLERCK <fadec@smile.fr>
* @copyright 2020 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\StoreLocator\Model\Autocomplete;
use Magento\Search\Model\QueryFactory;
use Magento\Search\Model\Autocomplete\ItemFactory;
use Magento\Store\Model\StoreManagerInterface;
use Smile\ElasticsuiteCore\Helper\Autocomplete as ConfigurationHelper;
use Smile\ElasticsuiteRetailer\Model\ResourceModel\Fulltext\CollectionFactory as RetailerCollectionFactory;
/**
* Retailer autocomplete data provider.
*
* @category Smile
* @package Smile\StoreLocator
* @author Fanny DECLERCK <fadec@smile.fr>
*/
class DataProvider
{
/**
* Autocomplete type
*/
const AUTOCOMPLETE_TYPE = "search_retailer";
/**
* Autocomplete result item factory
*
* @var ItemFactory
*/
protected $itemFactory;
/**
* Query factory
*
* @var QueryFactory
*/
protected $queryFactory;
/**
* @var RetailerCollectionFactory
*/
protected $retailerCollectionFactory;
/**
* @var ConfigurationHelper
*/
protected $configurationHelper;
/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @var string Autocomplete result type
*/
private $type;
/**
* @var \Smile\StoreLocator\Helper\Data
*/
protected $storeLocatorHelper;
/**
* Constructor.
*
* @param ItemFactory $itemFactory Suggest item factory.
* @param QueryFactory $queryFactory Search query factory.
* @param RetailerCollectionFactory $retailerCollectionFactory Retailer collection factory.
* @param ConfigurationHelper $configurationHelper Autocomplete configuration helper.
* @param StoreManagerInterface $storeManager Store manager.
* @param \Smile\StoreLocator\Helper\Data $storeLocatorHelper StoreLocator Helper.
* @param string $type Autocomplete provider type.
*/
public function __construct(
ItemFactory $itemFactory,
QueryFactory $queryFactory,
RetailerCollectionFactory $retailerCollectionFactory,
ConfigurationHelper $configurationHelper,
StoreManagerInterface $storeManager,
\Smile\StoreLocator\Helper\Data $storeLocatorHelper,
$type = self::AUTOCOMPLETE_TYPE
) {
$this->itemFactory = $itemFactory;
$this->queryFactory = $queryFactory;
$this->retailerCollectionFactory = $retailerCollectionFactory;
$this->configurationHelper = $configurationHelper;
$this->type = $type;
$this->storeManager = $storeManager;
$this->storeLocatorHelper = $storeLocatorHelper;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Return items.
*
* @param string $query Query text.
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getItems($query)
{
$result = [];
$retailerCollection = $this->getRetailerCollection($query);
if ($retailerCollection) {
foreach ($retailerCollection as $retailer) {
$result[] = $this->itemFactory->create(
[
'title' => $retailer->getName(),
'url' => $this->storeLocatorHelper->getRetailerUrl($retailer),
'type' => $this->getType(),
]
);
}
}
return $result;
}
/**
* Suggested retailer collection.
* Returns null if no suggested search terms.
*
* @param string $query Query text.
*
* @return \Smile\ElasticsuiteRetailer\Model\ResourceModel\Fulltext\Collection|null
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function getRetailerCollection($terms)
{
$retailerCollection = null;
$retailerCollection = $this->retailerCollectionFactory->create();
$retailerCollection->addAttributeToSelect(['name', 'url_key']);
$retailerCollection->addSearchFilter($terms);
return $retailerCollection;
}
}