-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClient.php
executable file
·205 lines (172 loc) · 5.96 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/
namespace Nexmo\Message;
use Nexmo\Client\ClientAwareInterface;
use Nexmo\Client\ClientAwareTrait;
use Nexmo\Client\Exception;
use Zend\Diactoros\Request;
/**
* Class Client
* @method Text sendText(string $to, string $from, string $text, array $additional = []) Send a Test Message
*/
class Client implements ClientAwareInterface
{
use ClientAwareTrait;
/**
* @param Message|array $message
* @return Message
* @throws Exception\Exception
* @throws Exception\Request
* @throws Exception\Server
*/
public function send($message)
{
if(!($message instanceof MessageInterface)){
$message = $this->createMessageFromArray($message);
}
$params = $message->getRequestData(false);
$request = new Request(
\Nexmo\Client::BASE_REST . '/sms/json'
,'POST',
'php://temp',
['content-type' => 'application/json']
);
$request->getBody()->write(json_encode($params));
$message->setRequest($request);
$response = $this->client->send($request);
$message->setResponse($response);
//check for valid data, as well as an error response from the API
$data = $message->getResponseData();
if(!isset($data['messages'])){
throw new Exception\Exception('unexpected response from API');
}
//normalize errors (client vrs server)
foreach($data['messages'] as $part){
switch($part['status']){
case '0':
continue 2; //all okay
case '1':
if(preg_match('#\[\s+(\d+)\s+\]#', $part['error-text'], $match)){
usleep($match[1] + 1);
} else {
sleep(1);
}
return $this->send($message);
case '5':
$e = new Exception\Server($part['error-text'], $part['status']);
$e->setEntity($message);
throw $e;
default:
$e = new Exception\Request($part['error-text'], $part['status']);
$e->setEntity($message);
throw $e;
}
}
return $message;
}
/**
* @param string|MessageInterface $idOrMessage
*/
public function search($idOrMessage)
{
if($idOrMessage instanceof MessageInterface){
$id = $idOrMessage->getMessageId();
$message = $idOrMessage;
} else {
$id = $idOrMessage;
}
$request = new Request(
\Nexmo\Client::BASE_REST . '/search/message?' . http_build_query(['id' => $id]),
'GET',
'php://temp',
['Accept' => 'application/json']
);
$response = $this->client->send($request);
$response->getBody()->rewind();
$data = json_decode($response->getBody()->getContents(), true);
if(!$data){
throw new Exception\Request('no message found for `' . $id . '`');
}
if($response->getStatusCode() != '200' && isset($data['error-code'])){
throw new Exception\Request($data['error-code-label'], $data['error-code']);
} elseif($response->getStatusCode() != '200'){
throw new Exception\Request('error status from API', $response->getStatusCode());
}
switch($data['type']){
case 'MT':
$new = new Message($data['message-id']);
break;
case 'MO':
$new = new InboundMessage($data['message-id']);
break;
default:
throw new Exception\Exception('unexpected response from API');
}
if(isset($message) && !($message instanceof $new)){
throw new Exception\Exception(sprintf(
'searched for message with type `%s` but message of type `%s`',
get_class($message),
get_class($new)
));
}
if(!isset($message)){
$message = $new;
}
$message->setResponse($response);
return $message;
}
/**
* @param array $message
* @return Message
*/
protected function createMessageFromArray($message)
{
if(!is_array($message)){
throw new \RuntimeException('message must implement `' . MessageInterface::class . '` or be an array`');
}
foreach(['to', 'from'] as $param){
if(!isset($message[$param])){
throw new \InvalidArgumentException('missing expected key `' . $param . '`');
}
}
$to = $message['to'];
$from = $message['from'];
unset($message['to']);
unset($message['from']);
return new Message($to, $from, $message);
}
/**
* Convenience feature allowing messages to be sent without creating a message object first.
*
* @param $name
* @param $arguments
* @return MessageInterface
*/
public function __call($name, $arguments)
{
if(!(strstr($name, 'send') !== 0)){
throw new \RuntimeException(sprintf(
'`%s` is not a valid method on `%s`',
$name,
get_class($this)
));
}
$class = substr($name, 4);
$class = 'Nexmo\\Message\\' . ucfirst(strtolower($class));
if(!class_exists($class)){
throw new \RuntimeException(sprintf(
'`%s` is not a valid method on `%s`',
$name,
get_class($this)
));
}
$reflection = new \ReflectionClass($class);
$message = $reflection->newInstanceArgs($arguments);
return $this->send($message);
}
}