Completed
Branch cake3 (08e0f8)
by Romain
02:07
created
src/Controller/Component/GcmComponent.php 1 patch
Indentation   +287 added lines, -287 removed lines patch added patch discarded remove patch
@@ -25,291 +25,291 @@
 block discarded – undo
25 25
 class GcmComponent extends Component
26 26
 {
27 27
 
28
-    /**
29
-     * Default config
30
-     *
31
-     * @var array
32
-     */
33
-    protected $_defaultConfig = [
34
-        'api' => [
35
-            'key' => null,
36
-            'url' => 'https://gcm-http.googleapis.com/gcm/send'
37
-        ],
38
-        'parameters' => [
39
-            'collapse_key' => null,
40
-            'priority' => 'normal',
41
-            'delay_while_idle' => false,
42
-            'dry_run' => false,
43
-            'time_to_live' => 0,
44
-            'restricted_package_name' => null
45
-        ]
46
-    ];
47
-
48
-    /**
49
-     * Error code and message.
50
-     *
51
-     * @var array
52
-     */
53
-    protected $_errorMessages = [];
54
-
55
-    /**
56
-     * Response of the request
57
-     *
58
-     * @var object
59
-     */
60
-    protected $_response = null;
61
-
62
-    /**
63
-     * Constructor
64
-     *
65
-     * @param ComponentRegistry $registry A ComponentRegistry
66
-     * @param array $config Array of configuration settings
67
-     */
68
-    public function __construct(ComponentRegistry $registry, array $config = [])
69
-    {
70
-        parent::__construct($registry, $config);
71
-        $this->_errorMessages = [
72
-            '400' => __('Error 400. The request could not be parsed as JSON.'),
73
-            '401' => __('Error 401. Unable to authenticating the sender account.'),
74
-            '500' => __('Error 500. Internal Server Error.'),
75
-            '503' => __('Error 503. Service Unavailable.')
76
-        ];
77
-    }
78
-
79
-    /**
80
-     * send method
81
-     *
82
-     * @param string|array $ids
83
-     * @param array $payload
84
-     * @param array $parameters
85
-     * @return boolean
86
-     */
87
-    public function send($ids = false, array $payload = [], array $parameters = [])
88
-    {
89
-        if (is_string($ids)) {
90
-            $ids = (array)$ids;
91
-        }
92
-
93
-        if ($ids === false || !is_array($ids) || empty($ids)) {
94
-            throw new \LogicException(__('Ids must be a string or an array.'));
95
-        }
96
-
97
-        if (!is_array($payload)) {
98
-            throw new \LogicException(__('Payload must be an array.'));
99
-        }
100
-
101
-        if (!is_array($parameters)) {
102
-            throw new \LogicException(__('Parameters must be an array.'));
103
-        }
104
-
105
-        if (isset($payload['notification'])) {
106
-            $payload['notification'] = $this->_checkNotification($payload['notification']);
107
-            if (!$payload['notification']) {
108
-                throw new \LogicException(__("Unable to check notification."));
109
-            }
110
-        }
111
-
112
-        if (isset($payload['data'])) {
113
-            $payload['data'] = $this->_checkData($payload['data']);
114
-            if (!$payload['data']) {
115
-                throw new \LogicException(__("Unable to check data."));
116
-            }
117
-        }
118
-
119
-        $parameters = $this->_checkParameters($parameters);
120
-        if (!$parameters) {
121
-            throw new \ErrorException(__('Unable to check parameters.'));
122
-        }
123
-
124
-        $message = $this->_buildMessage($ids, $payload, $parameters);
125
-        if ($message === false) {
126
-            throw new \ErrorException(__('Unable to build the message.'));
127
-        }
128
-
129
-        return $this->_executePush($message);
130
-    }
131
-
132
-    /**
133
-     * sendNotification method
134
-     *
135
-     * @param string|array $ids
136
-     * @param array $notification
137
-     * @param array $parameters
138
-     * @return boolean
139
-     */
140
-    public function sendNotification($ids = false, array $notification = [], array $parameters = [])
141
-    {
142
-        return $this->send($ids, ['notification' => $notification], $parameters);
143
-    }
144
-
145
-    /**
146
-     * sendData method
147
-     *
148
-     * @param string|array $ids
149
-     * @param array $data
150
-     * @param array $parameters
151
-     * @return boolean
152
-     */
153
-    public function sendData($ids = false, array $data = [], array $parameters = [])
154
-    {
155
-        return $this->send($ids, ['data' => $data], $parameters);
156
-    }
157
-
158
-    /**
159
-     * response method
160
-     *
161
-     * @return void
162
-     */
163
-    public function response()
164
-    {
165
-        if (array_key_exists($this->_response->code, $this->_errorMessages)) {
166
-            return $this->_errorMessages[$this->_response->code];
167
-        }
168
-
169
-        return json_decode($this->_response->body, true);
170
-    }
171
-
172
-    /**
173
-     * _executePush method
174
-     *
175
-     * @param json $message
176
-     * @return boolean
177
-     */
178
-    protected function _executePush($message = false)
179
-    {
180
-        if ($message === false) {
181
-            return false;
182
-        }
183
-
184
-        if ($this->config('api.key') === null) {
185
-            throw new \ErrorException(__('No API key set. Push not triggered'));
186
-        }
187
-
188
-        $http = new Client();
189
-        $this->_response = $http->post($this->config('api.url'), $message, [
190
-            'type' => 'json',
191
-            'header' => [
192
-                'Authorization' => 'key=' . $this->config('api.key'),
193
-                'Content-Type' => 'application/json'
194
-            ]
195
-        ]);
196
-
197
-        if ($this->_response->code === '200') {
198
-            return true;
199
-        }
200
-
201
-        return false;
202
-    }
203
-
204
-    /**
205
-     * _buildMessage method
206
-     *
207
-     * @param array $ids
208
-     * @param array $payload
209
-     * @param array $parameters
210
-     * @return false|string
211
-     */
212
-    protected function _buildMessage($ids = false, $payload = false, $parameters = false)
213
-    {
214
-        if ($ids === false) {
215
-            return false;
216
-        }
217
-
218
-        $message = ['registration_ids' => $ids];
219
-
220
-        if (!empty($payload)) {
221
-            $message += $payload;
222
-        }
223
-
224
-        if (!empty($parameters)) {
225
-            $message += $parameters;
226
-        }
227
-
228
-        return json_encode($message);
229
-    }
230
-
231
-    /**
232
-     * _checkNotification method
233
-     *
234
-     * @param array $notification
235
-     * @return false|array $notification
236
-     */
237
-    protected function _checkNotification($notification = false)
238
-    {
239
-        if ($notification === false) {
240
-            return false;
241
-        }
242
-
243
-        if (!is_array($notification)) {
244
-            throw new \LogicException("Notification must be an array.");
245
-        }
246
-
247
-        if (empty($notification) || !isset($notification['title'])) {
248
-            throw new \LogicException("Notification's array must contain at least a key title.");
249
-        }
250
-
251
-        if (!isset($notification['icon'])) {
252
-            $notification['icon'] = 'myicon';
253
-        }
254
-
255
-        return $notification;
256
-    }
257
-
258
-    /**
259
-     * _checkData method
260
-     *
261
-     * @param array $data
262
-     * @return false|array $data
263
-     */
264
-    public function _checkData($data = false)
265
-    {
266
-        if ($data === false) {
267
-            return false;
268
-        }
269
-
270
-        if (!is_array($data)) {
271
-            throw new \LogicException("Data must ba an array.");
272
-        }
273
-
274
-        if (empty($data)) {
275
-            throw new \LogicException("Data's array can't be empty.");
276
-        }
277
-
278
-        // Convert all data into string
279
-        foreach ($data as $key => $value) {
280
-            $data[$key] = (string)$value;
281
-        }
282
-
283
-        return $data;
284
-    }
285
-
286
-    /**
287
-     * _checkParameters method
288
-     *
289
-     * @param array $parameters
290
-     * @return false|array $parameters
291
-     */
292
-    protected function _checkParameters($parameters = false)
293
-    {
294
-        if ($parameters === false) {
295
-            return false;
296
-        }
297
-
298
-        $parameters = Hash::merge($this->config('parameters'), $parameters);
299
-        $parameters = array_filter($parameters);
300
-
301
-        if (isset($parameters['time_to_live']) && !is_int($parameters['time_to_live'])) {
302
-            $parameters['time_to_live'] = (int)$parameters['time_to_live'];
303
-        }
304
-
305
-        if (isset($parameters['delay_while_idle']) && !is_bool($parameters['delay_while_idle'])) {
306
-            $parameters['delay_while_idle'] = (bool)$parameters['delay_while_idle'];
307
-        }
308
-
309
-        if (isset($parameters['dry_run']) && !is_bool($parameters['dry_run'])) {
310
-            $parameters['dry_run'] = (bool)$parameters['dry_run'];
311
-        }
312
-
313
-        return $parameters;
314
-    }
28
+	/**
29
+	 * Default config
30
+	 *
31
+	 * @var array
32
+	 */
33
+	protected $_defaultConfig = [
34
+		'api' => [
35
+			'key' => null,
36
+			'url' => 'https://gcm-http.googleapis.com/gcm/send'
37
+		],
38
+		'parameters' => [
39
+			'collapse_key' => null,
40
+			'priority' => 'normal',
41
+			'delay_while_idle' => false,
42
+			'dry_run' => false,
43
+			'time_to_live' => 0,
44
+			'restricted_package_name' => null
45
+		]
46
+	];
47
+
48
+	/**
49
+	 * Error code and message.
50
+	 *
51
+	 * @var array
52
+	 */
53
+	protected $_errorMessages = [];
54
+
55
+	/**
56
+	 * Response of the request
57
+	 *
58
+	 * @var object
59
+	 */
60
+	protected $_response = null;
61
+
62
+	/**
63
+	 * Constructor
64
+	 *
65
+	 * @param ComponentRegistry $registry A ComponentRegistry
66
+	 * @param array $config Array of configuration settings
67
+	 */
68
+	public function __construct(ComponentRegistry $registry, array $config = [])
69
+	{
70
+		parent::__construct($registry, $config);
71
+		$this->_errorMessages = [
72
+			'400' => __('Error 400. The request could not be parsed as JSON.'),
73
+			'401' => __('Error 401. Unable to authenticating the sender account.'),
74
+			'500' => __('Error 500. Internal Server Error.'),
75
+			'503' => __('Error 503. Service Unavailable.')
76
+		];
77
+	}
78
+
79
+	/**
80
+	 * send method
81
+	 *
82
+	 * @param string|array $ids
83
+	 * @param array $payload
84
+	 * @param array $parameters
85
+	 * @return boolean
86
+	 */
87
+	public function send($ids = false, array $payload = [], array $parameters = [])
88
+	{
89
+		if (is_string($ids)) {
90
+			$ids = (array)$ids;
91
+		}
92
+
93
+		if ($ids === false || !is_array($ids) || empty($ids)) {
94
+			throw new \LogicException(__('Ids must be a string or an array.'));
95
+		}
96
+
97
+		if (!is_array($payload)) {
98
+			throw new \LogicException(__('Payload must be an array.'));
99
+		}
100
+
101
+		if (!is_array($parameters)) {
102
+			throw new \LogicException(__('Parameters must be an array.'));
103
+		}
104
+
105
+		if (isset($payload['notification'])) {
106
+			$payload['notification'] = $this->_checkNotification($payload['notification']);
107
+			if (!$payload['notification']) {
108
+				throw new \LogicException(__("Unable to check notification."));
109
+			}
110
+		}
111
+
112
+		if (isset($payload['data'])) {
113
+			$payload['data'] = $this->_checkData($payload['data']);
114
+			if (!$payload['data']) {
115
+				throw new \LogicException(__("Unable to check data."));
116
+			}
117
+		}
118
+
119
+		$parameters = $this->_checkParameters($parameters);
120
+		if (!$parameters) {
121
+			throw new \ErrorException(__('Unable to check parameters.'));
122
+		}
123
+
124
+		$message = $this->_buildMessage($ids, $payload, $parameters);
125
+		if ($message === false) {
126
+			throw new \ErrorException(__('Unable to build the message.'));
127
+		}
128
+
129
+		return $this->_executePush($message);
130
+	}
131
+
132
+	/**
133
+	 * sendNotification method
134
+	 *
135
+	 * @param string|array $ids
136
+	 * @param array $notification
137
+	 * @param array $parameters
138
+	 * @return boolean
139
+	 */
140
+	public function sendNotification($ids = false, array $notification = [], array $parameters = [])
141
+	{
142
+		return $this->send($ids, ['notification' => $notification], $parameters);
143
+	}
144
+
145
+	/**
146
+	 * sendData method
147
+	 *
148
+	 * @param string|array $ids
149
+	 * @param array $data
150
+	 * @param array $parameters
151
+	 * @return boolean
152
+	 */
153
+	public function sendData($ids = false, array $data = [], array $parameters = [])
154
+	{
155
+		return $this->send($ids, ['data' => $data], $parameters);
156
+	}
157
+
158
+	/**
159
+	 * response method
160
+	 *
161
+	 * @return void
162
+	 */
163
+	public function response()
164
+	{
165
+		if (array_key_exists($this->_response->code, $this->_errorMessages)) {
166
+			return $this->_errorMessages[$this->_response->code];
167
+		}
168
+
169
+		return json_decode($this->_response->body, true);
170
+	}
171
+
172
+	/**
173
+	 * _executePush method
174
+	 *
175
+	 * @param json $message
176
+	 * @return boolean
177
+	 */
178
+	protected function _executePush($message = false)
179
+	{
180
+		if ($message === false) {
181
+			return false;
182
+		}
183
+
184
+		if ($this->config('api.key') === null) {
185
+			throw new \ErrorException(__('No API key set. Push not triggered'));
186
+		}
187
+
188
+		$http = new Client();
189
+		$this->_response = $http->post($this->config('api.url'), $message, [
190
+			'type' => 'json',
191
+			'header' => [
192
+				'Authorization' => 'key=' . $this->config('api.key'),
193
+				'Content-Type' => 'application/json'
194
+			]
195
+		]);
196
+
197
+		if ($this->_response->code === '200') {
198
+			return true;
199
+		}
200
+
201
+		return false;
202
+	}
203
+
204
+	/**
205
+	 * _buildMessage method
206
+	 *
207
+	 * @param array $ids
208
+	 * @param array $payload
209
+	 * @param array $parameters
210
+	 * @return false|string
211
+	 */
212
+	protected function _buildMessage($ids = false, $payload = false, $parameters = false)
213
+	{
214
+		if ($ids === false) {
215
+			return false;
216
+		}
217
+
218
+		$message = ['registration_ids' => $ids];
219
+
220
+		if (!empty($payload)) {
221
+			$message += $payload;
222
+		}
223
+
224
+		if (!empty($parameters)) {
225
+			$message += $parameters;
226
+		}
227
+
228
+		return json_encode($message);
229
+	}
230
+
231
+	/**
232
+	 * _checkNotification method
233
+	 *
234
+	 * @param array $notification
235
+	 * @return false|array $notification
236
+	 */
237
+	protected function _checkNotification($notification = false)
238
+	{
239
+		if ($notification === false) {
240
+			return false;
241
+		}
242
+
243
+		if (!is_array($notification)) {
244
+			throw new \LogicException("Notification must be an array.");
245
+		}
246
+
247
+		if (empty($notification) || !isset($notification['title'])) {
248
+			throw new \LogicException("Notification's array must contain at least a key title.");
249
+		}
250
+
251
+		if (!isset($notification['icon'])) {
252
+			$notification['icon'] = 'myicon';
253
+		}
254
+
255
+		return $notification;
256
+	}
257
+
258
+	/**
259
+	 * _checkData method
260
+	 *
261
+	 * @param array $data
262
+	 * @return false|array $data
263
+	 */
264
+	public function _checkData($data = false)
265
+	{
266
+		if ($data === false) {
267
+			return false;
268
+		}
269
+
270
+		if (!is_array($data)) {
271
+			throw new \LogicException("Data must ba an array.");
272
+		}
273
+
274
+		if (empty($data)) {
275
+			throw new \LogicException("Data's array can't be empty.");
276
+		}
277
+
278
+		// Convert all data into string
279
+		foreach ($data as $key => $value) {
280
+			$data[$key] = (string)$value;
281
+		}
282
+
283
+		return $data;
284
+	}
285
+
286
+	/**
287
+	 * _checkParameters method
288
+	 *
289
+	 * @param array $parameters
290
+	 * @return false|array $parameters
291
+	 */
292
+	protected function _checkParameters($parameters = false)
293
+	{
294
+		if ($parameters === false) {
295
+			return false;
296
+		}
297
+
298
+		$parameters = Hash::merge($this->config('parameters'), $parameters);
299
+		$parameters = array_filter($parameters);
300
+
301
+		if (isset($parameters['time_to_live']) && !is_int($parameters['time_to_live'])) {
302
+			$parameters['time_to_live'] = (int)$parameters['time_to_live'];
303
+		}
304
+
305
+		if (isset($parameters['delay_while_idle']) && !is_bool($parameters['delay_while_idle'])) {
306
+			$parameters['delay_while_idle'] = (bool)$parameters['delay_while_idle'];
307
+		}
308
+
309
+		if (isset($parameters['dry_run']) && !is_bool($parameters['dry_run'])) {
310
+			$parameters['dry_run'] = (bool)$parameters['dry_run'];
311
+		}
312
+
313
+		return $parameters;
314
+	}
315 315
 }
Please login to merge, or discard this patch.