Skip to content

Commit 0a9214e

Browse files
authored
Initial Stitch support (#99)
* Add conversation support * User support added * Support for adding/leaving conversations
1 parent 7c00032 commit 0a9214e

File tree

16 files changed

+1164
-5
lines changed

16 files changed

+1164
-5
lines changed

src/Client.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public function __construct(CredentialsInterface $credentials, $options = array(
9999
'numbers' => 'Nexmo\Numbers\Client',
100100
'calls' => 'Nexmo\Call\Collection',
101101
'conversion' => 'Nexmo\Conversion\Client',
102+
'conversation' => 'Nexmo\Conversations\Collection',
103+
'user' => 'Nexmo\User\Collection',
102104
], $this));
103105
}
104106

src/Conversations/Collection.php

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2018 Nexmo, Inc. (http://nexmo.com)
6+
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
7+
*/
8+
9+
namespace Nexmo\Conversations;
10+
11+
use Nexmo\Client\ClientAwareInterface;
12+
use Nexmo\Client\ClientAwareTrait;
13+
use Nexmo\Entity\CollectionInterface;
14+
use Nexmo\Entity\CollectionTrait;
15+
use Nexmo\Entity\JsonResponseTrait;
16+
use Nexmo\Entity\JsonSerializableTrait;
17+
use Nexmo\Entity\NoRequestResponseTrait;
18+
use Psr\Http\Message\ResponseInterface;
19+
use Zend\Diactoros\Request;
20+
use Nexmo\Client\Exception;
21+
22+
class Collection implements ClientAwareInterface, CollectionInterface, \ArrayAccess
23+
{
24+
use ClientAwareTrait;
25+
use CollectionTrait;
26+
use JsonSerializableTrait;
27+
use NoRequestResponseTrait;
28+
use JsonResponseTrait;
29+
30+
public static function getCollectionName()
31+
{
32+
return 'conversations';
33+
}
34+
35+
public static function getCollectionPath()
36+
{
37+
return '/beta/' . self::getCollectionName();
38+
}
39+
40+
public function hydrateEntity($data, $idOrConversation)
41+
{
42+
if(!($idOrConversation instanceof Conversation)){
43+
$idOrConversation = new Conversation($idOrConversation);
44+
}
45+
46+
$idOrConversation->setClient($this->getClient());
47+
$idOrConversation->jsonUnserialize($data);
48+
49+
return $idOrConversation;
50+
}
51+
52+
/**
53+
* @param null $conversation
54+
* @return $this|Conversation
55+
*/
56+
public function __invoke(Filter $filter = null)
57+
{
58+
if(!is_null($filter)){
59+
$this->setFilter($filter);
60+
}
61+
62+
return $this;
63+
}
64+
65+
public function create($conversation)
66+
{
67+
return $this->post($conversation);
68+
}
69+
70+
public function post($conversation)
71+
{
72+
if($conversation instanceof Conversation){
73+
$body = $conversation->getRequestData();
74+
} else {
75+
$body = $conversation;
76+
}
77+
78+
$request = new Request(
79+
\Nexmo\Client::BASE_API . $this->getCollectionPath()
80+
,'POST',
81+
'php://temp',
82+
['content-type' => 'application/json']
83+
);
84+
85+
$request->getBody()->write(json_encode($body));
86+
$response = $this->getClient()->send($request);
87+
88+
if($response->getStatusCode() != '200'){
89+
throw $this->getException($response);
90+
}
91+
92+
$body = json_decode($response->getBody()->getContents(), true);
93+
$conversation = new Conversation($body['uuid']);
94+
$conversation->jsonUnserialize($body);
95+
$conversation->setClient($this->getClient());
96+
97+
return $conversation;
98+
}
99+
100+
public function get($conversation)
101+
{
102+
if(!($conversation instanceof Conversation)){
103+
$conversation = new Conversation($conversation);
104+
}
105+
106+
$conversation->setClient($this->getClient());
107+
$conversation->get();
108+
109+
return $conversation;
110+
}
111+
112+
protected function getException(ResponseInterface $response)
113+
{
114+
$body = json_decode($response->getBody()->getContents(), true);
115+
$status = $response->getStatusCode();
116+
117+
// This message isn't very useful, but we shouldn't ever see it
118+
$errorTitle = 'Unexpected error';
119+
120+
if (isset($body['description'])) {
121+
$errorTitle = $body['description'];
122+
}
123+
124+
if (isset($body['error_title'])) {
125+
$errorTitle = $body['error_title'];
126+
}
127+
128+
if($status >= 400 AND $status < 500) {
129+
$e = new Exception\Request($errorTitle, $status);
130+
} elseif($status >= 500 AND $status < 600) {
131+
$e = new Exception\Server($errorTitle, $status);
132+
} else {
133+
$e = new Exception\Exception('Unexpected HTTP Status Code');
134+
throw $e;
135+
}
136+
137+
return $e;
138+
}
139+
140+
public function offsetExists($offset)
141+
{
142+
return true;
143+
}
144+
145+
/**
146+
* @param mixed $conversation
147+
* @return Conversation
148+
*/
149+
public function offsetGet($conversation)
150+
{
151+
if(!($conversation instanceof Conversation)){
152+
$conversation = new Conversation($conversation);
153+
}
154+
155+
$conversation->setClient($this->getClient());
156+
return $conversation;
157+
}
158+
159+
public function offsetSet($offset, $value)
160+
{
161+
throw new \RuntimeException('can not set collection properties');
162+
}
163+
164+
public function offsetUnset($offset)
165+
{
166+
throw new \RuntimeException('can not unset collection properties');
167+
}
168+
}

src/Conversations/Conversation.php

Lines changed: 157 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,174 @@
99
namespace Nexmo\Conversations;
1010

1111

12-
class Conversation
12+
use Nexmo\Client\ClientAwareInterface;
13+
use Nexmo\Client\ClientAwareTrait;
14+
use Nexmo\Entity\EntityInterface;
15+
use Nexmo\Entity\JsonResponseTrait;
16+
use Nexmo\Entity\JsonSerializableTrait;
17+
use Nexmo\Entity\JsonUnserializableInterface;
18+
use Nexmo\Entity\NoRequestResponseTrait;
19+
use Nexmo\User\Collection as UserCollection;
20+
use Nexmo\User\User;
21+
use Psr\Http\Message\ResponseInterface;
22+
use Zend\Diactoros\Request;
23+
use Nexmo\Client\Exception;
24+
25+
class Conversation implements EntityInterface, \JsonSerializable, JsonUnserializableInterface, ClientAwareInterface
1326
{
14-
protected $id;
27+
28+
use NoRequestResponseTrait;
29+
use JsonSerializableTrait;
30+
use JsonResponseTrait;
31+
use ClientAwareTrait;
32+
33+
protected $data = [];
1534

1635
public function __construct($id = null)
1736
{
18-
$this->id = $id;
37+
$this->data['id'] = $id;
38+
}
39+
40+
public function setName($name)
41+
{
42+
$this->data['name'] = $name;
43+
return $this;
44+
}
45+
46+
public function setDisplayName($name)
47+
{
48+
$this->data['display_name'] = $name;
49+
return $this;
1950
}
2051

2152
public function getId()
2253
{
23-
return $this->id;
54+
if (isset($this->data['uuid'])) {
55+
return $this->data['uuid'];
56+
}
57+
return $this->data['id'];
2458
}
2559

2660
public function __toString()
2761
{
28-
return (string) $this->getId();
62+
return (string)$this->getId();
63+
}
64+
65+
66+
public function get()
67+
{
68+
$request = new Request(
69+
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId()
70+
,'GET'
71+
);
72+
73+
$response = $this->getClient()->send($request);
74+
75+
if($response->getStatusCode() != '200'){
76+
throw $this->getException($response);
77+
}
78+
79+
$data = json_decode($response->getBody()->getContents(), true);
80+
$this->jsonUnserialize($data);
81+
82+
return $this;
83+
}
84+
85+
86+
public function jsonSerialize()
87+
{
88+
return $this->data;
89+
}
90+
91+
public function jsonUnserialize(array $json)
92+
{
93+
$this->data = $json;
94+
}
95+
96+
public function members()
97+
{
98+
$response = $this->getClient()->get(\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId() .'/members');
99+
100+
if($response->getStatusCode() != '200'){
101+
throw $this->getException($response);
102+
}
103+
104+
$data = json_decode($response->getBody()->getContents(), true);
105+
$memberCollection = new UserCollection();
106+
return $memberCollection->hydrateAll($data);
107+
}
108+
109+
public function addMember(User $user)
110+
{
111+
return $this->sendPostAction($user, 'join');
112+
}
113+
114+
public function inviteMember(User $user)
115+
{
116+
return $this->sendPostAction($user, 'invite');
117+
}
118+
119+
public function removeMember(User $user)
120+
{
121+
$response = $this->getClient()->delete(
122+
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId() .'/members/'. $user->getId()
123+
);
124+
125+
if($response->getStatusCode() != '200'){
126+
throw $this->getException($response);
127+
}
128+
}
129+
130+
public function sendPostAction(User $user, $action, $channel = 'app') {
131+
$body = $user->getRequestDataForConversation();
132+
$body['action'] = $action;
133+
$body['channel'] = ['type' => $channel];
134+
135+
$response = $this->getClient()->post(
136+
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId() .'/members',
137+
$body
138+
);
139+
140+
if($response->getStatusCode() != '200'){
141+
throw $this->getException($response);
142+
}
143+
144+
$body = json_decode($response->getBody()->getContents(), true);
145+
146+
$user = new User($body['user_id']);
147+
$user->jsonUnserialize($body);
148+
$user->setClient($this->getClient());
149+
150+
return $user;
151+
}
152+
153+
protected function getException(ResponseInterface $response)
154+
{
155+
$body = json_decode($response->getBody()->getContents(), true);
156+
$status = $response->getStatusCode();
157+
158+
// This message isn't very useful, but we shouldn't ever see it
159+
$errorTitle = 'Unexpected error';
160+
161+
if (isset($body['description'])) {
162+
$errorTitle = $body['description'];
163+
}
164+
165+
if (isset($body['error_title'])) {
166+
$errorTitle = $body['error_title'];
167+
}
168+
169+
if($status >= 400 AND $status < 500) {
170+
$e = new Exception\Request($errorTitle, $status);
171+
} elseif($status >= 500 AND $status < 600) {
172+
$e = new Exception\Server($errorTitle, $status);
173+
} else {
174+
$e = new Exception\Exception('Unexpected HTTP Status Code');
175+
throw $e;
176+
}
177+
178+
return $e;
29179
}
180+
181+
30182
}

0 commit comments

Comments
 (0)