From 2de610fe5a1692f0af9b9dec68530373a380d708 Mon Sep 17 00:00:00 2001 From: James Seconde Date: Wed, 1 May 2024 16:49:18 +0100 Subject: [PATCH] WIP --- src/Conversation/Client.php | 7 + .../ConversationCallback.php | 119 ++++++++++++ .../ConversationNumber.php | 53 ++++++ .../NewConversationRequest.php | 177 ++++++++++++++++++ test/Conversation/ClientTest.php | 31 ++- .../Responses/create-conversation.json | 25 +++ 6 files changed, 410 insertions(+), 2 deletions(-) create mode 100644 src/Conversation/ConversationObjects/ConversationCallback.php create mode 100644 src/Conversation/ConversationObjects/ConversationNumber.php create mode 100644 src/Conversation/ConversationObjects/NewConversationRequest.php create mode 100644 test/Conversation/Fixtures/Responses/create-conversation.json diff --git a/src/Conversation/Client.php b/src/Conversation/Client.php index 8d01c74d..38f80864 100644 --- a/src/Conversation/Client.php +++ b/src/Conversation/Client.php @@ -5,6 +5,7 @@ use Vonage\Client\APIClient; use Vonage\Client\APIResource; use Vonage\Conversation\ConversationObjects\Conversation; +use Vonage\Conversation\ConversationObjects\NewConversationRequest; use Vonage\Conversation\Filter\ListConversationFilter; use Vonage\Entity\Hydrator\ArrayHydrator; use Vonage\Entity\IterableAPICollection; @@ -25,6 +26,7 @@ public function listConversations(ListConversationFilter $conversationFilter = n if (!$conversationFilter) { $conversationFilter = new ListConversationFilter(); } + $response = $this->api->search($conversationFilter); $response->setHasPagination(false); $response->setNaiveCount(true); @@ -35,4 +37,9 @@ public function listConversations(ListConversationFilter $conversationFilter = n return $response; } + + public function createConversation(NewConversationRequest $createConversation): ?array + { + return $this->api->create($createConversation->toArray()); + } } diff --git a/src/Conversation/ConversationObjects/ConversationCallback.php b/src/Conversation/ConversationObjects/ConversationCallback.php new file mode 100644 index 00000000..7a0dee1b --- /dev/null +++ b/src/Conversation/ConversationObjects/ConversationCallback.php @@ -0,0 +1,119 @@ +method; + } + + public function setMethod(string $method): ConversationCallback + { + $this->method = $method; + + return $this; + } + + public function getParams(): ?array + { + return $this->params; + } + + public function setParams(?array $params): ConversationCallback + { + $this->params = $params; + + return $this; + } + + public function getUrl(): ?string + { + return $this->url; + } + + public function setUrl(?string $url): ConversationCallback + { + $this->url = $url; + + return $this; + } + + public function getEventMask(): ?string + { + return $this->eventMask; + } + + public function setEventMask(?string $eventMask): ConversationCallback + { + $this->eventMask = $eventMask; + + return $this; + } + + public function setApplicationId(string $applicationId): static + { + $this->params['applicationId'] = $applicationId; + + return $this; + } + + public function setNccoUrl(string $nccoUrl): static + { + $this->params['ncco_url'] = $nccoUrl; + + return $this; + } + + public function fromArray(array $data): static + { + if (isset($data['url'])) { + $this->setUrl($data['url']); + } + + if (isset($data['event_mask'])) { + $this->setEventMask($data['event_mask']); + } + + if (isset($data['params'])) { + $this->setParams($data['params']); + } + + return $this; + } + + public function toArray(): array + { + $returnPayload = []; + + if ($this->getUrl()) { + $returnPayload['url'] = $this->getUrl(); + } + + if ($this->getEventMask()) { + $returnPayload['event_mask'] = $this->getEventMask(); + } + + if ($this->getParams()) { + $returnPayload['params'] = $this->getParams(); + } + + $returnPayload['method'] = $this->getMethod(); + + return $returnPayload; + } +} diff --git a/src/Conversation/ConversationObjects/ConversationNumber.php b/src/Conversation/ConversationObjects/ConversationNumber.php new file mode 100644 index 00000000..df6aa620 --- /dev/null +++ b/src/Conversation/ConversationObjects/ConversationNumber.php @@ -0,0 +1,53 @@ +type; + } + + public function getNumber(): ?string + { + return $this->number; + } + + public function setNumber(?string $number): ConversationNumber + { + $this->number = $number; + + return $this; + } + + public function fromArray(array $data): static + { + if (isset($data['number'])) { + $this->number = $data['number']; + } + + return $this; + } + + public function toArray(): array + { + return [ + 'type' => $this->getType(), + 'number' => $this->getNumber() + ]; + } +} diff --git a/src/Conversation/ConversationObjects/NewConversationRequest.php b/src/Conversation/ConversationObjects/NewConversationRequest.php new file mode 100644 index 00000000..de611989 --- /dev/null +++ b/src/Conversation/ConversationObjects/NewConversationRequest.php @@ -0,0 +1,177 @@ +ttl; + } + + public function setTtl(?int $ttl): NewConversationRequest + { + $this->ttl = $ttl; + + return $this; + } + + public function getCustomData(): ?array + { + return $this->customData; + } + + public function setCustomData(?array $customData): NewConversationRequest + { + $this->customData = $customData; + + return $this; + } + + public function getNumber(): ?ConversationNumber + { + return $this->number; + } + + public function setNumber(ConversationNumber $number): NewConversationRequest + { + $this->number = $number; + + return $this; + } + + public function getConversationCallback(): ?ConversationCallback + { + return $this->conversationCallback; + } + + public function setConversationCallback(?ConversationCallback $conversationCallback): NewConversationRequest + { + $this->conversationCallback = $conversationCallback; + + return $this; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(?string $name): NewConversationRequest + { + $this->name = $name; + + return $this; + } + + public function getDisplayName(): ?string + { + return $this->displayName; + } + + public function setDisplayName(?string $displayName): NewConversationRequest + { + $this->displayName = $displayName; + + return $this; + } + + public function getImageUrl(): ?string + { + return $this->imageUrl; + } + + public function setImageUrl(?string $imageUrl): NewConversationRequest + { + $this->imageUrl = $imageUrl; + + return $this; + } + + public function fromArray(array $data) + { + if (isset($data['name'])) { + $this->setName($data['name']); + } + + if (isset($data['display_name'])) { + $this->setDisplayName($data['display_name']); + } + + if (isset($data['image_url'])) { + $this->setImageUrl($data['image_url']); + } + + if (isset($data['properties']['ttl'])) { + $this->setTtl($data['properties']['ttl']); + } + + if (isset($data['properties']['custom_data'])) { + $this->setCustomData($data['properties']['custom_data']); + } + + if (isset($data['numbers'])) { + $numbers = new ConversationNumber(null); + $numbers->fromArray($data['numbers']); + $this->setNumbers($numbers); + } + + if (isset($data['callback'])) { + $callback = new Callback(); + $callback->fromArray($data['callback']); + $this->setConversationCallback($callback); + } + } + + public function toArray(): array + { + $returnPayload = []; + + if ($this->getName()) { + $returnPayload['name'] = $this->getName(); + } + + if ($this->getDisplayName()) { + $returnPayload['display_name'] = $this->getDisplayName(); + } + + if ($this->getImageUrl()) { + $returnPayload['image_url'] = $this->getImageUrl(); + } + + if ($this->getTtl()) { + $returnPayload['properties']['ttl'] = $this->getTtl(); + } + + if ($this->getCustomData()) { + $returnPayload['properties']['custom_data'] = $this->getCustomData(); + } + + if ($this->getNumber()) { + $returnPayload['numbers'] = $this->getNumber()->toArray(); + } + + if ($this->getConversationCallback()) { + $returnPayload['callback'] = $this->getConversationCallback()->toArray(); + } + + return $returnPayload; + } +} diff --git a/test/Conversation/ClientTest.php b/test/Conversation/ClientTest.php index 7afae3ae..af5115ee 100644 --- a/test/Conversation/ClientTest.php +++ b/test/Conversation/ClientTest.php @@ -12,6 +12,9 @@ use Vonage\Client\APIResource; use Vonage\Conversation\Client as ConversationClient; use Vonage\Conversation\ConversationObjects\Conversation; +use Vonage\Conversation\ConversationObjects\ConversationCallback; +use Vonage\Conversation\ConversationObjects\ConversationNumber; +use Vonage\Conversation\ConversationObjects\NewConversationRequest; use Vonage\Conversation\Filter\ListConversationFilter; use Vonage\Entity\IterableAPICollection; use VonageTest\VonageTestCase; @@ -129,7 +132,7 @@ public function testWillListConversationsByQueryParameters(): void $uri = $request->getUri(); $uriString = $uri->__toString(); - $this->assertEquals('https://api.nexmo.com/v1/conversations?order=desc&page_size=10&cursor=7EjDNQrAcipmOnc0HCzpQRkhBULzY44ljGUX4lXKyUIVfiZay5pv9wg=', $uriString); + $this->assertEquals('https://api.nexmo.com/v1/conversations?date_start=2018-01-01+10%3A00%3A00&date_end=2018-01-01+12%3A00%3A00&page_size=5&order=asc', $uriString); return true; }))->willReturn($this->getResponse('list-conversations')); @@ -154,7 +157,31 @@ public function testWillListConversationsByQueryParameters(): void public function testWillCreateConversation(): void { - $this->markTestIncomplete(); + $this->vonageClient->send(Argument::that(function (Request $request) use (&$requestIndex) { + $this->assertEquals('POST', $request->getMethod()); + + $uri = $request->getUri(); + $uriString = $uri->__toString(); + + $this->assertEquals('https://api.nexmo.com/v1/conversations', $uriString); + + return true; + }))->willReturn($this->getResponse('create-conversation')); + + $conversation = new NewConversationRequest('customer_chat', 'Customer Chat', 'https://example.com/image.png'); + $conversation->setTtl(60); + + $conversationNumber = new ConversationNumber('447700900000'); + + $conversationCallback = new ConversationCallback('https://example.com/eventcallback'); + $conversationCallback->setEventMask('member:invited, member:joined'); + $conversationCallback->setApplicationId('afa393df-2c46-475b-b2d6-92da4ea05481'); + $conversationCallback->setNccoUrl('https://example.com/ncco'); + + $conversation->setNumber($conversationNumber); + $conversation->setConversationCallback($conversationCallback); + + $this->conversationsClient->createConversation($conversation); } public function testWillRetrieveConversation(): void diff --git a/test/Conversation/Fixtures/Responses/create-conversation.json b/test/Conversation/Fixtures/Responses/create-conversation.json new file mode 100644 index 00000000..21ade902 --- /dev/null +++ b/test/Conversation/Fixtures/Responses/create-conversation.json @@ -0,0 +1,25 @@ +{ + "id": "CON-d66d47de-5bcb-4300-94f0-0c9d4b948e9a", + "name": "customer_chat", + "display_name": "Customer Chat", + "image_url": "https://example.com/image.png", + "state": "ACTIVE", + "sequence_number": 0, + "timestamp": { + "created": "2019-09-03T18:40:24.324Z", + "updated": "2019-09-03T18:50:24.324Z", + "destroyed": "2019-09-05T18:40:24.324Z" + }, + "properties": { + "ttl": 60, + "custom_data": { + "property1": "string", + "property2": "string" + } + }, + "_links": { + "self": { + "href": "https://api.nexmo.com/v1/conversations/CON-7f977ca5-6e86-46a8-bdc9-67b9d8c8dfa9" + } + } +} \ No newline at end of file