-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
b88a1fe
commit d4f2151
Showing
14 changed files
with
1,041 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.