1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusMailchimpPlugin\EventListener; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\EventSubscriber; |
8
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
9
|
|
|
use Odiseo\SyliusMailchimpPlugin\Handler\CustomerNewsletterSubscriptionHandlerInterface; |
10
|
|
|
use Odiseo\SyliusMailchimpPlugin\Handler\CustomerRegisterHandlerInterface; |
11
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
12
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
13
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
14
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
15
|
|
|
|
16
|
|
|
final class CustomerSubscriber implements EventSubscriber |
17
|
|
|
{ |
18
|
|
|
/** @var ChannelRepositoryInterface */ |
19
|
|
|
private $channelRepository; |
20
|
|
|
|
21
|
|
|
/** @var ChannelContextInterface */ |
22
|
|
|
private $channelContext; |
23
|
|
|
|
24
|
|
|
/** @var CustomerRegisterHandlerInterface */ |
25
|
|
|
private $customerRegisterHandler; |
26
|
|
|
|
27
|
|
|
/** @var CustomerNewsletterSubscriptionHandlerInterface */ |
28
|
|
|
private $customerNewsletterSubscriptionHandler; |
29
|
|
|
|
30
|
|
|
public function __construct( |
31
|
|
|
ChannelRepositoryInterface $channelRepository, |
32
|
|
|
ChannelContextInterface $channelContext, |
33
|
|
|
CustomerRegisterHandlerInterface $customerRegisterHandler, |
34
|
|
|
CustomerNewsletterSubscriptionHandlerInterface $customerNewsletterSubscriptionHandler |
35
|
|
|
) { |
36
|
|
|
$this->channelRepository = $channelRepository; |
37
|
|
|
$this->customerRegisterHandler = $customerRegisterHandler; |
38
|
|
|
$this->channelContext = $channelContext; |
39
|
|
|
$this->customerNewsletterSubscriptionHandler = $customerNewsletterSubscriptionHandler; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function getSubscribedEvents(): array |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'postPersist', |
49
|
|
|
'postUpdate', |
50
|
|
|
'postRemove', |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param LifecycleEventArgs $args |
56
|
|
|
*/ |
57
|
|
|
public function postPersist(LifecycleEventArgs $args): void |
58
|
|
|
{ |
59
|
|
|
$customer = $args->getEntity(); |
60
|
|
|
|
61
|
|
|
if ($customer instanceof CustomerInterface) { |
62
|
|
|
$this->register($customer); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param LifecycleEventArgs $args |
68
|
|
|
*/ |
69
|
|
|
public function postUpdate(LifecycleEventArgs $args): void |
70
|
|
|
{ |
71
|
|
|
$customer = $args->getEntity(); |
72
|
|
|
|
73
|
|
|
if ($customer instanceof CustomerInterface) { |
74
|
|
|
$this->register($customer); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param LifecycleEventArgs $args |
80
|
|
|
*/ |
81
|
|
|
public function postRemove(LifecycleEventArgs $args): void |
82
|
|
|
{ |
83
|
|
|
$customer = $args->getEntity(); |
84
|
|
|
|
85
|
|
|
if ($customer instanceof CustomerInterface) { |
86
|
|
|
$this->unregister($customer); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param CustomerInterface $customer |
92
|
|
|
*/ |
93
|
|
|
private function register(CustomerInterface $customer): void |
94
|
|
|
{ |
95
|
|
|
/** @var ChannelInterface|null $subscribedChannel */ |
96
|
|
|
$subscribedChannel = null; |
|
|
|
|
97
|
|
|
try { |
98
|
|
|
$subscribedChannel = (true === $customer->isSubscribedToNewsletter())?$this->channelContext->getChannel():null; |
99
|
|
|
} catch (\Exception $e) { |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$channels = $this->channelRepository->findAll(); |
103
|
|
|
/** @var ChannelInterface $channel */ |
104
|
|
|
foreach ($channels as $channel) { |
105
|
|
|
$isSubscribed = !!($subscribedChannel && $subscribedChannel->getCode() == $channel->getCode()); |
106
|
|
|
$this->customerRegisterHandler->register($customer, $channel, $isSubscribed); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param CustomerInterface $customer |
112
|
|
|
*/ |
113
|
|
|
private function unregister(CustomerInterface $customer): void |
114
|
|
|
{ |
115
|
|
|
$channels = $this->channelRepository->findAll(); |
116
|
|
|
|
117
|
|
|
/** @var ChannelInterface $channel */ |
118
|
|
|
foreach ($channels as $channel) { |
119
|
|
|
$this->customerRegisterHandler->unregister($customer, $channel); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|