-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNexmo.php
128 lines (116 loc) · 3.25 KB
/
Nexmo.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
<?php
declare(strict_types=1);
namespace Matthewbdaly\SMS\Drivers;
use GuzzleHttp\ClientInterface as GuzzleClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ServerException;
use Psr\Http\Message\ResponseInterface;
use Matthewbdaly\SMS\Contracts\Driver;
use Matthewbdaly\SMS\Exceptions\DriverNotConfiguredException;
/**
* Driver for Nexmo.
*/
class Nexmo implements Driver
{
/**
* Guzzle client.
*
* @var
*/
protected $client;
/**
* Guzzle response.
*
* @var
*/
protected $response;
/**
* Endpoint.
*
* @var
*/
private $endpoint = 'https://rest.nexmo.com/sms/json';
/**
* API Key.
*
* @var
*/
private $apiKey;
/**
* API Secret.
*
* @var
*/
private $apiSecret;
/**
* Constructor.
*
* @param GuzzleClient $client The Guzzle Client instance.
* @param ResponseInterface $response The response instance.
* @param array $config The configuration array.
* @throws DriverNotConfiguredException Driver not configured correctly.
*
* @return void
*/
public function __construct(GuzzleClient $client, ResponseInterface $response, array $config)
{
$this->client = $client;
$this->response = $response;
if (! array_key_exists('api_key', $config) || ! array_key_exists('api_secret', $config)) {
throw new DriverNotConfiguredException();
}
$this->apiKey = $config['api_key'];
$this->apiSecret = $config['api_secret'];
}
/**
* Get driver name.
*
* @return string
*/
public function getDriver(): string
{
return 'Nexmo';
}
/**
* Get endpoint URL.
*
* @return string
*/
public function getEndpoint(): string
{
return $this->endpoint;
}
/**
* Send the SMS.
*
* @param array $message An array containing the message.
*
* @throws \Matthewbdaly\SMS\Exceptions\ClientException Client exception.
* @throws \Matthewbdaly\SMS\Exceptions\ServerException Server exception.
* @throws \Matthewbdaly\SMS\Exceptions\RequestException Request exception.
* @throws \Matthewbdaly\SMS\Exceptions\ConnectException Connect exception.
*
* @return boolean
*/
public function sendRequest(array $message): bool
{
try {
$message['api_key'] = $this->apiKey;
$message['api_secret'] = $this->apiSecret;
$message['text'] = $message['content'];
unset($message['content']);
$response = $this->client->request('POST', $this->getEndpoint().'?'.http_build_query($message));
} catch (ClientException $e) {
throw new \Matthewbdaly\SMS\Exceptions\ClientException();
} catch (ServerException $e) {
throw new \Matthewbdaly\SMS\Exceptions\ServerException();
} catch (ConnectException $e) {
throw new \Matthewbdaly\SMS\Exceptions\ConnectException();
} catch (RequestException $e) {
throw new \Matthewbdaly\SMS\Exceptions\RequestException();
}
return true;
}
}