1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\Sipgate; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Orkhanahmadov\Sipgate\Resources\Call; |
7
|
|
|
use Orkhanahmadov\Sipgate\Resources\Device; |
8
|
|
|
use Orkhanahmadov\Sipgate\Resources\History; |
9
|
|
|
use Orkhanahmadov\Sipgate\Resources\User; |
10
|
|
|
|
11
|
|
|
class Sipgate implements Telephony |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string|null |
15
|
|
|
*/ |
16
|
|
|
private $username = null; |
17
|
|
|
/** |
18
|
|
|
* @var string|null |
19
|
|
|
*/ |
20
|
|
|
private $password = null; |
21
|
|
|
/** |
22
|
|
|
* @var Client |
23
|
|
|
*/ |
24
|
|
|
private $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Sipgate constructor. |
28
|
|
|
* |
29
|
|
|
* @param string|null $username |
30
|
|
|
* @param string|null $password |
31
|
|
|
*/ |
32
|
|
|
public function __construct(?string $username = null, ?string $password = null) |
33
|
|
|
{ |
34
|
|
|
$this->username = $username; |
35
|
|
|
$this->password = $password; |
36
|
|
|
|
37
|
|
|
$this->client = new Client(['base_uri' => 'https://api.sipgate.com/v2/']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Sets basic auth credentials. |
42
|
|
|
* |
43
|
|
|
* @param string $username |
44
|
|
|
* @param string $password |
45
|
|
|
* |
46
|
|
|
* @return Sipgate |
47
|
|
|
*/ |
48
|
|
|
public function setBasicAuthCredentials(string $username, string $password) |
49
|
|
|
{ |
50
|
|
|
$this->username = $username; |
51
|
|
|
$this->password = $password; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns account details. |
58
|
|
|
* |
59
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
60
|
|
|
* |
61
|
|
|
* @return array|null |
62
|
|
|
*/ |
63
|
|
|
public function account(): ?array |
64
|
|
|
{ |
65
|
|
|
return $this->sendRequest('account'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns all created users. |
70
|
|
|
* |
71
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function users(): array |
76
|
|
|
{ |
77
|
|
|
$response = $this->sendRequest('users'); |
78
|
|
|
|
79
|
|
|
$users = []; |
80
|
|
|
foreach ($response['items'] as $user) { |
81
|
|
|
array_push($users, new User($user)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $users; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns user devices. |
89
|
|
|
* |
90
|
|
|
* @param User|string $user |
91
|
|
|
* |
92
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function devices($user): array |
97
|
|
|
{ |
98
|
|
|
$userId = $user instanceof User ? $user->id : $user; |
99
|
|
|
|
100
|
|
|
$response = $this->sendRequest($userId.'/devices'); |
101
|
|
|
|
102
|
|
|
$devices = []; |
103
|
|
|
foreach ($response['items'] as $device) { |
104
|
|
|
array_push($devices, new Device($user, $device)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $devices; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns currently established calls. |
112
|
|
|
* |
113
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function calls(): array |
118
|
|
|
{ |
119
|
|
|
$response = $this->sendRequest('calls'); |
120
|
|
|
|
121
|
|
|
$calls = []; |
122
|
|
|
foreach ($response['data'] as $call) { |
123
|
|
|
array_push($calls, new Call($call)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $calls; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Initiates new call and returns session ID. |
131
|
|
|
* |
132
|
|
|
* @param Device|string $device |
133
|
|
|
* @param string|int $callee |
134
|
|
|
* @param string|int|null $callerId |
135
|
|
|
* |
136
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function initiateCall($device, $callee, $callerId = null): string |
141
|
|
|
{ |
142
|
|
|
$response = $this->sendRequest('sessions/calls', 'POST', [ |
143
|
|
|
'json' => [ |
144
|
|
|
'caller' => $device instanceof Device ? $device->id : $device, |
145
|
|
|
'callee' => $callee, |
146
|
|
|
'callerId' => $callerId, |
147
|
|
|
], |
148
|
|
|
]); |
149
|
|
|
|
150
|
|
|
return $response['sessionId']; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Hangs up active call. |
155
|
|
|
* |
156
|
|
|
* @param string $callId |
157
|
|
|
* |
158
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function hangupCall(string $callId): bool |
163
|
|
|
{ |
164
|
|
|
$this->sendRequest('calls/'.$callId, 'DELETE'); |
165
|
|
|
|
166
|
|
|
return true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Starts or stops call recording. |
171
|
|
|
* |
172
|
|
|
* @param string $callId |
173
|
|
|
* @param bool $value |
174
|
|
|
* @param bool $announcement |
175
|
|
|
* |
176
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function recordCall(string $callId, bool $value, bool $announcement): bool |
181
|
|
|
{ |
182
|
|
|
$this->sendRequest('calls/'.$callId.'/recording', 'PUT', [ |
183
|
|
|
'json' => [ |
184
|
|
|
'value' => $value, |
185
|
|
|
'announcement' => $announcement, |
186
|
|
|
], |
187
|
|
|
]); |
188
|
|
|
|
189
|
|
|
return $value; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns call history. |
194
|
|
|
* |
195
|
|
|
* @param array $options |
196
|
|
|
* |
197
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
198
|
|
|
* |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
|
|
public function history(array $options = []): array |
202
|
|
|
{ |
203
|
|
|
$response = $this->sendRequest('history', 'GET', ['query' => $options]); |
204
|
|
|
|
205
|
|
|
$history = []; |
206
|
|
|
foreach ($response['items'] as $item) { |
207
|
|
|
array_push($history, new History($item)); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $history; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Sends API requests to sipgate endpoint. |
215
|
|
|
* |
216
|
|
|
* @param string $url |
217
|
|
|
* @param string $method |
218
|
|
|
* @param array $options |
219
|
|
|
* |
220
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
221
|
|
|
* |
222
|
|
|
* @return array|null |
223
|
|
|
*/ |
224
|
|
|
private function sendRequest(string $url, string $method = 'GET', array $options = []): ?array |
225
|
|
|
{ |
226
|
|
|
$response = $this->client->request($method, $url, array_merge( |
227
|
|
|
[ |
228
|
|
|
'headers' => ['Accept' => 'application/json'], |
229
|
|
|
'auth' => [$this->username, $this->password], |
230
|
|
|
], |
231
|
|
|
$options |
232
|
|
|
)); |
233
|
|
|
|
234
|
|
|
return json_decode($response->getBody()->getContents(), true); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Sets Guzzle client. |
239
|
|
|
* |
240
|
|
|
* @param Client $client |
241
|
|
|
*/ |
242
|
|
|
public function setClient(Client $client): void |
243
|
|
|
{ |
244
|
|
|
$this->client = $client; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Sets base auth username. |
249
|
|
|
* |
250
|
|
|
* @return string|null |
251
|
|
|
*/ |
252
|
|
|
public function getUsername(): ?string |
253
|
|
|
{ |
254
|
|
|
return $this->username; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Sets base auth password. |
259
|
|
|
* |
260
|
|
|
* @return string|null |
261
|
|
|
*/ |
262
|
|
|
public function getPassword(): ?string |
263
|
|
|
{ |
264
|
|
|
return $this->password; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|