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