1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusMailchimpPlugin\Handler; |
6
|
|
|
|
7
|
|
|
use Odiseo\SyliusMailchimpPlugin\Api\ListsInterface; |
8
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
9
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
10
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
11
|
|
|
|
12
|
|
|
final class CustomerNewsletterSubscriptionHandler implements CustomerNewsletterSubscriptionHandlerInterface |
13
|
|
|
{ |
14
|
|
|
/** @var ListsInterface */ |
15
|
|
|
private $listsApi; |
16
|
|
|
|
17
|
|
|
/** @var bool */ |
18
|
|
|
private $enabled; |
19
|
|
|
|
20
|
|
|
/** @var EventDispatcherInterface */ |
21
|
|
|
private $eventDispatcher; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
ListsInterface $listsApi, |
25
|
|
|
EventDispatcherInterface $eventDispatcher, |
26
|
|
|
bool $enabled |
27
|
|
|
) { |
28
|
|
|
$this->listsApi = $listsApi; |
29
|
|
|
$this->eventDispatcher = $eventDispatcher; |
30
|
|
|
$this->enabled = $enabled; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function subscribe(CustomerInterface $customer, string $listId) |
37
|
|
|
{ |
38
|
|
|
if (!$this->enabled) { |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$subscriberHash = md5(strtolower($customer->getEmail())); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$getMemberResponse = $this->listsApi->getMember($listId, $subscriberHash); |
45
|
|
|
$isNew = !isset($getMemberResponse['id']); |
46
|
|
|
|
47
|
|
|
$data = [ |
48
|
|
|
'email_address' => $customer->getEmail(), |
49
|
|
|
'status' => 'subscribed', |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
if ($isNew) { |
53
|
|
|
$event = new GenericEvent($customer, ['data' => $data]); |
54
|
|
|
$this->eventDispatcher->dispatch($event, 'mailchimp.customer_newsletter.pre_add'); |
|
|
|
|
55
|
|
|
$data = $event->getArgument('data'); |
56
|
|
|
|
57
|
|
|
$response = $this->listsApi->addMember($listId, $data); |
58
|
|
|
} else { |
59
|
|
|
$event = new GenericEvent($customer, ['data' => $data, 'existing_mailchimp_member_data' => $getMemberResponse]); |
60
|
|
|
$this->eventDispatcher->dispatch($event, 'mailchimp.customer_newsletter.pre_update'); |
61
|
|
|
$data = $event->getArgument('data'); |
62
|
|
|
|
63
|
|
|
$response = $this->listsApi->updateMember($listId, $subscriberHash, $data); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $response; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function unsubscribe(CustomerInterface $customer, string $listId) |
73
|
|
|
{ |
74
|
|
|
if (!$this->enabled) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$subscriberHash = md5(strtolower($customer->getEmail())); |
|
|
|
|
79
|
|
|
|
80
|
|
|
$response = $this->listsApi->getMember($listId, $subscriberHash); |
81
|
|
|
$isNew = !isset($response['id']); |
82
|
|
|
|
83
|
|
|
if (!$isNew) { |
84
|
|
|
$event = new GenericEvent($customer, ['listId' => $listId]); |
85
|
|
|
$this->eventDispatcher->dispatch($event, 'mailchimp.customer_newsletter.pre_remove'); |
|
|
|
|
86
|
|
|
|
87
|
|
|
return $this->listsApi->removeMember($listId, $subscriberHash); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|