-
Notifications
You must be signed in to change notification settings - Fork 83
/
ReindexProductOrderListener.php
195 lines (166 loc) · 6.14 KB
/
ReindexProductOrderListener.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
<?php
namespace Oro\Bundle\OrderBundle\EventListener\ORM;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Oro\Bundle\EntityExtendBundle\Entity\AbstractEnumValue;
use Oro\Bundle\FeatureToggleBundle\Checker\FeatureCheckerHolderTrait;
use Oro\Bundle\OrderBundle\Entity\Order;
use Oro\Bundle\OrderBundle\Provider\OrderStatusesProviderInterface;
use Oro\Bundle\ProductBundle\Search\Reindex\ProductReindexManager;
use Oro\Bundle\WebsiteBundle\Entity\Website;
/**
* Reindexes related products when order is updated or removed.
*/
class ReindexProductOrderListener
{
use FeatureCheckerHolderTrait;
/**
* This is extended field
* @see \Oro\Bundle\OrderBundle\Model\ExtendOrder::getInternalStatus()
*/
const ORDER_INTERNAL_STATUS_FIELD = 'internal_status';
/**
* @see \Oro\Bundle\OrderBundle\Entity\Order::$website
*/
const ORDER_INTERNAL_WEBSITE_FIELD = 'website';
/**
* This is extended field
* @see \Oro\Bundle\OrderBundle\Entity\Order::$customerUser
*/
const ORDER_CUSTOMER_USER_FIELD = 'customerUser';
/**
* @see \Oro\Bundle\OrderBundle\Entity\Order::$createdAt
*/
const ORDER_CREATED_AT_FIELD = 'createdAt';
/**
* @var ProductReindexManager
*/
protected $productReindexManager;
/**
* @var OrderStatusesProviderInterface
*/
protected $statusesProvider;
public function __construct(
ProductReindexManager $productReindexManager,
OrderStatusesProviderInterface $statusesProvider
) {
$this->productReindexManager = $productReindexManager;
$this->statusesProvider = $statusesProvider;
}
protected function onStatusChange(Order $order, PreUpdateEventArgs $event)
{
if ($event->hasChangedField(static::ORDER_INTERNAL_STATUS_FIELD)) {
$oldStatus = $event->getOldValue(static::ORDER_INTERNAL_STATUS_FIELD);
$newStatus = $event->getNewValue(static::ORDER_INTERNAL_STATUS_FIELD);
if ($this->isOrderStatusChangedForReindex($oldStatus, $newStatus)) {
$this->reindexProductsInOrderWithoutStatusChecking($order, $order->getWebsite());
}
}
}
protected function onWebsiteChange(Order $order, PreUpdateEventArgs $event)
{
if ($event->hasChangedField(static::ORDER_INTERNAL_WEBSITE_FIELD)) {
$oldWebsite = $event->getOldValue(static::ORDER_INTERNAL_WEBSITE_FIELD);
$newWebsite = $event->getNewValue(static::ORDER_INTERNAL_WEBSITE_FIELD);
$this->reindexProductsInOrderWithinWebsite($order, $oldWebsite);
$this->reindexProductsInOrderWithinWebsite($order, $newWebsite);
}
}
protected function onCustomerUserChange(Order $order, PreUpdateEventArgs $event)
{
if ($event->hasChangedField(static::ORDER_CUSTOMER_USER_FIELD)) {
$this->reindexProductsInOrderWithinWebsite($order, $order->getWebsite());
}
}
protected function onCreatedAtChange(Order $order, PreUpdateEventArgs $event)
{
if ($event->hasChangedField(static::ORDER_CREATED_AT_FIELD)) {
$this->reindexProductsInOrderWithinWebsite($order, $order->getWebsite());
}
}
public function processOrderUpdate(Order $order, PreUpdateEventArgs $event)
{
$this->onStatusChange($order, $event);
$this->onCreatedAtChange($order, $event);
$this->onWebsiteChange($order, $event);
$this->onCustomerUserChange($order, $event);
}
public function processOrderRemove(Order $order)
{
$this->reindexProductsInOrderWithinWebsite($order, $order->getWebsite());
}
protected function reindexProductsInOrderWithinWebsite(Order $order, Website $website = null)
{
$orderStatus = $order->getInternalStatus();
if (!$this->isAllowedStatus($orderStatus)) {
return;
}
$this->reindexProductsInOrderWithoutStatusChecking($order, $website);
}
protected function reindexProductsInOrderWithoutStatusChecking(Order $order, Website $website = null)
{
if (!$this->isReindexAllowed($website)) {
return;
}
$productIds = [];
$websiteId = $website->getId();
foreach ($order->getProductsFromLineItems() as $product) {
$productIds[] = $product->getId();
}
$this->productReindexManager->reindexProductsWithFieldGroups($productIds, $websiteId, true, ['order']);
$this->productReindexManager->reindexProductsWithFieldGroups(
$this->getParentProductIds($order),
$websiteId,
true,
['order']
);
}
/**
* @param AbstractEnumValue|null $oldStatus
* @param AbstractEnumValue|null $newStatus
* @return bool
*/
protected function isOrderStatusChangedForReindex(
AbstractEnumValue $oldStatus = null,
AbstractEnumValue $newStatus = null
) {
return ($this->isAllowedStatus($newStatus) && !$this->isAllowedStatus($oldStatus))
|| (!$this->isAllowedStatus($newStatus) && $this->isAllowedStatus($oldStatus));
}
/**
* @param AbstractEnumValue|null $status
* @return bool
*/
protected function isAllowedStatus(AbstractEnumValue $status = null)
{
$availableStatuses = $this->statusesProvider->getAvailableStatuses();
$statusId = $status !== null ? $status->getId() : null;
return in_array($statusId, $availableStatuses);
}
/**
* @param Website|null $website
* @return bool
*/
protected function isReindexAllowed(Website $website = null)
{
/**
* Ignore reindex update in case when order doesn't attached to any website
*/
if (!($website instanceof Website)) {
return false;
}
if (!$this->isFeaturesEnabled($website)) {
return false;
}
return true;
}
private function getParentProductIds(Order $order): array
{
$ids = [];
foreach ($order->getLineItems() as $lineItem) {
if ($lineItem->getParentProduct()) {
$ids[] = $lineItem->getParentProduct()->getId();
}
}
return $ids;
}
}