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