Skip to content

Commit

Permalink
feat: add new classes for PassageUser, CreateUserArgs, and UpdateUser…
Browse files Browse the repository at this point in the history
…Args (#90)
  • Loading branch information
ctran88 authored Dec 11, 2024
1 parent e090c3f commit 7afac85
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 11 deletions.
9 changes: 9 additions & 0 deletions custom/lib/CreateUserArgs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Passage\Client;

use OpenAPI\Client\Model\CreateUserRequest;

class CreateUserArgs extends CreateUserRequest
{
}
17 changes: 15 additions & 2 deletions custom/lib/Passage.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ public function createUser(
trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);

try {
return $this->user->create($create_user_request);
$args = $this->cast($create_user_request, CreateUserArgs::class);
return $this->user->create($args);
} catch (PassageError $e) {
throw $e->getPrevious();
}
Expand Down Expand Up @@ -299,9 +300,21 @@ public function updateUser(
trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);

try {
return $this->user->update($user_id, $update_user_request);
$args = $this->cast($update_user_request, UpdateUserArgs::class);
return $this->user->update($user_id, $args);
} catch (PassageError $e) {
throw $e->getPrevious();
}
}

private function cast(object $object, string $class): mixed
{
return unserialize(
preg_replace(
'/^O:\d+:"[^"]++"/',
'O:' . strlen($class) . ':"' . $class . '"',
serialize($object)
)
);
}
}
9 changes: 9 additions & 0 deletions custom/lib/PassageUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Passage\Client;

use OpenAPI\Client\Model\UserInfo;

class PassageUser extends UserInfo
{
}
9 changes: 9 additions & 0 deletions custom/lib/UpdateUserArgs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Passage\Client;

use OpenAPI\Client\Model\UpdateUserRequest;

class UpdateUserArgs extends UpdateUserRequest
{
}
33 changes: 24 additions & 9 deletions custom/lib/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
use OpenAPI\Client\Configuration;
use OpenAPI\Client\Api\UsersApi;
use OpenAPI\Client\Api\UserDevicesApi;
use OpenAPI\Client\Model\UserInfo as PassageUser;
use OpenAPI\Client\Model\UpdateUserRequest as UpdateUserArgs;
use OpenAPI\Client\Model\CreateUserRequest as CreateUserArgs;
use OpenAPI\Client\Model\UserInfo;
use OpenAPI\Client\Model\WebAuthnDevices;
use OpenAPI\Client\ApiException;

Expand Down Expand Up @@ -45,7 +43,8 @@ public function get(string $userId): PassageUser
}

try {
return $this->usersApi->getUser($this->appId, $userId)->getUser();
$user = $this->usersApi->getUser($this->appId, $userId)->getUser();
return $this->castUserInfoToPassageUser($user);
} catch (ApiException $e) {
throw PassageError::fromApiException($e);
}
Expand Down Expand Up @@ -84,7 +83,8 @@ public function getByIdentifier(string $identifier): PassageUser
}

$userId = $users[0]->getId();
return $this->usersApi->getUser($this->appId, $userId)->getUser();
$user = $this->usersApi->getUser($this->appId, $userId)->getUser();
return $this->castUserInfoToPassageUser($user);
} catch (ApiException $e) {
throw PassageError::fromApiException($e);
}
Expand All @@ -105,7 +105,8 @@ public function activate(string $userId): PassageUser
}

try {
return $this->usersApi->activateUser($this->appId, $userId)->getUser();
$user = $this->usersApi->activateUser($this->appId, $userId)->getUser();
return $this->castUserInfoToPassageUser($user);
} catch (ApiException $e) {
throw PassageError::fromApiException($e);
}
Expand All @@ -126,7 +127,8 @@ public function deactivate(string $userId): PassageUser
}

try {
return $this->usersApi->deactivateUser($this->appId, $userId)->getUser();
$user = $this->usersApi->deactivateUser($this->appId, $userId)->getUser();
return $this->castUserInfoToPassageUser($user);
} catch (ApiException $e) {
throw PassageError::fromApiException($e);
}
Expand All @@ -148,7 +150,8 @@ public function update(string $userId, UpdateUserArgs $options): PassageUser
}

try {
return $this->usersApi->updateUser($this->appId, $userId, $options)->getUser();
$user = $this->usersApi->updateUser($this->appId, $userId, $options)->getUser();
return $this->castUserInfoToPassageUser($user);
} catch (ApiException $e) {
throw PassageError::fromApiException($e);
}
Expand All @@ -169,7 +172,8 @@ public function create(CreateUserArgs $args): PassageUser
}

try {
return $this->usersApi->createUser($this->appId, $args)->getUser();
$user = $this->usersApi->createUser($this->appId, $args)->getUser();
return $this->castUserInfoToPassageUser($user);
} catch (ApiException $e) {
throw PassageError::fromApiException($e);
}
Expand Down Expand Up @@ -263,4 +267,15 @@ public function revokeRefreshTokens(string $userId): void
throw PassageError::fromApiException($e);
}
}

private function castUserInfoToPassageUser(UserInfo $userInfo): PassageUser
{
return unserialize(
preg_replace(
'/^O:\d+:"[^"]++"/',
'O:' . strlen(PassageUser::class) . ':"' . PassageUser::class . '"',
serialize($userInfo)
)
);
}
}
29 changes: 29 additions & 0 deletions custom/test/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Passage\Test;

use Dotenv\Dotenv;
use OpenAPI\Client\Model\UpdateUserRequest;
use Passage\Client\CreateUserArgs;
use Passage\Client\UpdateUserArgs;
use PHPUnit\Framework\TestCase;
use Passage\Client\Passage;
use Passage\Client\PassageError;
Expand All @@ -11,6 +14,7 @@ class UserTest extends TestCase
{
private $appId;
private $apiKey;
private $userId;
private $passage;

public function setUp(): void
Expand All @@ -22,6 +26,7 @@ public function setUp(): void

$this->appId = getenv('APP_ID');
$this->apiKey = getenv('API_KEY');
$this->userId = getenv('EXAMPLE_USER_ID');

$this->passage = new Passage($this->appId, $this->apiKey);
}
Expand All @@ -31,4 +36,28 @@ public function testGetByIdentifierThrowsPassageError()
$this->expectException(PassageError::class);
$this->passage->user->getByIdentifier('error@passage.id');
}

public function testCreate()
{
$expectedEmail = 'php@test.com';
$args = new CreateUserArgs();
$args->setEmail($expectedEmail);

$actual = $this->passage->user->create($args);

$this->assertEquals($expectedEmail, $actual->getEmail());

$this->passage->user->delete($actual->getId());
}

public function testUpdate()
{
$expectedPhone = '+15125555555';
$options = new UpdateUserArgs();
$options->setPhone($expectedPhone);

$actual = $this->passage->user->update($this->userId, $options);

$this->assertEquals($expectedPhone, $actual->getPhone());
}
}

0 comments on commit 7afac85

Please sign in to comment.