1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File: PurchasedItemsAttachmentProvider.php |
7
|
|
|
* |
8
|
|
|
* @author Bartosz Kubicki [email protected]> |
9
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LizardMedia\ProductAttachment\Model\Customer; |
13
|
|
|
|
14
|
|
|
use \LizardMedia\ProductAttachment\Api\PurchasedItemsAttachmentProviderInterface; |
15
|
|
|
use \LizardMedia\ProductAttachment\Model\Attachment; |
16
|
|
|
use \LizardMedia\ProductAttachment\Model\ResourceModel\Attachment\Collection; |
17
|
|
|
use \LizardMedia\ProductAttachment\Model\ResourceModel\Attachment\CollectionFactory; |
18
|
|
|
use \Magento\Customer\Api\Data\CustomerInterface; |
19
|
|
|
use \Magento\Framework\Api\SearchCriteriaBuilder; |
20
|
|
|
use \Magento\Framework\Api\SearchCriteriaInterface; |
21
|
|
|
use \Magento\Framework\Data\Collection as DataCollection; |
22
|
|
|
use \Magento\Sales\Api\OrderItemRepositoryInterface; |
23
|
|
|
use \Magento\Sales\Api\OrderRepositoryInterface; |
24
|
|
|
use \Magento\Store\Model\StoreManagerInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class PurchasedItemsAttachmentProvider |
28
|
|
|
* @package LizardMedia\ProductAttachment\Model |
29
|
|
|
*/ |
30
|
|
|
class PurchasedItemsAttachmentProvider implements PurchasedItemsAttachmentProviderInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var \LizardMedia\ProductAttachment\Model\ResourceModel\Attachment\CollectionFactory |
34
|
|
|
*/ |
35
|
|
|
private $collectionFactory; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \Magento\Framework\Api\SearchCriteriaBuilder |
40
|
|
|
*/ |
41
|
|
|
private $searchCriteriaBuilder; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \Magento\Sales\Api\OrderItemRepositoryInterface |
46
|
|
|
*/ |
47
|
|
|
private $orderItemRepository; |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var \Magento\Sales\Api\OrderRepositoryInterface |
52
|
|
|
*/ |
53
|
|
|
private $orderRepository; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var \Magento\Store\Model\StoreManagerInterface |
58
|
|
|
*/ |
59
|
|
|
private $storeManager; |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param \LizardMedia\ProductAttachment\Model\ResourceModel\Attachment\CollectionFactory $collectionFactory |
64
|
|
|
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder |
65
|
|
|
* @param \Magento\Sales\Api\OrderItemRepositoryInterface $orderItemRepository |
66
|
|
|
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository |
67
|
|
|
* @param \Magento\Store\Model\StoreManagerInterface $storeManager |
68
|
|
|
*/ |
69
|
|
|
public function __construct( |
70
|
|
|
CollectionFactory $collectionFactory, |
71
|
|
|
SearchCriteriaBuilder $searchCriteriaBuilder, |
72
|
|
|
OrderItemRepositoryInterface $orderItemRepository, |
73
|
|
|
OrderRepositoryInterface $orderRepository, |
74
|
|
|
StoreManagerInterface $storeManager |
75
|
|
|
) { |
76
|
|
|
$this->collectionFactory = $collectionFactory; |
77
|
|
|
$this->searchCriteriaBuilder = $searchCriteriaBuilder; |
78
|
|
|
$this->orderItemRepository = $orderItemRepository; |
79
|
|
|
$this->orderRepository = $orderRepository; |
80
|
|
|
$this->storeManager = $storeManager; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
* @param \Magento\Customer\Api\Data\CustomerInterface $customer |
87
|
|
|
* |
88
|
|
|
* @throws \Exception |
89
|
|
|
* |
90
|
|
|
* @return mixed \LizardMedia\ProductAttachment\Model\ResourceModel\Attachment\Collection | null |
91
|
|
|
*/ |
92
|
|
|
public function get(CustomerInterface $customer) : Collection |
93
|
|
|
{ |
94
|
|
|
$orders = $this->getCustomerOrders($customer); |
95
|
|
|
|
96
|
|
|
if (empty($orders)) { |
97
|
|
|
return $this->returnEmptyAttachmentCollection(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$orderIds = []; |
101
|
|
|
foreach ($orders as $order) { |
102
|
|
|
$orderIds[] = $order->getEntityId(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$orderItems = $this->getOrderItems($orderIds); |
106
|
|
|
|
107
|
|
|
if (empty($orderItems)) { |
108
|
|
|
return $this->returnEmptyAttachmentCollection(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$productIds = []; |
112
|
|
|
foreach ($orderItems as $orderItem) { |
113
|
|
|
$productIds[] = $orderItem->getProductId(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->getAttachments($productIds); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param \Magento\Customer\Api\Data\CustomerInterface $customer |
122
|
|
|
* |
123
|
|
|
* @return \Magento\Sales\Api\Data\OrderInterface[] |
124
|
|
|
*/ |
125
|
|
|
private function getCustomerOrders(CustomerInterface $customer) : array |
126
|
|
|
{ |
127
|
|
|
$searchCriteria = $this->prepareCustomerOrdersSearchCriteria($customer); |
128
|
|
|
$searchResult = $this->orderRepository->getList($searchCriteria); |
129
|
|
|
if ($searchResult->getTotalCount() > 0) { |
130
|
|
|
return $searchResult->getItems(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return []; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param \Magento\Customer\Api\Data\CustomerInterface $customer |
139
|
|
|
* |
140
|
|
|
* @return \Magento\Framework\Api\SearchCriteriaInterface |
141
|
|
|
*/ |
142
|
|
|
private function prepareCustomerOrdersSearchCriteria(CustomerInterface $customer) : SearchCriteriaInterface |
143
|
|
|
{ |
144
|
|
|
$this->searchCriteriaBuilder->addFilter('customer_id', $customer->getId(), 'eq'); |
145
|
|
|
return $this->searchCriteriaBuilder->create(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param array $orderIds |
151
|
|
|
* |
152
|
|
|
* @return \Magento\Sales\Api\Data\OrderItemInterface[] |
153
|
|
|
*/ |
154
|
|
|
private function getOrderItems(array $orderIds) : array |
155
|
|
|
{ |
156
|
|
|
$searchCriteria = $this->prepareOrderItemsSearchCriteria($orderIds); |
157
|
|
|
$searchResult = $this->orderItemRepository->getList($searchCriteria); |
158
|
|
|
if ($searchResult->getTotalCount() > 0) { |
159
|
|
|
return $searchResult->getItems(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return []; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param array $orderItemsIds |
168
|
|
|
* |
169
|
|
|
* @return \Magento\Framework\Api\SearchCriteriaInterface |
170
|
|
|
*/ |
171
|
|
|
private function prepareOrderItemsSearchCriteria(array $orderItemsIds) : SearchCriteriaInterface |
172
|
|
|
{ |
173
|
|
|
$this->searchCriteriaBuilder->addFilter('order_id', $orderItemsIds, 'in'); |
174
|
|
|
return $this->searchCriteriaBuilder->create(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @throws \Exception |
180
|
|
|
* |
181
|
|
|
* @return \LizardMedia\ProductAttachment\Model\ResourceModel\Attachment\Collection |
182
|
|
|
*/ |
183
|
|
|
private function returnEmptyAttachmentCollection() |
184
|
|
|
{ |
185
|
|
|
$collection = $this->instantiateAttachmentCollection(); |
186
|
|
|
$collection->addProductToFilter([]); |
187
|
|
|
return $collection; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param array $productIds |
193
|
|
|
* |
194
|
|
|
* @throws \Exception |
195
|
|
|
* |
196
|
|
|
* @return \LizardMedia\ProductAttachment\Model\ResourceModel\Attachment\Collection |
197
|
|
|
*/ |
198
|
|
|
private function getAttachments(array $productIds) : Collection |
199
|
|
|
{ |
200
|
|
|
$collection = $this->instantiateAttachmentCollection(); |
201
|
|
|
$collection->addProductToFilter($productIds) |
202
|
|
|
->addTitleToResult((int) $this->storeManager->getStore()->getId()); |
203
|
|
|
|
204
|
|
|
$collection->joinProductTitle((int) $this->storeManager->getStore()->getId()); |
205
|
|
|
$collection->setOrder(Attachment::PRODUCT_ID, DataCollection::SORT_ORDER_DESC); |
206
|
|
|
|
207
|
|
|
return $collection; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return \LizardMedia\ProductAttachment\Model\ResourceModel\Attachment\Collection |
213
|
|
|
*/ |
214
|
|
|
private function instantiateAttachmentCollection() : Collection |
215
|
|
|
{ |
216
|
|
|
return $this->collectionFactory->create(); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|