Skip to content

Commit

Permalink
Add Users Client (#420)
Browse files Browse the repository at this point in the history
* Hacks for APIcollection (again), logic for 5 endpoints

* Move Users into its own namespace

* PR review changes, pt 1

* PR review changes, pt 2

* Pass in hydrator

* reset verify, make User full DTO

* Correct auth, add more tests

* Update src/Users/Client.php

Co-authored-by: Chris Tankersley <[email protected]>

* Update src/Users/Client.php

Co-authored-by: Chris Tankersley <[email protected]>

* Update src/Users/ClientFactory.php

Co-authored-by: Chris Tankersley <[email protected]>

* Update src/Users/Client.php

Co-authored-by: Chris Tankersley <[email protected]>

* Update src/Users/ClientFactory.php

Co-authored-by: Chris Tankersley <[email protected]>

* Update src/Users/Client.php

Co-authored-by: Chris Tankersley <[email protected]>

* pass in standard array hydrator

---------

Co-authored-by: Chris Tankersley <[email protected]>
  • Loading branch information
SecondeJK and dragonmantank authored Jul 25, 2023
1 parent b88a1fe commit d4f2151
Show file tree
Hide file tree
Showing 14 changed files with 1,041 additions and 8 deletions.
6 changes: 6 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
<testsuite name="meetings">
<directory>test/Meetings</directory>
</testsuite>
<testsuite name="application">
<directory>test/Application</directory>
</testsuite>
<testsuite name="users">
<directory>test/Users</directory>
</testsuite>
</testsuites>
<php>
<ini name='error_reporting' value='E_ALL' />
Expand Down
3 changes: 3 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use Vonage\SMS\ClientFactory as SMSClientFactory;
use Vonage\Subaccount\ClientFactory as SubaccountClientFactory;
use Vonage\Messages\ClientFactory as MessagesClientFactory;
use Vonage\Users\ClientFactory as UsersClientFactory;
use Vonage\Verify\ClientFactory as VerifyClientFactory;
use Vonage\Verify2\ClientFactory as Verify2ClientFactory;
use Vonage\Verify\Verification;
Expand Down Expand Up @@ -85,6 +86,7 @@
* @method Secrets\Client secrets()
* @method SMS\Client sms()
* @method Subaccount\Client subaccount()
* @method Users\Client users()
* @method Verify\Client verify()
* @method Verify2\Client verify2()
* @method Voice\Client voice()
Expand Down Expand Up @@ -224,6 +226,7 @@ public function __construct(
'secrets' => SecretsClientFactory::class,
'sms' => SMSClientFactory::class,
'subaccount' => SubaccountClientFactory::class,
'users' => UsersClientFactory::class,
'verify' => VerifyClientFactory::class,
'verify2' => Verify2ClientFactory::class,
'voice' => VoiceClientFactory::class,
Expand Down
25 changes: 23 additions & 2 deletions src/Entity/IterableAPICollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class IterableAPICollection implements ClientAwareInterface, Iterator, Countable
protected bool $noQueryParameters = false;

/**
* User set pgge sixe.
* User set pgge size.
*/
protected ?int $size = null;

Expand All @@ -123,6 +123,12 @@ class IterableAPICollection implements ClientAwareInterface, Iterator, Countable

protected $hydrator;

/**
* Used to override pagination to remove it from the URL page query
* but differs from noQueryParameters when you want to use other filters
*/
protected bool $hasPagination = true;

public function getPageIndexKey(): string
{
return $this->pageIndexKey;
Expand Down Expand Up @@ -496,7 +502,7 @@ protected function fetchPage($absoluteUri): void
$query[$this->pageSizeKey] = $this->size;
}

if (isset($this->index)) {
if (isset($this->index) && $this->hasPagination()) {
$query[$this->pageIndexKey] = $this->index;
}

Expand Down Expand Up @@ -625,4 +631,19 @@ public function setIndex(?int $index): IterableAPICollection

return $this;
}

/**
* @return bool
*/
public function hasPagination(): bool
{
return $this->hasPagination;
}

public function setHasPagination(bool $hasPagination): static
{
$this->hasPagination = $hasPagination;

return $this;
}
}
82 changes: 82 additions & 0 deletions src/Users/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Vonage\Users;

use Vonage\Client\APIClient;
use Vonage\Client\APIResource;
use Vonage\Client\ClientAwareInterface;
use Vonage\Client\ClientAwareTrait;
use Vonage\Client\Exception\Exception as ClientException;
use Vonage\Entity\Filter\EmptyFilter;
use Vonage\Entity\Hydrator\HydratorInterface;
use Vonage\Entity\IterableAPICollection;
use Vonage\Entity\Filter\FilterInterface;

use Vonage\Users\Filter\UserFilter;
use function is_null;

class Client implements ClientAwareInterface, APIClient
{
use ClientAwareTrait;

public function __construct(protected APIResource $api, protected ?HydratorInterface $hydrator = null)
{
}

public function getApiResource(): APIResource
{
return $this->api;
}

public function listUsers(FilterInterface $filter = null): IterableAPICollection
{
if (is_null($filter)) {
$filter = new EmptyFilter();
}

$response = $this->api->search($filter);
$response->setHydrator($this->hydrator);
$response->setPageSizeKey('page_size');
$response->setHasPagination(false);

return $response;
}

public function createUser(User $user): User
{
$response = $this->api->create($user->toArray());

return $this->hydrator->hydrate($response);
}

public function getUser(string $id): User
{
$response = $this->api->get($id);
return $this->hydrator->hydrate($response);

return $returnUser;
}

public function updateUser(User $user): User
{
if (is_null($user->getId())) {
throw new \InvalidArgumentException('User must have an ID set');
}

$response = $this->api->partiallyUpdate($user->getId(), $user->toArray());

return $this->hydrator->hydrate($response);
}

public function deleteUser(string $id): bool
{
try {
$this->api->delete($id);
return true;
} catch (ClientException $exception) {
return false;
}
}
}
27 changes: 27 additions & 0 deletions src/Users/ClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Vonage\Users;

use Psr\Container\ContainerInterface;
use Vonage\Client\APIResource;
use Vonage\Client\Credentials\Handler\KeypairHandler;
use Vonage\Entity\Hydrator\ArrayHydrator;

class ClientFactory
{
public function __invoke(ContainerInterface $container): Client
{
$api = $container->make(APIResource::class);
$api
->setBaseUri('/v1/users')
->setCollectionName('users')
->setAuthHandler(new KeypairHandler());

$hydrator = new ArrayHydrator();
$hydrator->setPrototype(new User());

return new Client($api, $hydrator);
}
}
78 changes: 78 additions & 0 deletions src/Users/Filter/UserFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Vonage\Users\Filter;

use InvalidArgumentException;
use Vonage\Entity\Filter\FilterInterface;

class UserFilter implements FilterInterface
{
public const ORDER_ASC = 'asc';
public const ORDER_DESC = 'desc';

protected ?int $pageSize = null;

protected ?string $order = null;
protected ?string $cursor = null;

public function getQuery(): array
{
$query = [];

if ($this->pageSize !== null) {
$query['page_size'] = $this->getPageSize();
}

if ($this->order !== null) {
$query['order'] = $this->getOrder();
}

if ($this->cursor !== null) {
$query['cursor'] = $this->getCursor();
}

return $query;
}

public function getPageSize(): int
{
return $this->pageSize;
}

public function setPageSize(int $pageSize): static
{
$this->pageSize = $pageSize;

return $this;
}

public function getOrder(): string
{
return $this->order;
}

public function setOrder(string $order): static
{
if ($order !== self::ORDER_ASC && $order !== self::ORDER_DESC) {
throw new InvalidArgumentException('Order must be `asc` or `desc`');
}

$this->order = $order;

return $this;
}

public function getCursor(): ?string
{
return $this->cursor;
}

public function setCursor(?string $cursor): static
{
$this->cursor = $cursor;

return $this;
}
}
Loading

0 comments on commit d4f2151

Please sign in to comment.