|
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
|
|
|
* @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
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
56
|
|
|
* |
|
57
|
|
|
* @return array|null |
|
58
|
|
|
*/ |
|
59
|
|
|
public function account(): ?array |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->sendRequest('account'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
public function users(): array |
|
70
|
|
|
{ |
|
71
|
|
|
$response = $this->sendRequest('users'); |
|
72
|
|
|
|
|
73
|
|
|
$users = []; |
|
74
|
|
|
foreach ($response['items'] as $user) { |
|
75
|
|
|
array_push($users, new User($user)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $users; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param User|string $user |
|
83
|
|
|
* |
|
84
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
public function devices($user): array |
|
89
|
|
|
{ |
|
90
|
|
|
$userId = $user instanceof User ? $user->id : $user; |
|
91
|
|
|
|
|
92
|
|
|
$response = $this->sendRequest($userId.'/devices'); |
|
93
|
|
|
|
|
94
|
|
|
$devices = []; |
|
95
|
|
|
foreach ($response['items'] as $device) { |
|
96
|
|
|
array_push($devices, new Device($user, $device)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $devices; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Returns currently established calls |
|
104
|
|
|
* |
|
105
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public function calls(): array |
|
110
|
|
|
{ |
|
111
|
|
|
$response = $this->sendRequest('calls'); |
|
112
|
|
|
|
|
113
|
|
|
$calls = []; |
|
114
|
|
|
foreach ($response['data'] as $call) { |
|
115
|
|
|
array_push($calls, new Call($call)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $calls; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param Device|string $device |
|
123
|
|
|
* @param string|int $callee |
|
124
|
|
|
* @param array $options |
|
125
|
|
|
* |
|
126
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
public function initiateCall($device, $callee, array $options = []): string |
|
131
|
|
|
{ |
|
132
|
|
|
$response = $this->sendRequest('sessions/calls', 'POST', [ |
|
133
|
|
|
'json' => [ |
|
134
|
|
|
'caller' => $device instanceof Device ? $device->id : $device, |
|
135
|
|
|
'callee' => $callee, |
|
136
|
|
|
'callerId' => isset($options['callerId']) ? $options['callerId'] : null, |
|
137
|
|
|
], |
|
138
|
|
|
]); |
|
139
|
|
|
|
|
140
|
|
|
if (isset($options['recording'], $options['recording']['value'], $options['recording']['announcement'])) { |
|
141
|
|
|
$this->recordCall( |
|
142
|
|
|
$response['sessionId'], |
|
143
|
|
|
$options['recording']['value'], |
|
144
|
|
|
$options['recording']['announcement'] |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $response['sessionId']; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $callId |
|
153
|
|
|
* @param bool $value |
|
154
|
|
|
* @param bool $announcement |
|
155
|
|
|
* |
|
156
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
157
|
|
|
* |
|
158
|
|
|
* @return bool |
|
159
|
|
|
*/ |
|
160
|
|
|
public function recordCall(string $callId, bool $value, bool $announcement): bool |
|
161
|
|
|
{ |
|
162
|
|
|
$this->sendRequest('calls/'.$callId.'/recording', 'PUT', [ |
|
163
|
|
|
'json' => [ |
|
164
|
|
|
'value' => $value, |
|
165
|
|
|
'announcement' => $announcement, |
|
166
|
|
|
], |
|
167
|
|
|
]); |
|
168
|
|
|
|
|
169
|
|
|
return $value; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param array $options |
|
174
|
|
|
* |
|
175
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
176
|
|
|
* |
|
177
|
|
|
* @return array |
|
178
|
|
|
*/ |
|
179
|
|
|
public function history(array $options = []): array |
|
180
|
|
|
{ |
|
181
|
|
|
$response = $this->sendRequest('history', 'GET', ['query' => $options]); |
|
182
|
|
|
|
|
183
|
|
|
$history = []; |
|
184
|
|
|
foreach ($response['items'] as $item) { |
|
185
|
|
|
array_push($history, new History($item)); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return $history; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param string $url |
|
193
|
|
|
* @param string $method |
|
194
|
|
|
* @param array $options |
|
195
|
|
|
* |
|
196
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
197
|
|
|
* |
|
198
|
|
|
* @return array|null |
|
199
|
|
|
*/ |
|
200
|
|
|
private function sendRequest(string $url, string $method = 'GET', array $options = []): ?array |
|
201
|
|
|
{ |
|
202
|
|
|
$response = $this->client->request($method, $url, array_merge( |
|
203
|
|
|
[ |
|
204
|
|
|
'headers' => ['Accept' => 'application/json'], |
|
205
|
|
|
'auth' => [$this->username, $this->password], |
|
206
|
|
|
], |
|
207
|
|
|
$options |
|
208
|
|
|
)); |
|
209
|
|
|
|
|
210
|
|
|
return json_decode($response->getBody()->getContents(), true); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param Client $client |
|
215
|
|
|
*/ |
|
216
|
|
|
public function setClient(Client $client): void |
|
217
|
|
|
{ |
|
218
|
|
|
$this->client = $client; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @return string|null |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getUsername(): ?string |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->username; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @return string|null |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getPassword(): ?string |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->password; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|