|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusMailchimpPlugin\Handler; |
|
6
|
|
|
|
|
7
|
|
|
use Odiseo\SyliusMailchimpPlugin\Api\EcommerceInterface; |
|
8
|
|
|
use Sylius\Component\Core\Model\AddressInterface; |
|
9
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
|
10
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
|
12
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
|
13
|
|
|
|
|
14
|
|
|
final class CustomerRegisterHandler implements CustomerRegisterHandlerInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var EcommerceInterface */ |
|
17
|
|
|
private $ecommerceApi; |
|
18
|
|
|
|
|
19
|
|
|
/** @var EventDispatcherInterface */ |
|
20
|
|
|
private $eventDispatcher; |
|
21
|
|
|
|
|
22
|
|
|
/** @var bool */ |
|
23
|
|
|
private $enabled; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct( |
|
26
|
|
|
EcommerceInterface $ecommerceApi, |
|
27
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
28
|
|
|
bool $enabled |
|
29
|
|
|
) { |
|
30
|
|
|
$this->ecommerceApi = $ecommerceApi; |
|
31
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
32
|
|
|
$this->enabled = $enabled; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function register( |
|
39
|
|
|
CustomerInterface $customer, |
|
40
|
|
|
ChannelInterface $channel, |
|
41
|
|
|
bool $optInStatus = false, |
|
42
|
|
|
bool $createOnly = false |
|
43
|
|
|
) { |
|
44
|
|
|
if (!$this->enabled) { |
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$customerId = (string) $customer->getId(); |
|
49
|
|
|
$storeId = $channel->getCode(); |
|
50
|
|
|
$customerAddress = $this->getCustomerAddress($customer); |
|
51
|
|
|
$firstName = $this->getCustomerFirstName($customer, $customerAddress); |
|
52
|
|
|
$lastName = $this->getCustomerLastName($customer, $customerAddress); |
|
53
|
|
|
|
|
54
|
|
|
$response = $this->ecommerceApi->getCustomer($storeId, $customerId); |
|
|
|
|
|
|
55
|
|
|
$isNew = !isset($response['id']); |
|
56
|
|
|
|
|
57
|
|
|
// Do nothing if the customer exists |
|
58
|
|
|
if (false === $isNew && true === $createOnly) { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$data = [ |
|
63
|
|
|
'id' => $customerId, |
|
64
|
|
|
'email_address' => $customer->getEmail(), |
|
65
|
|
|
'opt_in_status' => $optInStatus, |
|
66
|
|
|
'first_name' => $firstName ?: '', |
|
67
|
|
|
'last_name' => $lastName ?: '', |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
if ($customerAddress) { |
|
|
|
|
|
|
71
|
|
|
$data['company'] = $customerAddress->getCompany() ?: ''; |
|
72
|
|
|
$data['address'] = [ |
|
73
|
|
|
'address1' => $customerAddress->getStreet() ?: '', |
|
74
|
|
|
'city' => $customerAddress->getCity() ?: '', |
|
75
|
|
|
'province' => $customerAddress->getProvinceName() ?: '', |
|
76
|
|
|
'province_code' => $customerAddress->getProvinceCode() ?: '', |
|
77
|
|
|
'postal_code' => $customerAddress->getPostcode() ?: '', |
|
78
|
|
|
'country_code' => $customerAddress->getCountryCode() ?: '', |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($isNew) { |
|
83
|
|
|
$event = new GenericEvent($customer, ['data' => $data, 'channel' => $channel]); |
|
84
|
|
|
$this->eventDispatcher->dispatch($event, 'mailchimp.customer.pre_add'); |
|
|
|
|
|
|
85
|
|
|
$data = $event->getArgument('data'); |
|
86
|
|
|
|
|
87
|
|
|
$response = $this->ecommerceApi->addCustomer($storeId, $data); |
|
|
|
|
|
|
88
|
|
|
} else { |
|
89
|
|
|
$event = new GenericEvent($customer, ['data' => $data, 'channel' => $channel]); |
|
90
|
|
|
$this->eventDispatcher->dispatch($event, 'mailchimp.customer.pre_update'); |
|
91
|
|
|
$data = $event->getArgument('data'); |
|
92
|
|
|
|
|
93
|
|
|
$response = $this->ecommerceApi->updateCustomer($storeId, $customerId, $data); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $response; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function unregister(CustomerInterface $customer, ChannelInterface $channel) |
|
103
|
|
|
{ |
|
104
|
|
|
if (!$this->enabled) { |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$customerId = (string) $customer->getId(); |
|
109
|
|
|
$storeId = $channel->getCode(); |
|
110
|
|
|
|
|
111
|
|
|
$response = $this->ecommerceApi->getCustomer($storeId, $customerId); |
|
|
|
|
|
|
112
|
|
|
$isNew = !isset($response['id']); |
|
113
|
|
|
|
|
114
|
|
|
if (!$isNew) { |
|
115
|
|
|
$event = new GenericEvent($customer, ['channel' => $channel]); |
|
116
|
|
|
$this->eventDispatcher->dispatch($event, 'mailchimp.customer.pre_remove'); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
return $this->ecommerceApi->removeCustomer($storeId, $customerId); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param CustomerInterface $customer |
|
126
|
|
|
* @return AddressInterface|null |
|
127
|
|
|
*/ |
|
128
|
|
|
private function getCustomerAddress(CustomerInterface $customer): ?AddressInterface |
|
129
|
|
|
{ |
|
130
|
|
|
$address = $customer->getDefaultAddress(); |
|
131
|
|
|
|
|
132
|
|
|
if (!$address && count($customer->getAddresses()) > 0) { |
|
133
|
|
|
$address = $customer->getAddresses()->first(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $address; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param CustomerInterface $customer |
|
141
|
|
|
* @param AddressInterface|null $address |
|
142
|
|
|
* @return string|null |
|
143
|
|
|
*/ |
|
144
|
|
|
private function getCustomerFirstName(CustomerInterface $customer, AddressInterface $address = null): ?string |
|
145
|
|
|
{ |
|
146
|
|
|
$firstName = $customer->getFirstName(); |
|
147
|
|
|
|
|
148
|
|
|
if (!$firstName && $address) { |
|
149
|
|
|
$firstName = $address->getFirstName(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $firstName; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param CustomerInterface $customer |
|
157
|
|
|
* @param AddressInterface|null $address |
|
158
|
|
|
* @return string|null |
|
159
|
|
|
*/ |
|
160
|
|
|
private function getCustomerLastName(CustomerInterface $customer, AddressInterface $address = null): ?string |
|
161
|
|
|
{ |
|
162
|
|
|
$lastName = $customer->getLastName(); |
|
163
|
|
|
|
|
164
|
|
|
if (!$lastName && $address) { |
|
165
|
|
|
$lastName = $address->getLastName(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $lastName; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|