Skip to content
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

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

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.

Copy link
Collaborator

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?

Copy link
Contributor Author

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 file

use OpenAPI\Client\Model\CreateUserRequest as CreateUserArgs; only allows the CreateUserArgs name to be used within the file the line is written in

{
}
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 CreateUserRequest? This is the class that is genreated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 class_alias, which didn't seem to work to allow an end user to use the aliased class names. so i had to create new classes that are just copies of the generated classes with different names. because of that, it was necessary to cast the classes otherwise it threw an error at runtime

{
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;
Comment on lines -10 to -12
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
)
);
}
}
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('[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());
}
}
Loading