Completed
Push — master ( 09df9b...072647 )
by Drew
01:47
created

Batch::queueOperation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.4285
cc 3
eloc 9
nc 3
nop 4
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->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
     * Add an operation to the internal queue.
118
     * @param   string $http_verb GET, POST, PUT, PATCH or DELETE
119
     * @param   string $id ID for the operation within the batch
120
     * @param   string $method URL of the API request method
121
     * @param   array $args Assoc array of arguments (usually your data)
122
     * @return  void
123
     */
124
    private function queueOperation($http_verb, $id, $method, $args = null)
125
    {
126
        $operation = array(
127
            'operation_id' => $id,
128
            'method' => $http_verb,
129
            'path' => $method,
130
        );
131
132
        if ($args) {
133
            $key = ($http_verb == 'GET' ? 'params' : 'body');
134
            $operation[$key] = json_encode($args);
135
        }
136
137
        $this->operations[] = $operation;
138
    }
139
}
140