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 Odiseo\SyliusMailchimpPlugin\Provider\ListIdProviderInterface; |
12
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
13
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
14
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
15
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
16
|
|
|
|
17
|
|
|
final class CustomerSubscriber implements EventSubscriber |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ChannelRepositoryInterface |
21
|
|
|
*/ |
22
|
|
|
private $channelRepository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ChannelContextInterface |
26
|
|
|
*/ |
27
|
|
|
private $channelContext; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CustomerRegisterHandlerInterface |
31
|
|
|
*/ |
32
|
|
|
private $customerRegisterHandler; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var CustomerNewsletterSubscriptionHandlerInterface |
36
|
|
|
*/ |
37
|
|
|
private $customerNewsletterSubscriptionHandler; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param ChannelRepositoryInterface $channelRepository |
41
|
|
|
* @param ChannelContextInterface $channelContext |
42
|
|
|
* @param CustomerRegisterHandlerInterface $customerRegisterHandler |
43
|
|
|
* @param CustomerNewsletterSubscriptionHandlerInterface $customerNewsletterSubscriptionHandler |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
ChannelRepositoryInterface $channelRepository, |
47
|
|
|
ChannelContextInterface $channelContext, |
48
|
|
|
CustomerRegisterHandlerInterface $customerRegisterHandler, |
49
|
|
|
CustomerNewsletterSubscriptionHandlerInterface $customerNewsletterSubscriptionHandler |
50
|
|
|
) { |
51
|
|
|
$this->channelRepository = $channelRepository; |
52
|
|
|
$this->customerRegisterHandler = $customerRegisterHandler; |
53
|
|
|
$this->channelContext = $channelContext; |
54
|
|
|
$this->customerNewsletterSubscriptionHandler = $customerNewsletterSubscriptionHandler; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getSubscribedEvents() |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
|
|
'postPersist', |
61
|
|
|
'postUpdate', |
62
|
|
|
'postRemove', |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param LifecycleEventArgs $args |
68
|
|
|
*/ |
69
|
|
|
public function postPersist(LifecycleEventArgs $args) |
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 postUpdate(LifecycleEventArgs $args) |
82
|
|
|
{ |
83
|
|
|
$customer = $args->getEntity(); |
84
|
|
|
|
85
|
|
|
if ($customer instanceof CustomerInterface) { |
86
|
|
|
$this->register($customer); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param LifecycleEventArgs $args |
92
|
|
|
*/ |
93
|
|
|
public function postRemove(LifecycleEventArgs $args) |
94
|
|
|
{ |
95
|
|
|
$customer = $args->getEntity(); |
96
|
|
|
|
97
|
|
|
if ($customer instanceof CustomerInterface) { |
98
|
|
|
$this->unregister($customer); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param CustomerInterface $customer |
104
|
|
|
*/ |
105
|
|
|
private function register(CustomerInterface $customer) |
106
|
|
|
{ |
107
|
|
|
/** @var ChannelInterface|null $subscribedChannel */ |
108
|
|
|
$subscribedChannel = null; |
|
|
|
|
109
|
|
|
try { |
110
|
|
|
$subscribedChannel = (true === $customer->isSubscribedToNewsletter())?$this->channelContext->getChannel():null; |
111
|
|
|
} catch (\Exception $e) {} |
|
|
|
|
112
|
|
|
|
113
|
|
|
$channels = $this->channelRepository->findAll(); |
114
|
|
|
/** @var ChannelInterface $channel */ |
115
|
|
|
foreach ($channels as $channel) { |
116
|
|
|
$isSubscribed = !!($subscribedChannel && $subscribedChannel->getCode() == $channel->getCode()); |
117
|
|
|
$this->customerRegisterHandler->register($customer, $channel, $isSubscribed); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param CustomerInterface $customer |
123
|
|
|
*/ |
124
|
|
|
private function unregister(CustomerInterface $customer) |
125
|
|
|
{ |
126
|
|
|
$channels = $this->channelRepository->findAll(); |
127
|
|
|
|
128
|
|
|
/** @var ChannelInterface $channel */ |
129
|
|
|
foreach ($channels as $channel) { |
130
|
|
|
$this->customerRegisterHandler->unregister($customer, $channel); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|