Skip to content

Commit 37127e8

Browse files
committed
Merge branch 'master' of github.com:linkorb/userbase-client-php
2 parents 9dac2b3 + e549c05 commit 37127e8

3 files changed

Lines changed: 76 additions & 27 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: 53 additions & 26 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

@@ -119,7 +121,8 @@ protected function itemToUser($data)
119121
$user->setPassword($data['password']);
120122
$user->setCreatedAt($data['created_at']);
121123
$user->setDeletedAt($data['deleted_at']);
122-
$user->setAccountNonLocked((int) $data['accountNonLocked']);
124+
// $user->setAccountNonLocked((int) $data['accountNonLocked']);
125+
$user->setAccountNonLocked(isset($data['accountNonLocked']) ? (int) $data['accountNonLocked'] : true);
123126

124127
if (isset($data['accounts'])) {
125128
foreach ($data['accounts'] as $accountData) {
@@ -129,8 +132,8 @@ protected function itemToUser($data)
129132
$account = $this->itemToAccount($accountData);
130133
$accountUser->setAccount($account);
131134
$user->addAccountUser($accountUser);
132-
if ($account->getAccountType()=='user') {
133-
if ($accountData['status'] != 'ACTIVE') {
135+
if ('user' == $account->getAccountType()) {
136+
if ('ACTIVE' != $accountData['status']) {
134137
$user->setEnabled(false);
135138
}
136139
}
@@ -144,15 +147,16 @@ protected function itemToUser($data)
144147
$policy->setResource($policyData['resource']);
145148
foreach ($policyData['action'] as $action) {
146149
$policy->addAction($action);
147-
if ($policy->getEffect() == 'allow') {
148-
$roleName = 'ROLE_' . $policy->getResource();
149-
$roleName .= '@' . $action;
150+
if ('allow' == $policy->getEffect()) {
151+
$roleName = 'ROLE_'.$policy->getResource();
152+
$roleName .= '@'.$action;
150153
$user->addRole($roleName);
151154
}
152155
}
153156
$user->addPolicy($policy);
154157
}
155158
}
159+
156160
return $user;
157161
}
158162

@@ -180,7 +184,6 @@ protected function itemToAccount($data)
180184
$account->setMessage($data['message']);
181185
$account->setExpireAt($data['expire_at']);
182186

183-
184187
if (isset($data['emails'])) {
185188
foreach ($data['emails'] as $accountEmailData) {
186189
$accountEmail = new AccountEmail();
@@ -219,14 +222,14 @@ protected function itemToAccount($data)
219222

220223
public function getUserByUsername($username)
221224
{
222-
$cacheKey = 'user.' . $username . '.data';
225+
$cacheKey = 'user.'.$username.'.data';
223226
$cacheKey = str_replace('@', '%', $cacheKey);
224227

225228
$dataCache = $this->cache->getItem($cacheKey);
226229
if (!$dataCache->isHit()) {
227-
$data = $this->getData('/users/' . $username);
230+
$data = $this->getData('/users/'.$username);
228231
if (isset($data['error'])) {
229-
throw new RuntimeException('User not found: ' . $username);
232+
throw new RuntimeException('User not found: '.$username);
230233
}
231234

232235
$dataCache->set($data);
@@ -236,16 +239,18 @@ public function getUserByUsername($username)
236239
$data = $dataCache->get();
237240

238241
$user = $this->itemToUser($data);
242+
239243
return $user;
240244
}
241245

242246
public function getAccountByName($name)
243247
{
244-
$data = $this->getData('/accounts/' . $name);
248+
$data = $this->getData('/accounts/'.$name);
245249
if (!isset($data['name'])) {
246250
return false;
247251
}
248252
$account = $this->itemToAccount($data);
253+
249254
return $account;
250255
}
251256

@@ -257,6 +262,7 @@ public function getAccountsWithDetails()
257262
$user = $this->itemToAccount($item);
258263
$users[] = $user;
259264
}
265+
260266
return $users;
261267
}
262268

@@ -265,10 +271,10 @@ public function getData($uri, $jsonData = null)
265271
if ($this->timeDataCollector) {
266272
$this->timeDataCollector->startMeasure('getData', $uri);
267273
}
268-
$url = $this->baseUrl . $uri;
274+
$url = $this->baseUrl.$uri;
269275
$ch = curl_init($url);
270276
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
271-
curl_setopt($ch, CURLOPT_USERPWD, $this->username . ":" . $this->password);
277+
curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password);
272278

273279
if ($jsonData) {
274280
curl_setopt($ch, CURLOPT_POST, true);
@@ -282,8 +288,8 @@ public function getData($uri, $jsonData = null)
282288
if ($this->timeDataCollector) {
283289
$this->timeDataCollector->stopMeasure('getData');
284290
}
285-
if ($code != 200) {
286-
throw new RuntimeException("HTTP Status code: " . $code);
291+
if (200 != $code) {
292+
throw new RuntimeException('HTTP Status code: '.$code);
287293
}
288294
$data = @json_decode($json, true);
289295
curl_close($ch);
@@ -300,88 +306,109 @@ public function checkCredentials($username, $password)
300306
}
301307
$encoder = new \Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder();
302308
$valid = $encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt());
309+
303310
return $valid;
304311
}
305312

306313
public function setAccountProperty($accountName, $propertyName, $propertyValue)
307314
{
308315
$data = $this->getData('/accounts/'.$accountName.'/setProperty/'.$propertyName.'/'.$propertyValue);
316+
309317
return $data;
310318
}
311319

312320
public function setAccountPicture($accountName, $filename)
313321
{
314322
$data = $this->getData('/accounts/'.$accountName.'/setPicture');
323+
315324
return $data;
316325
}
317326

318327
public function addAccountUser($accountName, $userName, $isAdmin)
319328
{
320329
$data = $this->getData('/accounts/'.$accountName.'/addUser/'.$userName.'/'.$isAdmin);
321-
if ($data['status'] != 'ok') {
322-
throw new RuntimeException("Failed to add user to account");
330+
if ('ok' != $data['status']) {
331+
throw new RuntimeException('Failed to add user to account');
323332
}
333+
324334
return true;
325335
}
326336

327337
public function addEvent($accountName, $eventName, $data)
328338
{
329339
$data = $this->getData('/accounts/'.$accountName.'/addEvent/'.urlencode($eventName).'?'.$data);
340+
330341
return $data;
331342
}
332343

333344
public function createAccount($accountName, $accountType)
334345
{
335346
$data = $this->getData('/accounts/create/'.urlencode($accountName).'/'.urlencode($accountType));
336347
if (isset($data['error'])) {
337-
throw new RuntimeException($data['error']['code'] . ': ' . $data['error']['message']);
348+
throw new RuntimeException($data['error']['code'].': '.$data['error']['message']);
338349
}
350+
339351
return true;
340352
}
341353

342354
public function updateAccount($accountName, $properties)
343355
{
344356
$url = '/accounts/'.$accountName.'/update?x=1';
345357
foreach ($properties as $key => $value) {
346-
$url .= '&' . $key . '=' . urlencode($value);
358+
$url .= '&'.$key.'='.urlencode($value);
347359
}
348360
//echo $url; exit();
349361
$data = $this->getData($url);
362+
350363
return $data;
351364
}
352365

353366
public function createNotification($accountName, $jsonData = null)
354367
{
355368
$data = $this->getData('/accounts/'.$accountName.'/notifications/add', $jsonData);
369+
356370
return $data;
357371
}
358372

359373
public function getNotifications($accountName, $jsonData)
360374
{
361375
$data = $this->getData('/accounts/'.$accountName.'/notifications', $jsonData);
376+
362377
return $data;
363378
}
364379

365380
public function setAccountPrimaryEmail($accountName, $email)
366381
{
367-
$data = $this->getData('/accounts/'.$accountName.'/defaultEmail/' . $email);
382+
$data = $this->getData('/accounts/'.$accountName.'/defaultEmail/'.$email);
383+
368384
return $data;
369385
}
386+
370387
public function setAccountEmailVerified($accountName, $email)
371388
{
372-
$data = $this->getData('/accounts/'.$accountName.'/verifyEmail/' . $email);
389+
$data = $this->getData('/accounts/'.$accountName.'/verifyEmail/'.$email);
390+
373391
return $data;
374392
}
375393

376394
public function addAccountEmail($accountName, $email)
377395
{
378-
$data = $this->getData('/accounts/'.$accountName.'/addEmail/' . $email);
396+
$data = $this->getData('/accounts/'.$accountName.'/addEmail/'.$email);
397+
379398
return $data;
380399
}
381400

382401
public function invite($accountName, $displayName, $email, $payload = null)
383402
{
384-
$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+
385412
return $data;
386413
}
387414
}

0 commit comments

Comments
 (0)