|
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->queue_operation('DELETE', $id, $method); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Add an HTTP GET request operation to the batch - for retrieving data |
|
37
|
|
|
* @param string $id ID for the operation within the batch |
|
38
|
|
|
* @param string $method URL of the API request method |
|
39
|
|
|
* @param array $args Assoc array of arguments (usually your data) |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
|
|
public function get($id, $method, $args=array()) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->queue_operation('GET', $id, $method, $args); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Add an HTTP PATCH request operation to the batch - for performing partial updates |
|
49
|
|
|
* @param string $id ID for the operation within the batch |
|
50
|
|
|
* @param string $method URL of the API request method |
|
51
|
|
|
* @param array $args Assoc array of arguments (usually your data) |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function patch($id, $method, $args=array()) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->queue_operation('PATCH', $id, $method, $args); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Add an HTTP POST request operation to the batch - for creating and updating items |
|
61
|
|
|
* @param string $id ID for the operation within the batch |
|
62
|
|
|
* @param string $method URL of the API request method |
|
63
|
|
|
* @param array $args Assoc array of arguments (usually your data) |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
public function post($id, $method, $args=array()) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->queue_operation('POST', $id, $method, $args); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Add an HTTP PUT request operation to the batch - for creating new items |
|
73
|
|
|
* @param string $id ID for the operation within the batch |
|
74
|
|
|
* @param string $method URL of the API request method |
|
75
|
|
|
* @param array $args Assoc array of arguments (usually your data) |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
public function put($id, $method, $args=array()) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->queue_operation('PUT', $id, $method, $args); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Execute the batch request |
|
85
|
|
|
* @param int $timeout Request timeout in seconds (optional) |
|
86
|
|
|
* @return array|false Assoc array of API response, decoded from JSON |
|
87
|
|
|
*/ |
|
88
|
|
|
public function execute($timeout=10) |
|
89
|
|
|
{ |
|
90
|
|
|
$req = array('operations' => $this->operations); |
|
91
|
|
|
|
|
92
|
|
|
$result = $this->MailChimp->post('batches', $req, $timeout); |
|
93
|
|
|
|
|
94
|
|
|
if ($result && isset($result['id'])) { |
|
95
|
|
|
$this->batch_id = $result['id']; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $result; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Check the status of a batch request. If the current instance of the Batch object |
|
103
|
|
|
* was used to make the request, the batch_id is already known and is therefore optional. |
|
104
|
|
|
* @param string $batch_id ID of the batch about which to enquire |
|
105
|
|
|
* @return array|false Assoc array of API response, decoded from JSON |
|
106
|
|
|
*/ |
|
107
|
|
|
public function check_status($batch_id=null) |
|
108
|
|
|
{ |
|
109
|
|
|
if ($batch_id===null && $this->batch_id) $batch_id = $this->batch_id; |
|
110
|
|
|
return $this->MailChimp->get('batches/'.$batch_id); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Add an operation to the internal queue. |
|
115
|
|
|
* @param string $http_verb GET, POST, PUT, PATCH or DELETE |
|
116
|
|
|
* @param string $id ID for the operation within the batch |
|
117
|
|
|
* @param string $method URL of the API request method |
|
118
|
|
|
* @param array $args Assoc array of arguments (usually your data) |
|
119
|
|
|
* @return void |
|
120
|
|
|
*/ |
|
121
|
|
|
private function queue_operation($http_verb, $id, $method, $args=null) |
|
122
|
|
|
{ |
|
123
|
|
|
$operation = array( |
|
124
|
|
|
'operation_id' => $id, |
|
125
|
|
|
'method' => $http_verb, |
|
126
|
|
|
'path' => $method, |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
if ($args) { |
|
130
|
|
|
$key = ($http_verb == 'GET' ? 'params' : 'body'); |
|
131
|
|
|
$operation[$key] = json_encode($args); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$this->operations[] = $operation; |
|
135
|
|
|
} |
|
136
|
|
|
} |