1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the MailChimpEcommerceBundle package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017 kevin92dev.es |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* Feel free to edit as you please, and have fun. |
11
|
|
|
* |
12
|
|
|
* @author Kevin Murillo <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Kevin92dev\MailChimpEcommerceBundle\Services; |
16
|
|
|
|
17
|
|
|
use Kevin92dev\MailChimpEcommerceBundle\Entities\Customer; |
18
|
|
|
use Kevin92dev\MailChimpEcommerceBundle\Entities\Address; |
19
|
|
|
use Kevin92dev\MailChimpEcommerceBundle\Exceptions\CustomerNotFoundException; |
20
|
|
|
use Kevin92dev\MailChimpEcommerceBundle\RequestTypes; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* CustomerService |
24
|
|
|
* |
25
|
|
|
* @author Kevin Murillo <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class CustomerService |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var MailChimp |
31
|
|
|
*/ |
32
|
|
|
private $mailChimp; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initializes CustomerService |
36
|
|
|
* |
37
|
|
|
* @param MailChimp $mailChimp |
38
|
|
|
*/ |
39
|
|
|
public function __construct(MailChimp $mailChimp) |
40
|
|
|
{ |
41
|
|
|
$this->mailChimp = $mailChimp; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a new customer in MailChimp |
46
|
|
|
* |
47
|
|
|
* @var Customer $customer |
48
|
|
|
*/ |
49
|
|
|
public function create(Customer $customer) |
50
|
|
|
{ |
51
|
|
|
$method = RequestTypes::$POST; |
52
|
|
|
$resource = '/customers'; |
53
|
|
|
|
54
|
|
|
$body = [ |
55
|
|
|
'id' => strval($customer->getId()), |
56
|
|
|
'email_address' => strval($customer->getEmail()), |
57
|
|
|
'opt_in_status' => boolval($customer->isOptInStatus()), |
58
|
|
|
'company' => strval($customer->getCompany()), |
59
|
|
|
'first_name' => strval($customer->getFirstname()), |
60
|
|
|
'last_name' => strval($customer->getLastname()), |
61
|
|
|
'orders_count' => intval($customer->getOrdersCount()), |
62
|
|
|
'total_spent' => floatval($customer->getTotalSpent()), |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
if ($customer->getAddress() instanceof Address) { |
66
|
|
|
$this->addAddress($customer, $body); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->mailChimp->doRequest($method, $body, $resource); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Edit a customer in MailChimp |
74
|
|
|
* |
75
|
|
|
* @var Customer $customer |
76
|
|
|
*/ |
77
|
|
|
public function edit(Customer $customer) |
78
|
|
|
{ |
79
|
|
|
$method = RequestTypes::$PATCH; |
80
|
|
|
$resource = '/customers/'.$customer->getId(); |
81
|
|
|
|
82
|
|
|
$body = [ |
83
|
|
|
'opt_in_status' => boolval($customer->isOptInStatus()), |
84
|
|
|
'company' => strval($customer->getCompany()), |
85
|
|
|
'first_name' => strval($customer->getFirstname()), |
86
|
|
|
'last_name' => strval($customer->getLastname()), |
87
|
|
|
'orders_count' => intval($customer->getOrdersCount()), |
88
|
|
|
'total_spent' => floatval($customer->getTotalSpent()) |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
if ($customer->getAddress() instanceof Address) { |
92
|
|
|
$this->addAddress($customer, $body); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->mailChimp->doRequest($method, $body, $resource); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Read customers from MailChimp |
100
|
|
|
* |
101
|
|
|
* @param Customer $customer |
102
|
|
|
* @param int $count |
103
|
|
|
* @param int $offset |
104
|
|
|
* |
105
|
|
|
* @return null|string |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function read(Customer $customer = null, $count = 50, $offset = 0) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$method = RequestTypes::$GET; |
110
|
|
|
|
111
|
|
|
if ($customer instanceof Customer) { |
112
|
|
|
$resource = '/customers/'.$customer->getId(); |
113
|
|
|
} else { |
114
|
|
|
$resource = '/customers'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$resource .= '?count='.$count.'&offset='.$offset; |
118
|
|
|
|
119
|
|
|
$data = []; |
120
|
|
|
|
121
|
|
|
try { |
122
|
|
|
$request = $this->mailChimp->doRequest($method, $data, $resource); |
123
|
|
|
} catch (CustomerNotFoundException $e) { |
124
|
|
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $request->getBody()->getContents(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Delete a customer from MailChimp |
132
|
|
|
* |
133
|
|
|
* @var Customer $customer |
134
|
|
|
*/ |
135
|
|
|
public function delete(Customer $customer) |
136
|
|
|
{ |
137
|
|
|
$method = RequestTypes::$DELETE; |
138
|
|
|
$resource = '/customers/'.$customer->getId(); |
139
|
|
|
|
140
|
|
|
$this->mailChimp->doRequest($method, null, $resource); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Add address to a customer |
145
|
|
|
* |
146
|
|
|
* @param Customer $customer |
147
|
|
|
* @param array $body |
148
|
|
|
*/ |
149
|
|
|
public function addAddress(Customer $customer, &$body) |
150
|
|
|
{ |
151
|
|
|
$address = $customer->getAddress(); |
152
|
|
|
|
153
|
|
|
$body['address'] = [ |
154
|
|
|
'address1' => strval($address->getAddress1()), |
155
|
|
|
'address2' => strval($address->getAddress2()), |
156
|
|
|
'city' => strval($address->getCity()), |
157
|
|
|
'province' => strval($address->getProvince()), |
158
|
|
|
'province_code' => strval($address->getProvinceCode()), |
159
|
|
|
'postal_code' => strval($address->getPostalCode()), |
160
|
|
|
'country' => strval($address->getCountry()), |
161
|
|
|
'country_code' => strval($address->getCountryCode()) |
162
|
|
|
]; |
163
|
|
|
} |
164
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.