@@ -10,35 +10,35 @@ |
||
| 10 | 10 | |
| 11 | 11 | class GcmComponentTest extends TestCase |
| 12 | 12 | { |
| 13 | - public $component = null; |
|
| 14 | - public $controller = null; |
|
| 13 | + public $component = null; |
|
| 14 | + public $controller = null; |
|
| 15 | 15 | |
| 16 | - public function setUp() |
|
| 17 | - { |
|
| 18 | - parent::setUp(); |
|
| 19 | - // Setup our component and fake test controller |
|
| 20 | - $request = new Request(); |
|
| 21 | - $response = new Response(); |
|
| 22 | - $this->controller = $this->getMock( |
|
| 23 | - 'Cake\Controller\Controller', |
|
| 24 | - [], |
|
| 25 | - [$request, $response] |
|
| 26 | - ); |
|
| 27 | - $registry = new ComponentRegistry($this->controller); |
|
| 28 | - $this->component = new GcmComponent($registry); |
|
| 29 | - } |
|
| 16 | + public function setUp() |
|
| 17 | + { |
|
| 18 | + parent::setUp(); |
|
| 19 | + // Setup our component and fake test controller |
|
| 20 | + $request = new Request(); |
|
| 21 | + $response = new Response(); |
|
| 22 | + $this->controller = $this->getMock( |
|
| 23 | + 'Cake\Controller\Controller', |
|
| 24 | + [], |
|
| 25 | + [$request, $response] |
|
| 26 | + ); |
|
| 27 | + $registry = new ComponentRegistry($this->controller); |
|
| 28 | + $this->component = new GcmComponent($registry); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - public function testSend() |
|
| 32 | - { |
|
| 33 | - $this->component->send(); |
|
| 34 | - $response = $this->component->response(); |
|
| 35 | - $this->assertEquals(1, $response['success']); |
|
| 36 | - } |
|
| 31 | + public function testSend() |
|
| 32 | + { |
|
| 33 | + $this->component->send(); |
|
| 34 | + $response = $this->component->response(); |
|
| 35 | + $this->assertEquals(1, $response['success']); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - public function tearDown() |
|
| 39 | - { |
|
| 40 | - parent::tearDown(); |
|
| 41 | - // Clean up after we're done |
|
| 42 | - unset($this->component, $this->controller); |
|
| 43 | - } |
|
| 38 | + public function tearDown() |
|
| 39 | + { |
|
| 40 | + parent::tearDown(); |
|
| 41 | + // Clean up after we're done |
|
| 42 | + unset($this->component, $this->controller); |
|
| 43 | + } |
|
| 44 | 44 | } |
@@ -33,72 +33,72 @@ |
||
| 33 | 33 | Configure::write('debug', true); |
| 34 | 34 | |
| 35 | 35 | Configure::write('App', [ |
| 36 | - 'namespace' => 'App', |
|
| 37 | - 'encoding' => 'UTF-8', |
|
| 38 | - 'base' => false, |
|
| 39 | - 'baseUrl' => false, |
|
| 40 | - 'dir' => 'src', |
|
| 41 | - 'webroot' => 'webroot', |
|
| 42 | - 'wwwRoot' => APP . 'webroot', |
|
| 43 | - 'fullBaseUrl' => 'http://localhost', |
|
| 44 | - 'imageBaseUrl' => 'img/', |
|
| 45 | - 'jsBaseUrl' => 'js/', |
|
| 46 | - 'cssBaseUrl' => 'css/', |
|
| 47 | - 'paths' => [ |
|
| 48 | - 'plugins' => [APP . 'Plugin' . DS], |
|
| 49 | - 'templates' => [APP . 'Template' . DS] |
|
| 50 | - ] |
|
| 36 | + 'namespace' => 'App', |
|
| 37 | + 'encoding' => 'UTF-8', |
|
| 38 | + 'base' => false, |
|
| 39 | + 'baseUrl' => false, |
|
| 40 | + 'dir' => 'src', |
|
| 41 | + 'webroot' => 'webroot', |
|
| 42 | + 'wwwRoot' => APP . 'webroot', |
|
| 43 | + 'fullBaseUrl' => 'http://localhost', |
|
| 44 | + 'imageBaseUrl' => 'img/', |
|
| 45 | + 'jsBaseUrl' => 'js/', |
|
| 46 | + 'cssBaseUrl' => 'css/', |
|
| 47 | + 'paths' => [ |
|
| 48 | + 'plugins' => [APP . 'Plugin' . DS], |
|
| 49 | + 'templates' => [APP . 'Template' . DS] |
|
| 50 | + ] |
|
| 51 | 51 | ]); |
| 52 | 52 | |
| 53 | 53 | Configure::write('Session', [ |
| 54 | - 'defaults' => 'php' |
|
| 54 | + 'defaults' => 'php' |
|
| 55 | 55 | ]); |
| 56 | 56 | |
| 57 | 57 | Cache::config([ |
| 58 | - '_cake_core_' => [ |
|
| 59 | - 'engine' => 'File', |
|
| 60 | - 'prefix' => 'cake_core_', |
|
| 61 | - 'serialize' => true |
|
| 62 | - ], |
|
| 63 | - '_cake_model_' => [ |
|
| 64 | - 'engine' => 'File', |
|
| 65 | - 'prefix' => 'cake_model_', |
|
| 66 | - 'serialize' => true |
|
| 67 | - ], |
|
| 68 | - 'default' => [ |
|
| 69 | - 'engine' => 'File', |
|
| 70 | - 'prefix' => 'default_', |
|
| 71 | - 'serialize' => true |
|
| 72 | - ] |
|
| 58 | + '_cake_core_' => [ |
|
| 59 | + 'engine' => 'File', |
|
| 60 | + 'prefix' => 'cake_core_', |
|
| 61 | + 'serialize' => true |
|
| 62 | + ], |
|
| 63 | + '_cake_model_' => [ |
|
| 64 | + 'engine' => 'File', |
|
| 65 | + 'prefix' => 'cake_model_', |
|
| 66 | + 'serialize' => true |
|
| 67 | + ], |
|
| 68 | + 'default' => [ |
|
| 69 | + 'engine' => 'File', |
|
| 70 | + 'prefix' => 'default_', |
|
| 71 | + 'serialize' => true |
|
| 72 | + ] |
|
| 73 | 73 | ]); |
| 74 | 74 | |
| 75 | 75 | // Ensure default test connection is defined |
| 76 | 76 | if (!getenv('db_class')) { |
| 77 | - putenv('db_class=Cake\Database\Driver\Sqlite'); |
|
| 78 | - putenv('db_dsn=sqlite::memory:'); |
|
| 77 | + putenv('db_class=Cake\Database\Driver\Sqlite'); |
|
| 78 | + putenv('db_dsn=sqlite::memory:'); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | ConnectionManager::config('test', [ |
| 82 | - 'className' => 'Cake\Database\Connection', |
|
| 83 | - 'driver' => getenv('db_class'), |
|
| 84 | - 'dsn' => getenv('db_dsn'), |
|
| 85 | - 'database' => getenv('db_database'), |
|
| 86 | - 'username' => getenv('db_login'), |
|
| 87 | - 'password' => getenv('db_password'), |
|
| 88 | - 'timezone' => 'UTC' |
|
| 82 | + 'className' => 'Cake\Database\Connection', |
|
| 83 | + 'driver' => getenv('db_class'), |
|
| 84 | + 'dsn' => getenv('db_dsn'), |
|
| 85 | + 'database' => getenv('db_database'), |
|
| 86 | + 'username' => getenv('db_login'), |
|
| 87 | + 'password' => getenv('db_password'), |
|
| 88 | + 'timezone' => 'UTC' |
|
| 89 | 89 | ]); |
| 90 | 90 | |
| 91 | 91 | Log::config([ |
| 92 | - 'debug' => [ |
|
| 93 | - 'engine' => 'Cake\Log\Engine\FileLog', |
|
| 94 | - 'levels' => ['notice', 'info', 'debug'], |
|
| 95 | - 'file' => 'debug', |
|
| 96 | - ], |
|
| 97 | - 'error' => [ |
|
| 98 | - 'engine' => 'Cake\Log\Engine\FileLog', |
|
| 99 | - 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'], |
|
| 100 | - 'file' => 'error', |
|
| 101 | - ] |
|
| 92 | + 'debug' => [ |
|
| 93 | + 'engine' => 'Cake\Log\Engine\FileLog', |
|
| 94 | + 'levels' => ['notice', 'info', 'debug'], |
|
| 95 | + 'file' => 'debug', |
|
| 96 | + ], |
|
| 97 | + 'error' => [ |
|
| 98 | + 'engine' => 'Cake\Log\Engine\FileLog', |
|
| 99 | + 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'], |
|
| 100 | + 'file' => 'error', |
|
| 101 | + ] |
|
| 102 | 102 | ]); |
| 103 | 103 | |
| 104 | 104 | Plugin::load('CakeGcm', ['path' => ROOT]); |
@@ -25,302 +25,302 @@ |
||
| 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 | - * List of parameters available to use in notification messages. |
|
| 50 | - * |
|
| 51 | - * @var array |
|
| 52 | - */ |
|
| 53 | - protected $_allowedNotificationParameters = [ |
|
| 54 | - 'title', |
|
| 55 | - 'body', |
|
| 56 | - 'icon', |
|
| 57 | - 'sound', |
|
| 58 | - 'badge', |
|
| 59 | - 'tag', |
|
| 60 | - 'color', |
|
| 61 | - 'click_action', |
|
| 62 | - 'body_loc_key', |
|
| 63 | - 'body_loc_args', |
|
| 64 | - 'title_loc_key', |
|
| 65 | - 'title_loc_args' |
|
| 66 | - ]; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Error code and message. |
|
| 70 | - * |
|
| 71 | - * @var array |
|
| 72 | - */ |
|
| 73 | - protected $_errorMessages = []; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Response of the request |
|
| 77 | - * |
|
| 78 | - * @var object |
|
| 79 | - */ |
|
| 80 | - protected $_response = null; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Constructor |
|
| 84 | - * |
|
| 85 | - * @param ComponentRegistry $registry A ComponentRegistry |
|
| 86 | - * @param array $config Array of configuration settings |
|
| 87 | - */ |
|
| 88 | - public function __construct(ComponentRegistry $registry, array $config = []) |
|
| 89 | - { |
|
| 90 | - parent::__construct($registry, $config); |
|
| 91 | - $this->_errorMessages = [ |
|
| 92 | - '400' => __('Error 400. The request could not be parsed as JSON.'), |
|
| 93 | - '401' => __('Error 401. Unable to authenticating the sender account.'), |
|
| 94 | - '500' => __('Error 500. Internal Server Error.'), |
|
| 95 | - '503' => __('Error 503. Service Unavailable.') |
|
| 96 | - ]; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * send method |
|
| 101 | - * |
|
| 102 | - * @param string|array $ids |
|
| 103 | - * @param array $payload |
|
| 104 | - * @param array $parameters |
|
| 105 | - * @throws Exception |
|
| 106 | - * @return boolean |
|
| 107 | - */ |
|
| 108 | - public function send($ids = null, array $payload = [], array $parameters = []) |
|
| 109 | - { |
|
| 110 | - if (!is_string($ids) || !is_array($ids) || empty($ids)) { |
|
| 111 | - throw new Exception(__('Ids must be a string or an array with at least 1 token.')); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - if (is_string($ids)) { |
|
| 115 | - $ids = (array)$ids; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - if (is_array($ids) && count($ids) > 1000) { |
|
| 119 | - throw new Exception(__('Ids must contain at least 1 and at most 1000 registration tokens.')); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - if (!is_array($payload)) { |
|
| 123 | - throw new Exception(__('Payload must be an array.')); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - if (!is_array($parameters)) { |
|
| 127 | - throw new Exception(__('Parameters must be an array.')); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - if (isset($payload['notification'])) { |
|
| 131 | - $payload['notification'] = $this->_checkNotification($payload['notification']); |
|
| 132 | - if (!$payload['notification']) { |
|
| 133 | - throw new Exception(__("Unable to check notification.")); |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - if (isset($payload['data'])) { |
|
| 138 | - $payload['data'] = $this->_checkData($payload['data']); |
|
| 139 | - if (!$payload['data']) { |
|
| 140 | - throw new Exception(__("Unable to check data.")); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $parameters = $this->_checkParameters($parameters); |
|
| 145 | - if (!$parameters) { |
|
| 146 | - throw new Exception(__('Unable to check parameters.')); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - $message = $this->_buildMessage($ids, $payload, $parameters); |
|
| 150 | - |
|
| 151 | - return $this->_executePush($message); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * sendNotification method |
|
| 156 | - * |
|
| 157 | - * @param string|array $ids |
|
| 158 | - * @param array $notification |
|
| 159 | - * @param array $parameters |
|
| 160 | - * @return boolean |
|
| 161 | - */ |
|
| 162 | - public function sendNotification($ids = null, array $notification = [], array $parameters = []) |
|
| 163 | - { |
|
| 164 | - return $this->send($ids, ['notification' => $notification], $parameters); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * sendData method |
|
| 169 | - * |
|
| 170 | - * @param string|array $ids |
|
| 171 | - * @param array $data |
|
| 172 | - * @param array $parameters |
|
| 173 | - * @return boolean |
|
| 174 | - */ |
|
| 175 | - public function sendData($ids = null, array $data = [], array $parameters = []) |
|
| 176 | - { |
|
| 177 | - return $this->send($ids, ['data' => $data], $parameters); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * response method |
|
| 182 | - * |
|
| 183 | - * @return string |
|
| 184 | - */ |
|
| 185 | - public function response() |
|
| 186 | - { |
|
| 187 | - if (array_key_exists($this->_response->code, $this->_errorMessages)) { |
|
| 188 | - return $this->_errorMessages[$this->_response->code]; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - return json_decode($this->_response->body, true); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * _executePush method |
|
| 196 | - * |
|
| 197 | - * @param string $message |
|
| 198 | - * @throws Exception |
|
| 199 | - * @return boolean |
|
| 200 | - */ |
|
| 201 | - protected function _executePush($message) |
|
| 202 | - { |
|
| 203 | - if ($this->config('api.key') === null) { |
|
| 204 | - throw new Exception(__('No API key set. Push not triggered')); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - $http = new Client(); |
|
| 208 | - $this->_response = $http->post($this->config('api.url'), $message, [ |
|
| 209 | - 'type' => 'json', |
|
| 210 | - 'headers' => [ |
|
| 211 | - 'Authorization' => 'key=' . $this->config('api.key'), |
|
| 212 | - 'Content-Type' => 'application/json' |
|
| 213 | - ] |
|
| 214 | - ]); |
|
| 215 | - |
|
| 216 | - if ($this->_response->code === '200') { |
|
| 217 | - return true; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - return false; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * _buildMessage method |
|
| 225 | - * |
|
| 226 | - * @param array|string $ids |
|
| 227 | - * @param array $payload |
|
| 228 | - * @param array $parameters |
|
| 229 | - * @return string |
|
| 230 | - */ |
|
| 231 | - protected function _buildMessage($ids, $payload, $parameters) |
|
| 232 | - { |
|
| 233 | - $message = (count($ids) > 1) ? ['registration_ids' => $ids] : ['to' => current($ids)]; |
|
| 234 | - |
|
| 235 | - if (!empty($payload)) { |
|
| 236 | - $message += $payload; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - if (!empty($parameters)) { |
|
| 240 | - $message += $parameters; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - return json_encode($message); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * _checkNotification method |
|
| 248 | - * |
|
| 249 | - * @param array $notification |
|
| 250 | - * @throws Exception |
|
| 251 | - * @return array $notification |
|
| 252 | - */ |
|
| 253 | - protected function _checkNotification(array $notification = []) |
|
| 254 | - { |
|
| 255 | - if (!is_array($notification)) { |
|
| 256 | - throw new Exception('Notification must be an array.'); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - if (empty($notification) || !isset($notification['title'])) { |
|
| 260 | - throw new Exception('Notification\'s array must contain at least a key title.'); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - if (!isset($notification['icon'])) { |
|
| 264 | - $notification['icon'] = 'myicon'; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - foreach ($notification as $key => $value) { |
|
| 268 | - if (!in_array($key, $this->_allowedNotificationParameters)) { |
|
| 269 | - throw new Exception("The key {$key} is not allowed in notifications."); |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - return $notification; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * _checkData method |
|
| 278 | - * |
|
| 279 | - * @param array $data |
|
| 280 | - * @throws Exception |
|
| 281 | - * @return array $data |
|
| 282 | - */ |
|
| 283 | - public function _checkData(array $data = []) |
|
| 284 | - { |
|
| 285 | - if (!is_array($data)) { |
|
| 286 | - throw new Exception('Data must ba an array.'); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - if (empty($data)) { |
|
| 290 | - throw new Exception('Data\'s array can\'t be empty.'); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - // Convert all data into string |
|
| 294 | - foreach ($data as $key => $value) { |
|
| 295 | - $data[$key] = (string)$value; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - return $data; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * _checkParameters method |
|
| 303 | - * |
|
| 304 | - * @param array $parameters |
|
| 305 | - * @return array $parameters |
|
| 306 | - */ |
|
| 307 | - protected function _checkParameters(array $parameters = []) |
|
| 308 | - { |
|
| 309 | - $parameters = Hash::merge($this->config('parameters'), $parameters); |
|
| 310 | - $parameters = array_filter($parameters); |
|
| 311 | - |
|
| 312 | - if (isset($parameters['time_to_live']) && !is_int($parameters['time_to_live'])) { |
|
| 313 | - $parameters['time_to_live'] = (int)$parameters['time_to_live']; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - if (isset($parameters['delay_while_idle']) && !is_bool($parameters['delay_while_idle'])) { |
|
| 317 | - $parameters['delay_while_idle'] = (bool)$parameters['delay_while_idle']; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - if (isset($parameters['dry_run']) && !is_bool($parameters['dry_run'])) { |
|
| 321 | - $parameters['dry_run'] = (bool)$parameters['dry_run']; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - return $parameters; |
|
| 325 | - } |
|
| 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 | + * List of parameters available to use in notification messages. |
|
| 50 | + * |
|
| 51 | + * @var array |
|
| 52 | + */ |
|
| 53 | + protected $_allowedNotificationParameters = [ |
|
| 54 | + 'title', |
|
| 55 | + 'body', |
|
| 56 | + 'icon', |
|
| 57 | + 'sound', |
|
| 58 | + 'badge', |
|
| 59 | + 'tag', |
|
| 60 | + 'color', |
|
| 61 | + 'click_action', |
|
| 62 | + 'body_loc_key', |
|
| 63 | + 'body_loc_args', |
|
| 64 | + 'title_loc_key', |
|
| 65 | + 'title_loc_args' |
|
| 66 | + ]; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Error code and message. |
|
| 70 | + * |
|
| 71 | + * @var array |
|
| 72 | + */ |
|
| 73 | + protected $_errorMessages = []; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Response of the request |
|
| 77 | + * |
|
| 78 | + * @var object |
|
| 79 | + */ |
|
| 80 | + protected $_response = null; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Constructor |
|
| 84 | + * |
|
| 85 | + * @param ComponentRegistry $registry A ComponentRegistry |
|
| 86 | + * @param array $config Array of configuration settings |
|
| 87 | + */ |
|
| 88 | + public function __construct(ComponentRegistry $registry, array $config = []) |
|
| 89 | + { |
|
| 90 | + parent::__construct($registry, $config); |
|
| 91 | + $this->_errorMessages = [ |
|
| 92 | + '400' => __('Error 400. The request could not be parsed as JSON.'), |
|
| 93 | + '401' => __('Error 401. Unable to authenticating the sender account.'), |
|
| 94 | + '500' => __('Error 500. Internal Server Error.'), |
|
| 95 | + '503' => __('Error 503. Service Unavailable.') |
|
| 96 | + ]; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * send method |
|
| 101 | + * |
|
| 102 | + * @param string|array $ids |
|
| 103 | + * @param array $payload |
|
| 104 | + * @param array $parameters |
|
| 105 | + * @throws Exception |
|
| 106 | + * @return boolean |
|
| 107 | + */ |
|
| 108 | + public function send($ids = null, array $payload = [], array $parameters = []) |
|
| 109 | + { |
|
| 110 | + if (!is_string($ids) || !is_array($ids) || empty($ids)) { |
|
| 111 | + throw new Exception(__('Ids must be a string or an array with at least 1 token.')); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + if (is_string($ids)) { |
|
| 115 | + $ids = (array)$ids; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + if (is_array($ids) && count($ids) > 1000) { |
|
| 119 | + throw new Exception(__('Ids must contain at least 1 and at most 1000 registration tokens.')); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + if (!is_array($payload)) { |
|
| 123 | + throw new Exception(__('Payload must be an array.')); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + if (!is_array($parameters)) { |
|
| 127 | + throw new Exception(__('Parameters must be an array.')); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + if (isset($payload['notification'])) { |
|
| 131 | + $payload['notification'] = $this->_checkNotification($payload['notification']); |
|
| 132 | + if (!$payload['notification']) { |
|
| 133 | + throw new Exception(__("Unable to check notification.")); |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + if (isset($payload['data'])) { |
|
| 138 | + $payload['data'] = $this->_checkData($payload['data']); |
|
| 139 | + if (!$payload['data']) { |
|
| 140 | + throw new Exception(__("Unable to check data.")); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $parameters = $this->_checkParameters($parameters); |
|
| 145 | + if (!$parameters) { |
|
| 146 | + throw new Exception(__('Unable to check parameters.')); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + $message = $this->_buildMessage($ids, $payload, $parameters); |
|
| 150 | + |
|
| 151 | + return $this->_executePush($message); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * sendNotification method |
|
| 156 | + * |
|
| 157 | + * @param string|array $ids |
|
| 158 | + * @param array $notification |
|
| 159 | + * @param array $parameters |
|
| 160 | + * @return boolean |
|
| 161 | + */ |
|
| 162 | + public function sendNotification($ids = null, array $notification = [], array $parameters = []) |
|
| 163 | + { |
|
| 164 | + return $this->send($ids, ['notification' => $notification], $parameters); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * sendData method |
|
| 169 | + * |
|
| 170 | + * @param string|array $ids |
|
| 171 | + * @param array $data |
|
| 172 | + * @param array $parameters |
|
| 173 | + * @return boolean |
|
| 174 | + */ |
|
| 175 | + public function sendData($ids = null, array $data = [], array $parameters = []) |
|
| 176 | + { |
|
| 177 | + return $this->send($ids, ['data' => $data], $parameters); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * response method |
|
| 182 | + * |
|
| 183 | + * @return string |
|
| 184 | + */ |
|
| 185 | + public function response() |
|
| 186 | + { |
|
| 187 | + if (array_key_exists($this->_response->code, $this->_errorMessages)) { |
|
| 188 | + return $this->_errorMessages[$this->_response->code]; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + return json_decode($this->_response->body, true); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * _executePush method |
|
| 196 | + * |
|
| 197 | + * @param string $message |
|
| 198 | + * @throws Exception |
|
| 199 | + * @return boolean |
|
| 200 | + */ |
|
| 201 | + protected function _executePush($message) |
|
| 202 | + { |
|
| 203 | + if ($this->config('api.key') === null) { |
|
| 204 | + throw new Exception(__('No API key set. Push not triggered')); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + $http = new Client(); |
|
| 208 | + $this->_response = $http->post($this->config('api.url'), $message, [ |
|
| 209 | + 'type' => 'json', |
|
| 210 | + 'headers' => [ |
|
| 211 | + 'Authorization' => 'key=' . $this->config('api.key'), |
|
| 212 | + 'Content-Type' => 'application/json' |
|
| 213 | + ] |
|
| 214 | + ]); |
|
| 215 | + |
|
| 216 | + if ($this->_response->code === '200') { |
|
| 217 | + return true; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + return false; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * _buildMessage method |
|
| 225 | + * |
|
| 226 | + * @param array|string $ids |
|
| 227 | + * @param array $payload |
|
| 228 | + * @param array $parameters |
|
| 229 | + * @return string |
|
| 230 | + */ |
|
| 231 | + protected function _buildMessage($ids, $payload, $parameters) |
|
| 232 | + { |
|
| 233 | + $message = (count($ids) > 1) ? ['registration_ids' => $ids] : ['to' => current($ids)]; |
|
| 234 | + |
|
| 235 | + if (!empty($payload)) { |
|
| 236 | + $message += $payload; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + if (!empty($parameters)) { |
|
| 240 | + $message += $parameters; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + return json_encode($message); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * _checkNotification method |
|
| 248 | + * |
|
| 249 | + * @param array $notification |
|
| 250 | + * @throws Exception |
|
| 251 | + * @return array $notification |
|
| 252 | + */ |
|
| 253 | + protected function _checkNotification(array $notification = []) |
|
| 254 | + { |
|
| 255 | + if (!is_array($notification)) { |
|
| 256 | + throw new Exception('Notification must be an array.'); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + if (empty($notification) || !isset($notification['title'])) { |
|
| 260 | + throw new Exception('Notification\'s array must contain at least a key title.'); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + if (!isset($notification['icon'])) { |
|
| 264 | + $notification['icon'] = 'myicon'; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + foreach ($notification as $key => $value) { |
|
| 268 | + if (!in_array($key, $this->_allowedNotificationParameters)) { |
|
| 269 | + throw new Exception("The key {$key} is not allowed in notifications."); |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + return $notification; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * _checkData method |
|
| 278 | + * |
|
| 279 | + * @param array $data |
|
| 280 | + * @throws Exception |
|
| 281 | + * @return array $data |
|
| 282 | + */ |
|
| 283 | + public function _checkData(array $data = []) |
|
| 284 | + { |
|
| 285 | + if (!is_array($data)) { |
|
| 286 | + throw new Exception('Data must ba an array.'); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + if (empty($data)) { |
|
| 290 | + throw new Exception('Data\'s array can\'t be empty.'); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + // Convert all data into string |
|
| 294 | + foreach ($data as $key => $value) { |
|
| 295 | + $data[$key] = (string)$value; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + return $data; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * _checkParameters method |
|
| 303 | + * |
|
| 304 | + * @param array $parameters |
|
| 305 | + * @return array $parameters |
|
| 306 | + */ |
|
| 307 | + protected function _checkParameters(array $parameters = []) |
|
| 308 | + { |
|
| 309 | + $parameters = Hash::merge($this->config('parameters'), $parameters); |
|
| 310 | + $parameters = array_filter($parameters); |
|
| 311 | + |
|
| 312 | + if (isset($parameters['time_to_live']) && !is_int($parameters['time_to_live'])) { |
|
| 313 | + $parameters['time_to_live'] = (int)$parameters['time_to_live']; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + if (isset($parameters['delay_while_idle']) && !is_bool($parameters['delay_while_idle'])) { |
|
| 317 | + $parameters['delay_while_idle'] = (bool)$parameters['delay_while_idle']; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + if (isset($parameters['dry_run']) && !is_bool($parameters['dry_run'])) { |
|
| 321 | + $parameters['dry_run'] = (bool)$parameters['dry_run']; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + return $parameters; |
|
| 325 | + } |
|
| 326 | 326 | } |