PluginProbe
FV Player 8 / trunk
FV Player 8 vtrunk
trunk 8.0.18 8.0.19 8.0.20 8.0.21 8.0.25 8.0.27 8.1 8.1.3
fv-player / includes / mailchimp-api / src / Batch.php

Batch.php in FV Player 8 trunk, at includes/mailchimp-api/src/Batch.php

149 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 <drew.mclellan@gmail.com>
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->queueOperation('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->queueOperation('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->queueOperation('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->queueOperation('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->queueOperation('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) {
110 $batch_id = $this->batch_id;
111 }
112
113 return $this->MailChimp->get('batches/' . $batch_id);
114 }
115
116 /**
117 * Get operations
118 * @return array
119 */
120 public function get_operations()
121 {
122 return $this->operations;
123 }
124
125 /**
126 * Add an operation to the internal queue.
127 * @param string $http_verb GET, POST, PUT, PATCH or DELETE
128 * @param string $id ID for the operation within the batch
129 * @param string $method URL of the API request method
130 * @param array $args Assoc array of arguments (usually your data)
131 * @return void
132 */
133 private function queueOperation($http_verb, $id, $method, $args = null)
134 {
135 $operation = array(
136 'operation_id' => $id,
137 'method' => $http_verb,
138 'path' => $method,
139 );
140
141 if ($args) {
142 $key = ($http_verb == 'GET' ? 'params' : 'body');
143 $operation[$key] = wp_json_encode($args);
144 }
145
146 $this->operations[] = $operation;
147 }
148 }
149