This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
AcmPromotionsManager.php
501 lines (438 loc) · 17.1 KB
/
AcmPromotionsManager.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
namespace Drupal\acm_promotion;
use Drupal\acm\Connector\APIWrapperInterface;
use Drupal\acm_sku\Entity\SKU;
use Drupal\acm_sku\Entity\SKUInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\acm\I18nHelper;
use Drupal\Core\Database\Driver\mysql\Connection;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\node\NodeInterface;
/**
* Class AcmPromotionsManager.
*/
class AcmPromotionsManager {
/**
* Language Manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Sku Entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $skuStorage;
/**
* Entity Repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Node Entity Storage.
*
* @var \Drupal\node\NodeStorageInterface
*/
private $nodeStorage;
/**
* The api wrapper.
*
* @var \Drupal\acm\Connector\APIWrapperInterface
*/
protected $apiWrapper;
/**
* Queue Factory service.
*
* @var \Drupal\Core\Queue\QueueFactory
*/
protected $queue;
/**
* Config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Database connection service.
*
* @var \Drupal\Core\Database\Driver\mysql\Connection
*/
protected $connection;
/**
* I18n Helper.
*
* @var \Drupal\acm\I18nHelper
*/
private $i18nHelper;
/**
* Constructs a new AcmPromotionsManager object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* EntityTypeManager object.
* @param \Drupal\acm\Connector\APIWrapperInterface $api_wrapper
* The api wrapper.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* LoggerFactory object.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* Language Manager service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
* Entity Repository service.
* @param \Drupal\Core\Queue\QueueFactory $queue
* Queue factory service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory service.
* @param \Drupal\Core\Database\Driver\mysql\Connection $connection
* Database connection service.
* @param \Drupal\acm\I18nHelper $i18n_helper
* I18nHelper object.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager,
APIWrapperInterface $api_wrapper,
LoggerChannelFactoryInterface $logger_factory,
LanguageManagerInterface $languageManager,
EntityRepositoryInterface $entityRepository,
QueueFactory $queue,
ConfigFactoryInterface $configFactory,
Connection $connection,
I18nHelper $i18n_helper) {
$this->nodeStorage = $entity_type_manager->getStorage('node');
$this->skuStorage = $entity_type_manager->getStorage('acm_sku');
$this->apiWrapper = $api_wrapper;
$this->logger = $logger_factory->get('acm_promotion');
$this->languageManager = $languageManager;
$this->entityRepository = $entityRepository;
$this->queue = $queue;
$this->configFactory = $configFactory;
$this->connection = $connection;
$this->i18nHelper = $i18n_helper;
}
/**
* Synchronize promotions through the API.
*
* @param mixed $types
* The type of promotion to synchronize.
*/
public function syncPromotions($types = ['category', 'cart']) {
$types = is_array($types) ? $types : [$types];
$ids = [];
$fetched_promotions = [];
foreach ($types as $type) {
$promotions = $this->apiWrapper->getPromotions($type);
foreach ($promotions as $key => $promotion) {
// Add type to $promotion array, to be saved later.
$promotion['promotion_type'] = $type;
$fetched_promotions[] = $promotion;
$ids[] = $promotion['rule_id'];
}
}
if (!empty($fetched_promotions)) {
$this->processPromotions($fetched_promotions);
}
// Delete promotions, which are not part of API response.
if (!empty($ids)) {
$this->unpublishPromotions($ids);
}
}
/**
* Delete Promotion nodes, not part of API Response.
*
* @param array $validIDs
* Valid Rule ID's from API.
*/
protected function deletePromotions(array $validIDs = []) {
$query = $this->nodeStorage->getQuery();
$query->condition('type', 'acm_promotion');
if ($validIDs) {
$query->condition('field_acm_promotion_rule_id', $validIDs, 'NOT IN');
}
$nids = $query->execute();
foreach ($nids as $nid) {
/* @var $node \Drupal\node\NodeInterface */
$node = $this->nodeStorage->load($nid);
if ($node instanceof NodeInterface) {
$node->delete();
$this->logger->notice('Deleted orphan promotion node @promotion having rule_id:@rule_id.', [
'@promotion' => $node->label(),
'@rule_id' => $node->get('field_acm_promotion_rule_id')->first()->getString(),
]);
}
}
}
/**
* Helper function to fetch promotion node given rule id.
*
* @param int $rule_id
* Rule id of the promotion to load.
* @param string $rule_type
* Rule type of the promotion to load.
*
* @return \Drupal\node\NodeInterface|null
* Return node if a promotion found associated with the rule id else Null.
*/
public function getPromotionByRuleId($rule_id, $rule_type) {
$query = $this->nodeStorage->getQuery();
$query->condition('type', 'acm_promotion');
$query->condition('field_acm_promotion_rule_id', $rule_id);
$query->condition('field_acm_promotion_type', $rule_type);
$nids = $query->execute();
if (empty($nids)) {
return NULL;
}
else {
// Log a message for admin to check errors in data.
if (count($nids) > 1) {
$this->logger->critical('Multiple nodes found for rule id @rule_id', ['@rule_id' => $rule_id]);
return NULL;
}
// We only load the first node.
/* @var $node \Drupal\node\NodeInterface */
$node = $this->nodeStorage->load(reset($nids));
$langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
// Get the promotion with language fallback, if it did not have a
// translation for $langcode.
$node = $this->entityRepository->getTranslationFromContext($node, $langcode);
return $node;
}
}
/**
* Helper function to get skus attached with a promotion.
*
* @param \Drupal\node\NodeInterface $promotion
* Promotion node for which we need to find skus.
*
* @return array
* Array of sku objects attached with the promotion.
*/
public function getSkusForPromotion(NodeInterface $promotion) {
$query = $this->connection->select('acm_sku__field_acm_sku_promotions', 'fasp');
if (\Drupal::entityTypeManager()->getDefinition('acm_sku')->isTranslatable()) {
$query->join('acm_sku_field_data', 'asfd', 'asfd.id = fasp.entity_id');
}
else {
$query->join('acm_sku', 'asfd', 'asfd.id = fasp.entity_id');
}
$query->condition('fasp.field_acm_sku_promotions_target_id', $promotion->id());
$query->fields('asfd', ['id', 'sku']);
$query->distinct();
$skus = $query->execute()->fetchAllKeyed(0, 1);
return $skus;
}
/**
* Helper function to create Promotion node from connector response.
*
* @param array $promotion
* Promotion response from Connector.
* @param \Drupal\node\NodeInterface $promotion_node
* Promotion node in case we need to update Promotion.
*
* @return \Drupal\Core\Entity\EntityInterface
* Promotion node.
*
* @throws \Drupal\Core\TypedData\Exception\ReadOnlyException
* Exception thrown when trying to write or set ready-only data.
* @throws \Exception
* Generic exception.
*/
public function syncPromotionWithConnectorResponse(array $promotion, NodeInterface $promotion_node = NULL) {
if (!$promotion_node) {
$promotion_node = $this->nodeStorage->create([
'type' => 'acm_promotion',
]);
}
$config = $this->configFactory->get('acm.connector');
$text_format = $config->get('text_format') ?: 'rich_text';
$promotions_labels = $promotion['labels'];
$promotion_label_languages = [];
$site_default_langcode = $this->languageManager->getDefaultLanguage()->getId();
$promotions_label = reset($promotion['labels']);
foreach ($promotions_labels as $promotion_label) {
$promotion_label_language = $this->i18nHelper->getLangcodeFromStoreId($promotion_label['store_id']);
// Magento might have stores that what we don't support.
if (empty($promotion_label_language)) {
continue;
}
$promotion_label_languages[$promotion_label_language] = $promotion_label['store_label'];
}
$promotion_node->get('title')->setValue($promotion['name']);
// Set the description.
$promotion_node->get('field_acm_promotion_description')->setValue(['value' => $promotion['description'], 'format' => $text_format]);
// Set promotion rule_id.
$promotion_node->get('field_acm_promotion_rule_id')->setValue($promotion['rule_id']);
// Set the status.
$promotion_node->setPublished((bool) $promotion['status']);
// Store everything as serialized string in DB.
$promotion_node->get('field_acm_promotion_data')->setValue(serialize($promotion));
// Set the Promotion type.
$promotion_node->get('field_acm_promotion_type')->setValue($promotion['promotion_type']);
// Set the Promotion label.
if (isset($promotion_label_languages[$site_default_langcode])) {
$promotion_node->get('field_acm_promotion_label')->setValue($promotion_label_languages[$site_default_langcode]);
}
// Set promotion coupon code.
$promotion_node->get('field_coupon_code')->setValue($promotion['coupon_code']);
// Set the Promotion skus.
$skus = [];
foreach ($promotion['products'] as $product) {
$sku = SKU::loadFromSku($product['product_sku']);
if ($sku instanceof SKUInterface) {
$skus[] = $product['product_sku'];
}
}
$promotion_node->get('field_skus')->setValue($skus);
// Set promotion type to percent & discount value depending on the promotion
// being imported.
if (($promotion['type'] === 'NO_COUPON') && isset($promotion['action']) && ($promotion['action'] === 'by_percent')) {
$promotion_node->get('field_acm_promotion_disc_type')->setValue('percentage');
$promotion_node->get('field_acm_promotion_discount')->setValue($promotion['discount']);
}
// Check promotion action type & store in Drupal.
if (!empty($promotion['action'])) {
$promotion_node->get('field_acm_promotion_action')->setValue($promotion['action']);
}
// Invoke the alter hook to allow modules to update the node from API data.
\Drupal::moduleHandler()->alter('acm_promotion_promotion_node', $promotion_node, $promotion);
$status = $promotion_node->save();
// Create promotion translations based on the language codes available in
// promotion labels.
foreach ($promotion_label_languages as $langcode => $promotion_label_language) {
if ($langcode !== $site_default_langcode) {
if ($promotion_node->hasTranslation($langcode)) {
$promotion_node->removeTranslation($langcode);
}
$node_translation = $promotion_node->addTranslation($langcode, $promotion_node->toArray());
$node_translation->get('field_acm_promotion_label')->setValue($promotion_label_languages[$langcode]);
$node_translation->save();
}
}
if ($status) {
return $promotion_node;
}
else {
$this->logger->critical('Error occured while creating Promotion node for rule id: @rule_id.', ['@rule_id' => $promotion['rule_id']]);
return NULL;
}
}
/**
* Helper function to process Promotions obtained from connector.
*
* @param array $promotions
* List of promotions to sync.
*
* @return array
* Messages around attach & detach queues.
*/
public function processPromotions(array $promotions = []) {
$output = [];
$acm_promotion_attach_batch_size = $this->configFactory
->get('acm_promotion.settings')
->get('promotion_attach_batch_size');
$promotion_detach_queue = $this->queue->get('acm_promotion_detach_queue');
$promotion_attach_queue = $this->queue->get('acm_promotion_attach_queue');
// Clear any outstanding items in queue before starting promotion import to
// avoid duplicate queues.
$promotion_detach_queue->deleteQueue();
$promotion_attach_queue->deleteQueue();
foreach ($promotions as $promotion) {
$fetched_promotion_skus = [];
$fetched_promotion_sku_attach_data = [];
// Extract list of sku text attached with the promotion passed.
$products = $promotion['products'];
foreach ($products as $product) {
if (!in_array($product['product_sku'], array_keys($fetched_promotion_skus))) {
$fetched_promotion_skus[$product['product_sku']] = $product['product_sku'];
$fetched_promotion_sku_attach_data[$product['product_sku']] = [
'sku' => $product['product_sku'],
];
if (($promotion['promotion_type'] === 'category') && isset($product['final_price'])) {
$fetched_promotion_sku_attach_data[$product['product_sku']]['final_price'] = $product['final_price'];
}
}
}
// Check if this promotion exists in Drupal.
// Assuming rule_id is unique across a promotion type.
$promotion_node = $this->getPromotionByRuleId($promotion['rule_id'], $promotion['promotion_type']);
// If promotion exists, we update the related skus & final price.
if ($promotion_node) {
$promotion_nid = $promotion_node->id();
// Update promotion metadata.
$this->syncPromotionWithConnectorResponse($promotion, $promotion_node);
$attached_promotion_skus = $this->getSkusForPromotion($promotion_node);
$detach_promotion_skus = [];
// Get list of skus for which promotions should be detached.
if (!empty($attached_promotion_skus)) {
$detach_promotion_skus = array_diff($attached_promotion_skus, $fetched_promotion_skus);
}
// Create a queue for removing promotions from skus.
if (!empty($detach_promotion_skus)) {
$chunks = array_chunk($detach_promotion_skus, $acm_promotion_attach_batch_size);
foreach ($chunks as $chunk) {
$data['promotion'] = $promotion_nid;
$data['skus'] = $chunk;
$promotion_detach_queue->createItem($data);
$output['detached_message'] = t(
'Skus @skus queued up to detach promotion rule: @rule_id',
[
'@skus' => implode(',', $data['skus']),
'@rule_id' => $promotion['rule_id'],
]
);
}
}
}
else {
// Create promotions node using Metadata from Promotions Object.
$promotion_node = $this->syncPromotionWithConnectorResponse($promotion);
}
// Attach promotions to skus.
if ($promotion_node && (!empty($fetched_promotion_sku_attach_data))) {
$data['promotion'] = $promotion_node->id();
$chunks = array_chunk($fetched_promotion_sku_attach_data, $acm_promotion_attach_batch_size);
foreach ($chunks as $chunk) {
$data['skus'] = $chunk;
$promotion_attach_queue->createItem($data);
$output['attached_message'] = t(
'Skus @skus queued up to attach promotion rule: @rule_id',
[
'@skus' => implode(',', array_keys($fetched_promotion_sku_attach_data)),
'@rule_id' => $promotion['rule_id'],
]
);
}
}
}
return $output;
}
/**
* Removes the given promotion from SKU entity.
*
* @param \Drupal\acm_sku\Entity\SKU $sku
* SKU Entity.
* @param int $nid
* Promotion node id.
*/
public function removeOrphanPromotionFromSku(SKU $sku, int $nid) {
$promotion_detach_item[] = ['target_id' => $nid];
$sku_promotions = $sku->get('field_acm_sku_promotions')->getValue();
$sku_promotions = array_udiff($sku_promotions, $promotion_detach_item, function ($array1, $array2) {
return $array1['target_id'] - $array2['target_id'];
});
$sku->get('field_acm_sku_promotions')->setValue($sku_promotions);
$sku->save();
// Update Sku Translations.
$translation_languages = $sku->getTranslationLanguages(TRUE);
if (!empty($translation_languages)) {
foreach ($translation_languages as $langcode => $language) {
$sku_entity_translation = $sku->getTranslation($langcode);
$sku_entity_translation->get('field_acm_sku_promotions')->setValue($sku_promotions);
$sku_entity_translation->save();
}
}
}
}