Skip to content

Commit e549c05

Browse files
authored
Merge pull request #13 from kaushikindianic/2047-sendPushMessage
Card #2047: Userbase: update client library: sendPushMessage()
2 parents 5a7de8b + 04a8547 commit e549c05

3 files changed

Lines changed: 74 additions & 26 deletions

File tree

examples/common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require_once(__DIR__ . '/../vendor/autoload.php');
3+
require_once __DIR__.'/../vendor/autoload.php';
44

55
use UserBase\Client\Client;
66
use Symfony\Component\Dotenv\Dotenv;

examples/send_push_message.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
require_once 'common.php';
4+
5+
if (3 != count($argv)) {
6+
echo "Please pass 1 parameter: username\n";
7+
echo "Please pass 2 parameter: message\n";
8+
exit();
9+
}
10+
try {
11+
$username = $argv[1];
12+
$message = $argv[2];
13+
$data = [
14+
'word' => 'hello',
15+
'key' => 'test',
16+
];
17+
18+
$result = $client->sendPushMessage($username, $message, $data);
19+
print_r($result);
20+
} catch (Exception $e) {
21+
echo 'Exception '.$e->getMessage()."\n";
22+
}

src/Client.php

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
function curl_file_create($filename, $mimetype = '', $postname = '')
1616
{
1717
return "@$filename;filename="
18-
. ($postname ?: basename($filename))
19-
. ($mimetype ? ";type=$mimetype" : '');
18+
.($postname ?: basename($filename))
19+
.($mimetype ? ";type=$mimetype" : '');
2020
}
2121
}
2222

@@ -83,7 +83,8 @@ public function setCache(CacheItemPoolInterface $cache, $cacheDuration = 60)
8383
private function getStatusCode($ch)
8484
{
8585
$info = curl_getinfo($ch);
86-
return (int)$info['http_code'];
86+
87+
return (int) $info['http_code'];
8788
}
8889

8990
public function getBaseUrl()
@@ -109,6 +110,7 @@ public function getUsersWithDetails()
109110
$user = $this->itemToUser($item);
110111
$users[] = $user;
111112
}
113+
112114
return $users;
113115
}
114116

@@ -130,8 +132,8 @@ protected function itemToUser($data)
130132
$account = $this->itemToAccount($accountData);
131133
$accountUser->setAccount($account);
132134
$user->addAccountUser($accountUser);
133-
if ($account->getAccountType()=='user') {
134-
if ($accountData['status'] != 'ACTIVE') {
135+
if ('user' == $account->getAccountType()) {
136+
if ('ACTIVE' != $accountData['status']) {
135137
$user->setEnabled(false);
136138
}
137139
}
@@ -145,15 +147,16 @@ protected function itemToUser($data)
145147
$policy->setResource($policyData['resource']);
146148
foreach ($policyData['action'] as $action) {
147149
$policy->addAction($action);
148-
if ($policy->getEffect() == 'allow') {
149-
$roleName = 'ROLE_' . $policy->getResource();
150-
$roleName .= '@' . $action;
150+
if ('allow' == $policy->getEffect()) {
151+
$roleName = 'ROLE_'.$policy->getResource();
152+
$roleName .= '@'.$action;
151153
$user->addRole($roleName);
152154
}
153155
}
154156
$user->addPolicy($policy);
155157
}
156158
}
159+
157160
return $user;
158161
}
159162

@@ -181,7 +184,6 @@ protected function itemToAccount($data)
181184
$account->setMessage($data['message']);
182185
$account->setExpireAt($data['expire_at']);
183186

184-
185187
if (isset($data['emails'])) {
186188
foreach ($data['emails'] as $accountEmailData) {
187189
$accountEmail = new AccountEmail();
@@ -220,14 +222,14 @@ protected function itemToAccount($data)
220222

221223
public function getUserByUsername($username)
222224
{
223-
$cacheKey = 'user.' . $username . '.data';
225+
$cacheKey = 'user.'.$username.'.data';
224226
$cacheKey = str_replace('@', '%', $cacheKey);
225227

226228
$dataCache = $this->cache->getItem($cacheKey);
227229
if (!$dataCache->isHit()) {
228-
$data = $this->getData('/users/' . $username);
230+
$data = $this->getData('/users/'.$username);
229231
if (isset($data['error'])) {
230-
throw new RuntimeException('User not found: ' . $username);
232+
throw new RuntimeException('User not found: '.$username);
231233
}
232234

233235
$dataCache->set($data);
@@ -237,16 +239,18 @@ public function getUserByUsername($username)
237239
$data = $dataCache->get();
238240

239241
$user = $this->itemToUser($data);
242+
240243
return $user;
241244
}
242245

243246
public function getAccountByName($name)
244247
{
245-
$data = $this->getData('/accounts/' . $name);
248+
$data = $this->getData('/accounts/'.$name);
246249
if (!isset($data['name'])) {
247250
return false;
248251
}
249252
$account = $this->itemToAccount($data);
253+
250254
return $account;
251255
}
252256

@@ -258,6 +262,7 @@ public function getAccountsWithDetails()
258262
$user = $this->itemToAccount($item);
259263
$users[] = $user;
260264
}
265+
261266
return $users;
262267
}
263268

@@ -266,10 +271,10 @@ public function getData($uri, $jsonData = null)
266271
if ($this->timeDataCollector) {
267272
$this->timeDataCollector->startMeasure('getData', $uri);
268273
}
269-
$url = $this->baseUrl . $uri;
274+
$url = $this->baseUrl.$uri;
270275
$ch = curl_init($url);
271276
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
272-
curl_setopt($ch, CURLOPT_USERPWD, $this->username . ":" . $this->password);
277+
curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password);
273278

