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 | |||
12 | final class CustomerRegisterHandler implements CustomerRegisterHandlerInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var EcommerceInterface |
||
16 | */ |
||
17 | private $ecommerceApi; |
||
18 | |||
19 | /** |
||
20 | * @var bool |
||
21 | */ |
||
22 | private $enabled; |
||
23 | |||
24 | /** |
||
25 | * @param EcommerceInterface $ecommerceApi |
||
26 | * @param bool $enabled |
||
27 | */ |
||
28 | public function __construct( |
||
29 | EcommerceInterface $ecommerceApi, |
||
30 | bool $enabled |
||
31 | ) { |
||
32 | $this->ecommerceApi = $ecommerceApi; |
||
33 | $this->enabled = $enabled; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param CustomerInterface $customer |
||
38 | * @param ChannelInterface $channel |
||
39 | * |
||
40 | * @return array|false |
||
41 | */ |
||
42 | public function register(CustomerInterface $customer, ChannelInterface $channel) |
||
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 | |||
52 | $response = $this->ecommerceApi->getCustomer($storeId, $customerId); |
||
53 | $isNew = !isset($response['id']); |
||
54 | |||
55 | $firstName = $this->getCustomerFirstName($customer, $customerAddress); |
||
56 | $lastName = $this->getCustomerLastName($customer, $customerAddress); |
||
57 | |||
58 | $data = [ |
||
59 | 'id' => $customerId, |
||
60 | 'email_address' => $customer->getEmail(), |
||
61 | 'opt_in_status' => false, |
||
62 | 'first_name' => $firstName ?: '', |
||
63 | 'last_name' => $lastName ?: '', |
||
64 | ]; |
||
65 | |||
66 | if ($customerAddress) { |
||
67 | $data['company'] = $customerAddress->getCompany() ?: ''; |
||
68 | $data['address'] = [ |
||
69 | 'address1' => $customerAddress->getStreet() ?: '', |
||
70 | 'city' => $customerAddress->getCity() ?: '', |
||
71 | 'province' => $customerAddress->getProvinceName() ?: '', |
||
72 | 'province_code' => $customerAddress->getProvinceCode() ?: '', |
||
73 | 'postal_code' => $customerAddress->getPostcode() ?: '', |
||
74 | 'country_code' => $customerAddress->getCountryCode() ?: '', |
||
75 | ]; |
||
76 | } |
||
77 | |||
78 | if ($isNew) { |
||
79 | $response = $this->ecommerceApi->addCustomer($storeId, $data); |
||
80 | } else { |
||
81 | $response = $this->ecommerceApi->updateCustomer($storeId, $customerId, $data); |
||
82 | } |
||
83 | |||
84 | return $response; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | public function unregister(CustomerInterface $customer, ChannelInterface $channel) |
||
91 | { |
||
92 | if (!$this->enabled) { |
||
93 | return false; |
||
94 | } |
||
95 | |||
96 | $customerId = (string) $customer->getId(); |
||
97 | $storeId = $channel->getCode(); |
||
98 | |||
99 | $response = $this->ecommerceApi->getCustomer($storeId, $customerId); |
||
100 | $isNew = !isset($response['id']); |
||
101 | |||
102 | if (!$isNew) { |
||
103 | return $this->ecommerceApi->removeCustomer($storeId, $customerId); |
||
104 | } |
||
105 | |||
106 | return false; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param CustomerInterface $customer |
||
111 | * |
||
112 | * @return AddressInterface|null |
||
113 | */ |
||
114 | private function getCustomerAddress(CustomerInterface $customer): ?AddressInterface |
||
115 | { |
||
116 | $address = $customer->getDefaultAddress(); |
||
117 | |||
118 | if (!$address && count($customer->getAddresses()) > 0) { |
||
119 | $address = $customer->getAddresses()->first(); |
||
120 | } |
||
121 | |||
122 | return $address; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param CustomerInterface $customer |
||
127 | * @param AddressInterface|null $address |
||
128 | * |
||
129 | * @return string|null |
||
130 | */ |
||
131 | private function getCustomerFirstName(CustomerInterface $customer, AddressInterface $address = null): ?string |
||
132 | { |
||
133 | $firstName = $customer->getFirstName(); |
||
134 | |||
135 | if (!$firstName && $address) { |
||
136 | $firstName = $address->getFirstName(); |
||
137 | } |
||
138 | |||
139 | return $firstName; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param CustomerInterface $customer |
||
144 | * @param AddressInterface|null $address |
||
145 | * |
||
146 | * @return string|null |
||
147 | */ |
||
148 | private function getCustomerLastName(CustomerInterface $customer, AddressInterface $address = null): ?string |
||
149 | { |
||
150 | $lastName = $customer->getLastName(); |
||
151 | |||
152 | if (!$lastName && $address) { |
||
153 | $lastName = $address->getLastName(); |
||
154 | } |
||
155 | |||
156 | return $lastName; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param CustomerInterface $customer |
||
161 | * @param AddressInterface|null $address |
||
162 | * |
||
163 | * @return string|null |
||
164 | */ |
||
165 | private function getCustomerPhoneNumber(CustomerInterface $customer, AddressInterface $address = null): ?string |
||
0 ignored issues
–
show
|
|||
166 | { |
||
167 | $phoneNumber = $customer->getPhoneNumber(); |
||
168 | |||
169 | if (!$phoneNumber && $address) { |
||
170 | $phoneNumber = $address->getPhoneNumber(); |
||
171 | } |
||
172 | |||
173 | return $phoneNumber; |
||
174 | } |
||
175 | } |
||
176 |
This check looks for private methods that have been defined, but are not used inside the class.