-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrandRepository.php
270 lines (251 loc) · 8.64 KB
/
BrandRepository.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
270
<?php
namespace Vuefront\Brands\Model;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\EntityManager\HydratorInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Exception\ValidatorException;
use Magento\Framework\Exception\NoSuchEntityException;
use Vuefront\Brands\Api\BrandRepositoryInterface;
use Vuefront\Brands\Api\Data\BrandInterface;
use Vuefront\Brands\Api\Data\BrandInterfaceFactory;
use Vuefront\Brands\Api\Data\BrandSearchResultsInterface;
use Vuefront\Brands\Api\Data\BrandSearchResultsInterfaceFactory;
use Vuefront\Brands\Model\ResourceModel\Brand as ResourceBrand;
use Vuefront\Brands\Model\ResourceModel\Brand\Collection;
use Vuefront\Brands\Model\ResourceModel\Brand\CollectionFactory as BrandCollectionFactory;
class BrandRepository implements BrandRepositoryInterface
{
/**
* @var array
*/
public $instances = [];
/**
* @var ResourceBrand
*/
public $resource;
/**
* @var BrandCollectionFactory
*/
public $brandCollectionFactory;
/**
* @var BrandSearchResultsInterfaceFactory
*/
public $searchResultsFactory;
/**
* @var BrandInterfaceFactory
*/
public $brandInterfaceFactory;
/**
* @var DataObjectHelper
*/
public $dataObjectHelper;
/**
* @var HydratorInterface
*/
private $hydrator;
/**
* BrandRepository constructor.
* @param ResourceBrand $resource
* @param BrandCollectionFactory $brandCollectionFactory
* @param BrandSearchResultsInterfaceFactory $brandSearchResultsInterfaceFactory
* @param BrandInterfaceFactory $brandInterfaceFactory
* @param DataObjectHelper $dataObjectHelper
* @param HydratorInterface|null $hydrator
*/
public function __construct(
ResourceBrand $resource,
BrandCollectionFactory $brandCollectionFactory,
BrandSearchResultsInterfaceFactory $brandSearchResultsInterfaceFactory,
BrandInterfaceFactory $brandInterfaceFactory,
DataObjectHelper $dataObjectHelper,
?HydratorInterface $hydrator = null
) {
$this->resource = $resource;
$this->brandCollectionFactory = $brandCollectionFactory;
$this->searchResultsFactory = $brandSearchResultsInterfaceFactory;
$this->brandInterfaceFactory = $brandInterfaceFactory;
$this->dataObjectHelper = $dataObjectHelper;
$this->hydrator = $hydrator ?: ObjectManager::getInstance()
->get(HydratorInterface::class);
}
/**
* Save Brand.
*
* @param BrandInterface $brand
* @return BrandInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function save(BrandInterface $brand)
{
/**
* @var BrandInterface|\Magento\Framework\Model\AbstractModel $brand
*/
try {
$brandId = $brand->getId();
if ($brandId && !($brand instanceof Brand && $brand->getOrigData())) {
$brand = $this->hydrator->hydrate($this->getById($brandId), $this->hydrator->extract($brand));
}
$this->resource->save($brand);
} catch (\Exception $exception) {
throw new CouldNotSaveException(
__(
'Could not save the brand: %1',
$exception->getMessage()
)
);
}
return $brand;
}
/**
* Retrieve Brand.
*
* @param int $brandId
* @return BrandInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getById(int $brandId)
{
if (!isset($this->instances[$brandId])) {
/**
* @var BrandInterface|\Magento\Framework\Model\AbstractModel $brand
*/
$brand = $this->brandInterfaceFactory->create();
$this->resource->load($brand, $brandId);
if (!$brand->getId()) {
throw new NoSuchEntityException(__('Requested brand doesn\'t exist'));
}
$this->instances[$brandId] = $brand;
}
return $this->instances[$brandId];
}
/**
* Retrieve brands matching the specified criteria.
*
* @param SearchCriteriaInterface $searchCriteria
* @return BrandSearchResultsInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getList(SearchCriteriaInterface $searchCriteria)
{
/**
* @var BrandSearchResultsInterface $searchResults
*/
$searchResults = $this->searchResultsFactory->create();
$searchResults->setSearchCriteria($searchCriteria);
/**
* @var \Vuefront\Brands\Model\ResourceModel\Brand\Collection $collection
*/
$collection = $this->brandCollectionFactory->create();
//Add filters from root filter group to the collection
/**
* @var FilterGroup $group
*/
foreach ($searchCriteria->getFilterGroups() as $group) {
$this->addFilterGroupToCollection($group, $collection);
}
$sortOrders = $searchCriteria->getSortOrders();
if ($sortOrders) {
/**
* @var SortOrder $sortOrder
*/
foreach ($searchCriteria->getSortOrders() as $sortOrder) {
$field = $sortOrder->getField();
$collection->addOrder(
$field,
($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
);
}
} else {
// set a default sorting order since this method is used constantly in many
// different blocks
$field = 'brand_id';
$collection->addOrder($field, 'ASC');
}
$collection->setCurPage($searchCriteria->getCurrentPage());
$collection->setPageSize($searchCriteria->getPageSize());
/**
* @var BrandInterface[] $categories
*/
$brands = [];
/**
* @var \Vuefront\Brands\Model\Brand $brand
*/
foreach ($collection as $brand) {
/**
* @var BrandInterface $brandDataObject
*/
$brandDataObject = $this->brandInterfaceFactory->create();
$this->dataObjectHelper->populateWithArray(
$brandDataObject,
$brand->getData(),
BrandInterface::class
);
$categories[] = $brandDataObject;
}
$searchResults->setTotalCount($collection->getSize());
return $searchResults->setItems($categories);
}
/**
* Delete brand.
*
* @param BrandInterface $brand
* @return bool true on success
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function delete(BrandInterface $brand)
{
$id = $brand->getId();
try {
unset($this->instances[$id]);
$this->resource->delete($brand);
} catch (ValidatorException $e) {
throw new CouldNotSaveException(__($e->getMessage()));
} catch (\Exception $e) {
throw new StateException(
__('Unable to remove Brand %1', $id)
);
}
unset($this->instances[$id]);
return true;
}
/**
* Delete Brand by ID.
*
* @param int $brandId
* @return bool true on success
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function deleteById($brandId)
{
$brand = $this->getById($brandId);
return $this->delete($brand);
}
/**
* Helper function that adds a FilterGroup to the collection.
*
* @param FilterGroup $filterGroup
* @param Collection $collection
* @return $this
* @throws \Magento\Framework\Exception\InputException
*/
public function addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
{
$fields = [];
$conditions = [];
foreach ($filterGroup->getFilters() as $filter) {
$condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
$fields[] = $filter->getField();
$conditions[] = [$condition => $filter->getValue()];
}
if ($fields) {
$collection->addFieldToFilter($fields, $conditions);
}
return $this;
}
}