-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add new classes for PassageUser, CreateUserArgs, and UpdateUserArgs #90
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
} | ||
|
@@ -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 | ||
bertrmz marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused by this. Why is it was expecting one class but receiving another? For instance, why would createUser not expect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's because of the class aliasing. the new functions take a different class name. in the other languages it was as easy as type aliasing but i tried php's |
||
{ | ||
return unserialize( | ||
preg_replace( | ||
'/^O:\d+:"[^"]++"/', | ||
'O:' . strlen($class) . ':"' . $class . '"', | ||
serialize($object) | ||
) | ||
); | ||
} | ||
} |
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 | ||
{ | ||
} |
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 | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
Comment on lines
-10
to
-12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wasn't working in the Passage.php file so you created the workaround? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that was working, but only within the scope of this file. it didn't actually alias the class names for an external dev |
||
use OpenAPI\Client\Model\UserInfo; | ||
use OpenAPI\Client\Model\WebAuthnDevices; | ||
use OpenAPI\Client\ApiException; | ||
|
||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
} | ||
|
@@ -263,4 +267,15 @@ public function revokeRefreshTokens(string $userId): void | |
throw PassageError::fromApiException($e); | ||
} | ||
} | ||
|
||
private function castUserInfoToPassageUser(UserInfo $userInfo): PassageUser | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. duplicated just to keep this function private and to not have to create a whole base class to inherit from just for this. |
||
{ | ||
return unserialize( | ||
preg_replace( | ||
'/^O:\d+:"[^"]++"/', | ||
'O:' . strlen(PassageUser::class) . ':"' . PassageUser::class . '"', | ||
serialize($userInfo) | ||
) | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -11,6 +14,7 @@ class UserTest extends TestCase | |
{ | ||
private $appId; | ||
private $apiKey; | ||
private $userId; | ||
private $passage; | ||
|
||
public function setUp(): void | ||
|
@@ -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); | ||
} | ||
|
@@ -31,4 +36,28 @@ public function testGetByIdentifierThrowsPassageError() | |
$this->expectException(PassageError::class); | ||
$this->passage->user->getByIdentifier('[email protected]'); | ||
} | ||
|
||
public function testCreate() | ||
{ | ||
$expectedEmail = '[email protected]'; | ||
$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()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
couldn't get
class_alias
to resolve the imports correctly, so just went with creating new classes. These are only used with the new function signatures. All of the old ones still use the same codegen'd classes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed?
use OpenAPI\Client\Model\CreateUserRequest as CreateUserArgs;
wasn't working in the Passage.php file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class CreateUserArgs extends CreateUserRequest
creates a new class with the name, which is available to be imported/used by any dev external to this fileuse OpenAPI\Client\Model\CreateUserRequest as CreateUserArgs;
only allows theCreateUserArgs
name to be used within the file the line is written in