1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Core\Checkout\Promotion\DataAbstractionLayer; |
4
|
|
|
|
5
|
|
|
use Shopware\Core\Checkout\Promotion\Aggregate\PromotionIndividualCode\PromotionIndividualCodeDefinition; |
6
|
|
|
use Shopware\Core\Checkout\Promotion\Event\PromotionIndexerEvent; |
7
|
|
|
use Shopware\Core\Checkout\Promotion\PromotionDefinition; |
8
|
|
|
use Shopware\Core\Framework\Adapter\Cache\CacheClearer; |
9
|
|
|
use Shopware\Core\Framework\Api\Context\AdminApiSource; |
10
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory; |
11
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; |
12
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent; |
13
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexer; |
14
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexingMessage; |
15
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
16
|
|
|
|
17
|
|
|
class PromotionIndexer extends EntityIndexer |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var IteratorFactory |
21
|
|
|
*/ |
22
|
|
|
private $iteratorFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var EntityRepositoryInterface |
26
|
|
|
*/ |
27
|
|
|
private $repository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CacheClearer |
31
|
|
|
*/ |
32
|
|
|
private $cacheClearer; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var PromotionExclusionUpdater |
36
|
|
|
*/ |
37
|
|
|
private $exclusionUpdater; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var PromotionRedemptionUpdater |
41
|
|
|
*/ |
42
|
|
|
private $redemptionUpdater; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var EventDispatcherInterface |
46
|
|
|
*/ |
47
|
|
|
private $eventDispatcher; |
48
|
|
|
|
49
|
|
|
public function __construct( |
50
|
|
|
IteratorFactory $iteratorFactory, |
51
|
|
|
EntityRepositoryInterface $repository, |
52
|
|
|
CacheClearer $cacheClearer, |
53
|
|
|
PromotionExclusionUpdater $exclusionUpdater, |
54
|
|
|
PromotionRedemptionUpdater $redemptionUpdater, |
55
|
|
|
EventDispatcherInterface $eventDispatcher |
56
|
|
|
) { |
57
|
|
|
$this->iteratorFactory = $iteratorFactory; |
58
|
|
|
$this->repository = $repository; |
59
|
|
|
$this->cacheClearer = $cacheClearer; |
60
|
|
|
$this->exclusionUpdater = $exclusionUpdater; |
61
|
|
|
$this->redemptionUpdater = $redemptionUpdater; |
62
|
|
|
$this->eventDispatcher = $eventDispatcher; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getName(): string |
66
|
|
|
{ |
67
|
|
|
return 'promotion.indexer'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function iterate($offset): ?EntityIndexingMessage |
71
|
|
|
{ |
72
|
|
|
$iterator = $this->iteratorFactory->createIterator($this->repository->getDefinition(), $offset); |
73
|
|
|
|
74
|
|
|
$ids = $iterator->fetch(); |
75
|
|
|
|
76
|
|
|
if (empty($ids)) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return new PromotionIndexingMessage(array_values($ids), $iterator->getOffset()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessage |
84
|
|
|
{ |
85
|
|
|
$updates = $event->getPrimaryKeys(PromotionDefinition::ENTITY_NAME); |
86
|
|
|
|
87
|
|
|
if (empty($updates)) { |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($this->isGeneratingIndividualCode($event)) { |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return new PromotionIndexingMessage(array_values($updates), null, $event->getContext()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function handle(EntityIndexingMessage $message): void |
99
|
|
|
{ |
100
|
|
|
$ids = $message->getData(); |
101
|
|
|
$ids = array_unique(array_filter($ids)); |
102
|
|
|
|
103
|
|
|
if (empty($ids)) { |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->exclusionUpdater->update($ids); |
108
|
|
|
|
109
|
|
|
$this->redemptionUpdater->update($ids, $message->getContext()); |
110
|
|
|
|
111
|
|
|
$this->eventDispatcher->dispatch(new PromotionIndexerEvent($ids, $message->getContext())); |
112
|
|
|
|
113
|
|
|
$this->cacheClearer->invalidateIds($ids, PromotionDefinition::ENTITY_NAME); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function isGeneratingIndividualCode(EntityWrittenContainerEvent $event): bool |
117
|
|
|
{ |
118
|
|
|
$events = $event->getEvents(); |
119
|
|
|
|
120
|
|
|
if (!$event->getContext()->getSource() instanceof AdminApiSource || $events === null || $events->count() !== 2) { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$promotionIndividualWrittenEvent = $event->getEventByEntityName(PromotionIndividualCodeDefinition::ENTITY_NAME); |
125
|
|
|
|
126
|
|
|
if ($promotionIndividualWrittenEvent === null || $promotionIndividualWrittenEvent->getName() !== 'promotion_individual_code.written') { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$promotionWrittenEvent = $event->getEventByEntityName(PromotionDefinition::ENTITY_NAME); |
131
|
|
|
|
132
|
|
|
if ($promotionWrittenEvent === null || $promotionWrittenEvent->getName() !== 'promotion.written' || !empty($promotionWrittenEvent->getPayloads()[0])) { |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|