Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK committed Apr 30, 2024
1 parent 8cc9df0 commit 68aed3a
Show file tree
Hide file tree
Showing 8 changed files with 475 additions and 1 deletion.
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<testsuite name="verify2">
<directory>test/Verify2</directory>
</testsuite>
<testsuite name="conversations">
<directory>test/Conversation</directory>
</testsuite>
<testsuite name="proactive_connect">
<directory>test/ProactiveConnect</directory>
</testsuite>
Expand Down
38 changes: 38 additions & 0 deletions src/Conversation/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Vonage\Conversation;

use Vonage\Client\APIClient;
use Vonage\Client\APIResource;
use Vonage\Conversation\ConversationObjects\Conversation;
use Vonage\Conversation\Filter\ListConversationFilter;
use Vonage\Entity\Hydrator\ArrayHydrator;
use Vonage\Entity\IterableAPICollection;

class Client implements APIClient
{
public function __construct(protected APIResource $api)
{
}

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

public function listConversations(ListConversationFilter $conversationFilter = null): IterableAPICollection
{
if (!$conversationFilter) {
$conversationFilter = new ListConversationFilter();
}
$response = $this->api->search($conversationFilter);
$response->setHasPagination(false);
$response->setNaiveCount(true);

$hydrator = new ArrayHydrator();
$hydrator->setPrototype(new Conversation());
$response->setHydrator($hydrator);

return $response;
}
}
22 changes: 22 additions & 0 deletions src/Conversation/ClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Vonage\Conversation;

use Psr\Container\ContainerInterface;
use Vonage\Client\APIResource;
use Vonage\Client\Credentials\Handler\KeypairHandler;
use Vonage\Verify2\Client;

class ClientFactory
{
public function __invoke(ContainerInterface $container): Client
{
$api = $container->make(APIResource::class);
$api->setIsHAL(true)
->setErrorsOn200(false)
->setAuthHandler(new KeypairHandler())
->setBaseUrl('https://api.nexmo.com/v1/conversations');

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

declare(strict_types=1);

namespace Vonage\Conversation\ConversationObjects;

use Vonage\Entity\Hydrator\ArrayHydrateInterface;

class Conversation implements ArrayHydrateInterface
{
protected array $data;

public function __construct()
{
}

public function fromArray(array $data): void
{
$this->data = $data;
}

public function toArray(): array
{
return $this->data;
}

public function __get(string $name)
{
return $this->data[$name];
}

public function __set(string $name, $value)
{
$this->data[$name] = $value;
}

public function __isset(string $name): bool
{
return isset($this->data[$name]);
}
}
102 changes: 102 additions & 0 deletions src/Conversation/Filter/ListConversationFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Vonage\Conversation\Filter;

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

class ListConversationFilter implements FilterInterface
{
protected ?string $startDate = null;
protected ?string $endDate = null;
protected int $pageSize = 10;
protected string $order = 'asc';
private const VALID_ORDERS = ['asc', 'ASC', 'desc', 'DESC'];
protected ?string $cursor = null;

public function __construct()
{
}

public function getStartDate(): ?string
{
return $this->startDate;
}

public function setStartDate(?string $startDate): ListConversationFilter
{
$this->startDate = $startDate;

return $this;
}

public function getEndDate(): ?string
{
return $this->endDate;
}

public function setEndDate(?string $endDate): ListConversationFilter
{
$this->endDate = $endDate;

return $this;
}

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

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

return $this;
}

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

public function setOrder(string $order): ListConversationFilter
{
if (!in_array($order, self::VALID_ORDERS)) {
throw new \InvalidArgumentException($order . ' is an invalid order value');
}

$this->order = $order;

return $this;
}

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

return $this;
}

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

if ($this->getStartDate()) {
$query['date_start'] = $this->getStartDate();
}

if ($this->getEndDate()) {
$query['date_end'] = $this->getEndDate();
}

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

$query['order'] = $this->getOrder();

return $query;
}
}
2 changes: 1 addition & 1 deletion src/Entity/IterableAPICollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class IterableAPICollection implements ClientAwareInterface, Iterator, Countable
protected APIResource $api;

/**
* This allows for override if the endpoint API uses a different query key
* This allows for override if the endpoint API uÏses a different query key
*/
protected string $pageIndexKey = 'page_index';

Expand Down
Loading

0 comments on commit 68aed3a

Please sign in to comment.