1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrewM\MailChimp; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A MailChimp Batch operation. |
7
|
|
|
* http://developer.mailchimp.com/documentation/mailchimp/reference/batches/ |
8
|
|
|
* |
9
|
|
|
* @author Drew McLellan <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Batch |
12
|
|
|
{ |
13
|
|
|
private $MailChimp; |
14
|
|
|
|
15
|
|
|
private $operations = array(); |
16
|
|
|
private $batch_id; |
17
|
|
|
|
18
|
|
|
public function __construct(MailChimp $MailChimp, $batch_id=null) |
19
|
|
|
{ |
20
|
|
|
$this->MailChimp = $MailChimp; |
21
|
|
|
$this->batch_id = $batch_id; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Add an HTTP DELETE request operation to the batch - for deleting data |
26
|
|
|
* @param string $id ID for the operation within the batch |
27
|
|
|
* @param string $method URL of the API request method |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function delete($id, $method) |
31
|
|
|
{ |
32
|
|
|
$this->operations[] = array( |
33
|
|
|
'operation_id' => $id, |
34
|
|
|
'method' => 'DELETE', |
35
|
|
|
'path' => $method, |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Add an HTTP GET request operation to the batch - for retrieving data |
41
|
|
|
* @param string $id ID for the operation within the batch |
42
|
|
|
* @param string $method URL of the API request method |
43
|
|
|
* @param array $args Assoc array of arguments (usually your data) |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function get($id, $method, $args=array()) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$this->operations[] = array( |
49
|
|
|
'operation_id' => $id, |
50
|
|
|
'method' => 'GET', |
51
|
|
|
'path' => $method, |
52
|
|
|
'params' => json_encode($args), |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Add an HTTP PATCH request operation to the batch - for performing partial updates |
58
|
|
|
* @param string $id ID for the operation within the batch |
59
|
|
|
* @param string $method URL of the API request method |
60
|
|
|
* @param array $args Assoc array of arguments (usually your data) |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function patch($id, $method, $args=array()) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->operations[] = array( |
66
|
|
|
'operation_id' => $id, |
67
|
|
|
'method' => 'PATCH', |
68
|
|
|
'path' => $method, |
69
|
|
|
'body' => json_encode($args), |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Add an HTTP POST request operation to the batch - for creating and updating items |
75
|
|
|
* @param string $id ID for the operation within the batch |
76
|
|
|
* @param string $method URL of the API request method |
77
|
|
|
* @param array $args Assoc array of arguments (usually your data) |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function post($id, $method, $args=array()) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$this->operations[] = array( |
83
|
|
|
'operation_id' => $id, |
84
|
|
|
'method' => 'POST', |
85
|
|
|
'path' => $method, |
86
|
|
|
'body' => json_encode($args), |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Add an HTTP PUT request operation to the batch - for creating new items |
92
|
|
|
* @param string $id ID for the operation within the batch |
93
|
|
|
* @param string $method URL of the API request method |
94
|
|
|
* @param array $args Assoc array of arguments (usually your data) |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public function put($id, $method, $args=array()) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$this->operations[] = array( |
100
|
|
|
'operation_id' => $id, |
101
|
|
|
'method' => 'PUT', |
102
|
|
|
'path' => $method, |
103
|
|
|
'body' => json_encode($args), |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Execute the batch request |
109
|
|
|
* @param int $timeout Request timeout in seconds (optional) |
110
|
|
|
* @return array|false Assoc array of API response, decoded from JSON |
111
|
|
|
*/ |
112
|
|
|
public function execute($timeout=10) |
113
|
|
|
{ |
114
|
|
|
$req = array('operations' => $this->operations); |
115
|
|
|
|
116
|
|
|
$result = $this->MailChimp->post('batches', $req, $timeout); |
117
|
|
|
|
118
|
|
|
if ($result && isset($result['id'])) { |
119
|
|
|
$this->batch_id = $result['id']; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $result; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Check the status of a batch request. If the current instance of the Batch object |
127
|
|
|
* was used to make the request, the batch_id is already known and is therefore optional. |
128
|
|
|
* @param string $batch_id ID of the batch about which to enquire |
129
|
|
|
* @return array|false Assoc array of API response, decoded from JSON |
130
|
|
|
*/ |
131
|
|
|
public function check_status($batch_id=null) |
132
|
|
|
{ |
133
|
|
|
if ($batch_id===null && $this->batch_id) $batch_id = $this->batch_id; |
134
|
|
|
return $this->MailChimp->get('batches/'.$batch_id); |
135
|
|
|
} |
136
|
|
|
} |
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.