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