274279
if ($jsonData) {
275280
curl_setopt($ch, CURLOPT_POST, true);
@@ -283,8 +288,8 @@ public function getData($uri, $jsonData = null)
283288
if ($this->timeDataCollector) {
284289
$this->timeDataCollector->stopMeasure('getData');
285290
}
286-
if ($code != 200) {
287-
throw new RuntimeException("HTTP Status code: " . $code);
291+
if (200 != $code) {
292+
throw new RuntimeException('HTTP Status code: '.$code);
288293
}
289294
$data = @json_decode($json, true);
290295
curl_close($ch);
@@ -301,88 +306,109 @@ public function checkCredentials($username, $password)
301306
}
302307
$encoder = new \Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder();
303308
$valid = $encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt());
309+
304310
return $valid;
305311
}
306312

307313
public function setAccountProperty($accountName, $propertyName, $propertyValue)
308314
{
309315
$data = $this->getData('/accounts/'.$accountName.'/setProperty/'.$propertyName.'/'.$propertyValue);
316+
310317
return $data;
311318
}
312319

313320
public function setAccountPicture($accountName, $filename)
314321
{
315322
$data = $this->getData('/accounts/'.$accountName.'/setPicture');
323+
316324
return $data;
317325
}
318326

319327
public function addAccountUser($accountName, $userName, $isAdmin)
320328
{
321329
$data = $this->getData('/accounts/'.$accountName.'/addUser/'.$userName.'/'.$isAdmin);
322-
if ($data['status'] != 'ok') {
323-
throw new RuntimeException("Failed to add user to account");
330+
if ('ok' != $data['status']) {
331+
throw new RuntimeException('Failed to add user to account');
324332
}
333+
325334
return true;
326335
}
327336

328337
public function addEvent($accountName, $eventName, $data)
329338
{
330339
$data = $this->getData('/accounts/'.$accountName.'/addEvent/'.urlencode($eventName).'?'.$data);
340+
331341
return $data;
332342
}
333343

334344
public function createAccount($accountName, $accountType)
335345
{
336346
$data = $this->getData('/accounts/create/'.urlencode($accountName).'/'.urlencode($accountType));
337347
if (isset($data['error'])) {
338-
throw new RuntimeException($data['error']['code'] . ': ' . $data['error']['message']);
348+
throw new RuntimeException($data['error']['code'].': '.$data['error']['message']);
339349
}
350+
340351
return true;
341352
}
342353

343354
public function updateAccount($accountName, $properties)
344355
{
345356
$url = '/accounts/'.$accountName.'/update?x=1';
346357
foreach ($properties as $key => $value) {
347-
$url .= '&' . $key . '=' . urlencode($value);
358+
$url .= '&'.$key.'='.urlencode($value);
348359
}
349360
//echo $url; exit();
350361
$data = $this->getData($url);
362+
351363
return $data;
352364
}
353365

354366
public function createNotification($accountName, $jsonData = null)
355367
{
356368
$data = $this->getData('/accounts/'.$accountName.'/notifications/add', $jsonData);
369+
357370
return $data;
358371
}
359372

360373
public function getNotifications($accountName, $jsonData)
361374
{
362375
$data = $this->getData('/accounts/'.$accountName.'/notifications', $jsonData);
376+
363377
return $data;
364378
}
365379

366380
public function setAccountPrimaryEmail($accountName, $email)
367381
{
368-
$data = $this->getData('/accounts/'.$accountName.'/defaultEmail/' . $email);
382+
$data = $this->getData('/accounts/'.$accountName.'/defaultEmail/'.$email);
383+
369384
return $data;
370385
}
386+
371387
public function setAccountEmailVerified($accountName, $email)
372388
{
373-
$data = $this->getData('/accounts/'.$accountName.'/verifyEmail/' . $email);
389+
$data = $this->getData('/accounts/'.$accountName.'/verifyEmail/'.$email);
390+
374391
return $data;
375392
}
376393

377394
public function addAccountEmail($accountName, $email)
378395
{
379-
$data = $this->getData('/accounts/'.$accountName.'/addEmail/' . $email);
396+
$data = $this->getData('/accounts/'.$accountName.'/addEmail/'.$email);
397+
380398
return $data;
381399
}
382400

383401
public function invite($accountName, $displayName, $email, $payload = null)
384402
{
385-
$data = $this->getData('/invites/create/'. $accountName.'/' . rawurlencode($displayName) . '/' . rawurlencode($email) . '?payload=' . rawurlencode(base64_encode($payload)));
403+
$data = $this->getData('/invites/create/'.$accountName.'/'.rawurlencode($displayName).'/'.rawurlencode($email).'?payload='.rawurlencode(base64_encode($payload)));
404+
405+
return $data;
406+
}
407+
408+
public function sendPushMessage($username, $message, $data = [])
409+
{
410+
$data = $this->getData('/push?username='.$username.'&message='.rawurlencode($message), json_encode($data));
411+
386412
return $data;
387413
}
388414
}

0 commit comments

Comments
 (0)