Newer
Older
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
<?php
namespace NicolasBejean\CategoryWidget\Model;
use \Magento\Framework\Api\SearchCriteriaInterface;
use \Magento\Framework\Exception\LocalizedException;
use \NicolasBejean\CategoryWidget\Api\Data\CategoryWidgetInterface;
use \NicolasBejean\CategoryWidget\Api\Data\CategoryWidgetInterfaceFactory;
use \NicolasBejean\CategoryWidget\Api\Data\CategoryWidgetSearchResultsInterface;
use \NicolasBejean\CategoryWidget\Api\CategoryWidgetRepositoryInterface;
use \NicolasBejean\CategoryWidget\Api\Data;
use \NicolasBejean\CategoryWidget\Model\ResourceModel\CategoryWidget as Resource;
use \NicolasBejean\CategoryWidget\Model\ResourceModel\CategoryWidget\Collection;
use \NicolasBejean\CategoryWidget\Model\ResourceModel\CategoryWidget\CollectionFactory as CategoryWidgetCollectionFactory;
use \Magento\Framework\Api\DataObjectHelper;
use \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use \Magento\Framework\Exception\CouldNotDeleteException;
use \Magento\Framework\Exception\CouldNotSaveException;
use \Magento\Framework\Exception\NoSuchEntityException;
use \Magento\Framework\Reflection\DataObjectProcessor;
use \Magento\Store\Model\StoreManagerInterface;
use \Exception;
/**
* Class CategoryWidgetRepository
*
* @category PHP
* @package NicolasBejean\CategoryWidget\Model
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/category-widget/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class CategoryWidgetRepository implements CategoryWidgetRepositoryInterface
{
/**
* @var Resource
*/
protected $resource;
/**
* @var CategoryWidgetFactory
*/
protected $categoryWidgetFactory;
/**
* @var CategoryWidgetCollectionFactory
*/
protected $categoryWidgetCollectionFactory;
/**
* @var Data\CategoryWidgetSearchResultsInterfaceFactory
*/
protected $searchResultsFactory;
/**
* @var DataObjectHelper
*/
protected $dataObjectHelper;
/**
* @var DataObjectProcessor
*/
protected $dataObjectProcessor;
/**
* @var CategoryWidgetInterfaceFactory
*/
protected $dataCategoryWidgetFactory;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var CollectionProcessorInterface
*/
private $collectionProcessor;
/**
* @param Resource $resource
* @param CategoryWidgetFactory $categoryWidgetFactory
* @param CategoryWidgetInterfaceFactory $dataCategoryWidgetFactory
* @param CategoryWidgetCollectionFactory $categoryWidgetCollectionFactory
* @param Data\CategoryWidgetSearchResultsInterfaceFactory $searchResultsFactory
* @param DataObjectHelper $dataObjectHelper
* @param DataObjectProcessor $dataObjectProcessor
* @param StoreManagerInterface $storeManager
* @param CollectionProcessorInterface $collectionProcessor
*/
public function __construct(
Resource $resource,
CategoryWidgetFactory $categoryWidgetFactory,
CategoryWidgetInterfaceFactory $dataCategoryWidgetFactory,
CategoryWidgetCollectionFactory $categoryWidgetCollectionFactory,
Data\CategoryWidgetSearchResultsInterfaceFactory $searchResultsFactory,
DataObjectHelper $dataObjectHelper,
DataObjectProcessor $dataObjectProcessor,
StoreManagerInterface $storeManager,
CollectionProcessorInterface $collectionProcessor = null
) {
$this->resource = $resource;
$this->categoryWidgetFactory = $categoryWidgetFactory;
$this->categoryWidgetCollectionFactory = $categoryWidgetCollectionFactory;
$this->searchResultsFactory = $searchResultsFactory;
$this->dataObjectHelper = $dataObjectHelper;
$this->dataCategoryWidgetFactory = $dataCategoryWidgetFactory;
$this->dataObjectProcessor = $dataObjectProcessor;
$this->storeManager = $storeManager;
$this->collectionProcessor = $collectionProcessor;
}
/**
* Save Category Widget data
*
* @param CategoryWidgetInterface $categoryWidget
* @return CategoryWidgetInterface
* @throws CouldNotSaveException
* @throws NoSuchEntityException
*/
public function save(CategoryWidgetInterface $categoryWidget)
{
if (empty($categoryWidget->getStoreId())) {
$categoryWidget->setStoreId($this->storeManager->getStore()->getId());
}
try {
$this->resource->save($categoryWidget);
} catch (Exception $exception) {
throw new CouldNotSaveException(__($exception->getMessage()));
}
return $categoryWidget;
}
/**
* Load Category Widget data by given Image Identity
*
* @param string $id
* @return CategoryWidget
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function getById($id)
{
/** @var CategoryWidget $categoryWidget */
$categoryWidget = $this->categoryWidgetFactory->create();
$this->resource->load($categoryWidget, $id);
if (!$categoryWidget->getId()) {
throw new NoSuchEntityException(__('The category widget with the \'%1\' ID doesn\'t exist.', $id));
}
return $categoryWidget;
}
/**
* Load Category Widget data collection by given search criteria
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @param SearchCriteriaInterface $criteria
* @return CategoryWidgetSearchResultsInterface
*/
public function getList(SearchCriteriaInterface $criteria)
{
/** @var Collection $collection */
$collection = $this->categoryWidgetCollectionFactory->create();
$this->collectionProcessor->process($criteria, $collection);
/** @var CategoryWidgetSearchResultsInterface $searchResults */
$searchResults = $this->searchResultsFactory->create();
$searchResults->setSearchCriteria($criteria);
$searchResults->setItems($collection->getItems());
$searchResults->setTotalCount($collection->getSize());
return $searchResults;
}
/**
* Delete Category Widget
*
* @param CategoryWidgetInterface $categoryWidget
* @return bool
* @throws CouldNotDeleteException
*/
public function delete(CategoryWidgetInterface $categoryWidget)
{
try {
$this->resource->delete($categoryWidget);
} catch (Exception $exception) {
throw new CouldNotDeleteException(__($exception->getMessage()));
}
return true;
}
/**
* Delete Category Widget by given Category Widget Identity
*
* @param string $id
* @return bool
* @throws CouldNotDeleteException
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function deleteById($id)
{
return $this->delete($this->getById($id));
}
}