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\Model\MailchimpListIdAwareInterface; |
||
12 | use Odiseo\SyliusMailchimpPlugin\Provider\ListIdProviderInterface; |
||
13 | use Sylius\Component\Channel\Context\ChannelContextInterface; |
||
14 | use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
||
15 | use Sylius\Component\Core\Model\ChannelInterface; |
||
16 | use Sylius\Component\Core\Model\CustomerInterface; |
||
17 | |||
18 | final class CustomerSubscriber implements EventSubscriber |
||
19 | { |
||
20 | /** |
||
21 | * @var ChannelRepositoryInterface |
||
22 | */ |
||
23 | private $channelRepository; |
||
24 | |||
25 | /** |
||
26 | * @var ChannelContextInterface |
||
27 | */ |
||
28 | private $channelContext; |
||
29 | |||
30 | /** |
||
31 | * @var CustomerRegisterHandlerInterface |
||
32 | */ |
||
33 | private $customerRegisterHandler; |
||
34 | |||
35 | /** |
||
36 | * @var CustomerNewsletterSubscriptionHandlerInterface |
||
37 | */ |
||
38 | private $customerNewsletterSubscriptionHandler; |
||
39 | |||
40 | /** |
||
41 | * @var ListIdProviderInterface |
||
42 | */ |
||
43 | private $listIdProvider; |
||
44 | |||
45 | /** |
||
46 | * @param ChannelRepositoryInterface $channelRepository |
||
47 | * @param ChannelContextInterface $channelContext |
||
48 | * @param CustomerRegisterHandlerInterface $customerRegisterHandler |
||
49 | * @param CustomerNewsletterSubscriptionHandlerInterface $customerNewsletterSubscriptionHandler |
||
50 | * @param ListIdProviderInterface $listIdProvider |
||
51 | */ |
||
52 | public function __construct( |
||
53 | ChannelRepositoryInterface $channelRepository, |
||
54 | ChannelContextInterface $channelContext, |
||
55 | CustomerRegisterHandlerInterface $customerRegisterHandler, |
||
56 | CustomerNewsletterSubscriptionHandlerInterface $customerNewsletterSubscriptionHandler, |
||
57 | ListIdProviderInterface $listIdProvider |
||
58 | ) { |
||
59 | $this->channelRepository = $channelRepository; |
||
60 | $this->customerRegisterHandler = $customerRegisterHandler; |
||
61 | $this->channelContext = $channelContext; |
||
62 | $this->customerNewsletterSubscriptionHandler = $customerNewsletterSubscriptionHandler; |
||
63 | $this->listIdProvider = $listIdProvider; |
||
64 | } |
||
65 | |||
66 | public function getSubscribedEvents() |
||
67 | { |
||
68 | return [ |
||
69 | 'postPersist', |
||
70 | 'postUpdate', |
||
71 | 'postRemove', |
||
72 | ]; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param LifecycleEventArgs $args |
||
77 | */ |
||
78 | public function postPersist(LifecycleEventArgs $args) |
||
79 | { |
||
80 | $customer = $args->getEntity(); |
||
81 | |||
82 | if ($customer instanceof CustomerInterface) { |
||
83 | $this->register($customer); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param LifecycleEventArgs $args |
||
89 | */ |
||
90 | public function postUpdate(LifecycleEventArgs $args) |
||
91 | { |
||
92 | $customer = $args->getEntity(); |
||
93 | |||
94 | if ($customer instanceof CustomerInterface) { |
||
95 | $this->register($customer); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param LifecycleEventArgs $args |
||
101 | */ |
||
102 | public function postRemove(LifecycleEventArgs $args) |
||
103 | { |
||
104 | $customer = $args->getEntity(); |
||
105 | |||
106 | if ($customer instanceof CustomerInterface) { |
||
107 | $this->unregister($customer); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param CustomerInterface $customer |
||
113 | */ |
||
114 | private function register(CustomerInterface $customer) |
||
115 | { |
||
116 | try { |
||
117 | if (true === $customer->isSubscribedToNewsletter()) { |
||
118 | /** @var ChannelInterface $channel */ |
||
119 | $channel = $this->channelContext->getChannel(); |
||
120 | $this->customerNewsletterSubscriptionHandler->subscribe($customer, $this->getListIdByChannel($channel)); |
||
121 | } |
||
122 | } catch (\Exception $e) {} |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
123 | |||
124 | $channels = $this->channelRepository->findAll(); |
||
125 | /** @var ChannelInterface $channel */ |
||
126 | foreach ($channels as $channel) { |
||
127 | $this->customerRegisterHandler->register($customer, $channel); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param CustomerInterface $customer |
||
133 | */ |
||
134 | private function unregister(CustomerInterface $customer) |
||
135 | { |
||
136 | $channels = $this->channelRepository->findAll(); |
||
137 | |||
138 | /** @var ChannelInterface $channel */ |
||
139 | foreach ($channels as $channel) { |
||
140 | $this->customerRegisterHandler->unregister($customer, $channel); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param ChannelInterface $channel |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | private function getListIdByChannel(ChannelInterface $channel): string |
||
150 | { |
||
151 | if ($channel instanceof MailchimpListIdAwareInterface) { |
||
152 | if ($listId = $channel->getListId()) { |
||
153 | return $listId; |
||
154 | } |
||
155 | } |
||
156 | |||
157 | return $this->listIdProvider->getListId(); |
||
158 | } |
||
159 | } |
||
160 |