Completed
Push — develop ( a30469...01d3b8 )
by Romain
02:12
created

GcmComponent::_executePush()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
rs 8.9713
cc 3
eloc 14
nc 3
nop 1
1
<?php
2
namespace ker0x\CakeGcm\Controller\Component;
3
4
use Cake\Controller\Component;
5
use Cake\Controller\ComponentRegistry;
6
use Cake\Network\Http\Client;
7
use Cake\Utility\Hash;
8
use \Exception;
9
10
/**
11
 * Gcm Component
12
 *
13
 */
14
class GcmComponent extends Component
15
{
16
17
    /**
18
     * Default config
19
     *
20
     * @var array
21
     */
22
    protected $_defaultConfig = [
23
        'api' => [
24
            'key' => null,
25
            'url' => 'https://gcm-http.googleapis.com/gcm/send'
26
        ],
27
        'parameters' => [
28
            'collapse_key' => null,
29
            'priority' => 'normal',
30
            'delay_while_idle' => false,
31
            'dry_run' => false,
32
            'time_to_live' => 0,
33
            'restricted_package_name' => null
34
        ],
35
        'http' => []
36
    ];
37
38
    /**
39
     * List of parameters available to use in notification messages.
40
     *
41
     * @var array
42
     */
43
    protected $_allowedNotificationParameters = [
44
        'title',
45
        'body',
46
        'icon',
47
        'sound',
48
        'badge',
49
        'tag',
50
        'color',
51
        'click_action',
52
        'body_loc_key',
53
        'body_loc_args',
54
        'title_loc_key',
55
        'title_loc_args'
56
    ];
57
58
    /**
59
     * Error code and message.
60
     *
61
     * @var array
62
     */
63
    protected $_errorMessages = [];
64
65
    /**
66
     * Response of the request
67
     *
68
     * @var object
69
     */
70
    protected $_response = null;
71
72
    /**
73
     * Constructor
74
     *
75
     * @param ComponentRegistry $registry A ComponentRegistry
76
     * @param array $config Array of configuration settings
77
     */
78
    public function __construct(ComponentRegistry $registry, array $config = [])
79
    {
80
        parent::__construct($registry, $config);
81
        $this->_errorMessages = [
82
            '400' => __('Error 400. The request could not be parsed as JSON.'),
83
            '401' => __('Error 401. Unable to authenticating the sender account.'),
84
            '500' => __('Error 500. Internal Server Error.'),
85
            '503' => __('Error 503. Service Unavailable.')
86
        ];
87
    }
88
89
    /**
90
     * Send a downstream message to one or more devices
91
     *
92
     * @param string|array $ids Devices'ids
93
     * @param array $payload The notification and/or some datas
94
     * @param array $parameters Parameters for the GCM request
95
     * @throws Exception
96
     * @return bool
97
     */
98
    public function send($ids = null, array $payload = [], array $parameters = [])
99
    {
100
        $ids = $this->_checkIds($ids);
101
102
        if (!is_array($payload)) {
103
            throw new Exception(__('Payload must be an array.'));
104
        }
105
106
        if (isset($payload['notification'])) {
107
            $payload['notification'] = $this->_checkNotification($payload['notification']);
108
        }
109
110
        if (isset($payload['data'])) {
111
            $payload['data'] = $this->_checkData($payload['data']);
112
        }
113
114
        $parameters = $this->_checkParameters($parameters);
115
116
        $message = $this->_buildMessage($ids, $payload, $parameters);
117
118
        return $this->_executePush($message);
119
    }
120
121
    /**
122
     * Shortcut to send notification
123
     *
124
     * @param string|array $ids Devices'ids
125
     * @param array $notification The notification
126
     * @param array $parameters Parameters for the GCM request
127
     * @return bool
128
     */
129
    public function sendNotification($ids = null, array $notification = [], array $parameters = [])
130
    {
131
        return $this->send($ids, ['notification' => $notification], $parameters);
132
    }
133
134
    /**
135
     * Shortcut to send datas
136
     *
137
     * @param string|array $ids Devices'ids
138
     * @param array $data Some datas
139
     * @param array $parameters Parameters for the GCM request
140
     * @return bool
141
     */
142
    public function sendData($ids = null, array $data = [], array $parameters = [])
143
    {
144
        return $this->send($ids, ['data' => $data], $parameters);
145
    }
146
147
    /**
148
     * Return the response of the push
149
     *
150
     * @return string
151
     */
152
    public function response()
153
    {
154
        if (array_key_exists($this->_response->code, $this->_errorMessages)) {
155
            return $this->_errorMessages[$this->_response->code];
156
        }
157
158
        return json_decode($this->_response->body, true);
159
    }
160
161
    /**
162
     * Send the message throught a POST request to the GCM servers
163
     *
164
     * @param string $message The message to send
165
     * @throws Exception
166
     * @return bool
167
     */
168
    protected function _executePush($message)
169
    {
170
        if ($this->config('api.key') === null) {
171
            throw new Exception(__('No API key set. Push not triggered'));
172
        }
173
174
        $options = Hash::merge($this->config('http'), [
175
            'type' => 'json',
176
            'headers' => [
177
                'Authorization' => 'key=' . $this->config('api.key'),
178
                'Content-Type' => 'application/json'
179
            ]
180
        ]);
181
        $options = array_filter($options);
182
183
        $http = new Client();
184
        $this->_response = $http->post($this->config('api.url'), $message, $options);
185
186
        if ($this->_response->code === '200') {
187
            return true;
188
        }
189
190
        return false;
191
    }
192
193
    /**
194
     * Build the message from the ids, payload and parameters
195
     *
196
     * @param array|string $ids Devices'ids
197
     * @param array $payload The notification and/or some datas
198
     * @param array $parameters Parameters for the GCM request
199
     * @return string
200
     */
201
    protected function _buildMessage($ids, $payload, $parameters)
202
    {
203
        $message = (count($ids) > 1) ? ['registration_ids' => $ids] : ['to' => current($ids)];
204
205
        if (!empty($payload)) {
206
            $message += $payload;
207
        }
208
209
        if (!empty($parameters)) {
210
            $message += $parameters;
211
        }
212
213
        return json_encode($message);
214
    }
215
216
    /**
217
     * Check if the ids are correct
218
     *
219
     * @param mixed $ids Devices'ids
220
     * @throws Exception
221
     * @return array
222
     */
223
    protected function _checkIds($ids)
224
    {
225
        if (is_string($ids)) {
226
            $ids = (array)$ids;
227
        }
228
229
        if (is_null($ids) || !is_array($ids) || empty($ids)) {
230
            throw new Exception(__('Ids must be a string or an array with at least 1 token.'));
231
        }
232
233
        if (is_array($ids) && count($ids) > 1000) {
234
            throw new Exception(__('Ids must contain at least 1 and at most 1000 registration tokens.'));
235
        }
236
237
        return $ids;
238
    }
239
240
    /**
241
     * Check if the notification array is correctly build
242
     *
243
     * @param array $notification The notification
244
     * @throws Exception
245
     * @return array $notification
246
     */
247
    protected function _checkNotification(array $notification = [])
248
    {
249
        if (!is_array($notification)) {
250
            throw new Exception('Notification must be an array.');
251
        }
252
253
        if (empty($notification) || !isset($notification['title'])) {
254
            throw new Exception('Notification\'s array must contain at least a key title.');
255
        }
256
257
        if (!isset($notification['icon'])) {
258
            $notification['icon'] = 'myicon';
259
        }
260
261
        foreach ($notification as $key => $value) {
262
            if (!in_array($key, $this->_allowedNotificationParameters)) {
263
                throw new Exception("The key {$key} is not allowed in notifications.");
264
            }
265
        }
266
267
        return $notification;
268
    }
269
270
    /**
271
     * Check if the data array is correctly build
272
     *
273
     * @param array $data Some datas
274
     * @throws Exception
275
     * @return array $data
276
     */
277
    protected function _checkData(array $data = [])
278
    {
279
        if (!is_array($data)) {
280
            throw new Exception('Data must ba an array.');
281
        }
282
283
        if (empty($data)) {
284
            throw new Exception('Data\'s array can\'t be empty.');
285
        }
286
287
        // Convert all data into string
288
        foreach ($data as $key => $value) {
289
            $data[$key] = (string)$value;
290
        }
291
292
        return $data;
293
    }
294
295
    /**
296
     * Check the parameters for the message
297
     *
298
     * @param array $parameters Parameters for the GCM request
299
     * @throws Exception
300
     * @return array $parameters
301
     */
302
    protected function _checkParameters(array $parameters = [])
303
    {
304
        if (!is_array($parameters)) {
305
            throw new Exception(__('Parameters must be an array.'));
306
        }
307
308
        $parameters = Hash::merge($this->config('parameters'), $parameters);
309
        $parameters = array_filter($parameters);
310
311
        if (isset($parameters['time_to_live']) && !is_int($parameters['time_to_live'])) {
312
            $parameters['time_to_live'] = (int)$parameters['time_to_live'];
313
        }
314
315
        if (isset($parameters['delay_while_idle']) && !is_bool($parameters['delay_while_idle'])) {
316
            $parameters['delay_while_idle'] = (bool)$parameters['delay_while_idle'];
317
        }
318
319
        if (isset($parameters['dry_run']) && !is_bool($parameters['dry_run'])) {
320
            $parameters['dry_run'] = (bool)$parameters['dry_run'];
321
        }
322
323
        return $parameters;
324
    }
325
}
326