|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\Sipgate; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Orkhanahmadov\Sipgate\Resources\Device; |
|
7
|
|
|
use Orkhanahmadov\Sipgate\Resources\History; |
|
8
|
|
|
use Orkhanahmadov\Sipgate\Resources\User; |
|
9
|
|
|
|
|
10
|
|
|
class Sipgate implements SIPInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string|null |
|
14
|
|
|
*/ |
|
15
|
|
|
private $username = null; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string|null |
|
18
|
|
|
*/ |
|
19
|
|
|
private $password = null; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Client |
|
22
|
|
|
*/ |
|
23
|
|
|
private $client; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Sipgate constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string|null $username |
|
29
|
|
|
* @param string|null $password |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(?string $username = null, ?string $password = null) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->username = $username; |
|
34
|
|
|
$this->password = $password; |
|
35
|
|
|
|
|
36
|
|
|
$this->client = new Client(['base_uri' => 'https://api.sipgate.com/v2/']); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $username |
|
41
|
|
|
* @param string $password |
|
42
|
|
|
* |
|
43
|
|
|
* @return Sipgate |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setBasicAuthCredentials(string $username, string $password) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->username = $username; |
|
48
|
|
|
$this->password = $password; |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public function account(): array |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->sendRequest('account'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
public function users(): array |
|
69
|
|
|
{ |
|
70
|
|
|
$response = $this->sendRequest('users'); |
|
71
|
|
|
|
|
72
|
|
|
$users = []; |
|
73
|
|
|
foreach ($response['items'] as $user) { |
|
74
|
|
|
array_push($users, new User($user)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $users; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param User|string $user |
|
82
|
|
|
* |
|
83
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
public function devices($user): array |
|
88
|
|
|
{ |
|
89
|
|
|
$userId = $user instanceof User ? $user->id : $user; |
|
90
|
|
|
|
|
91
|
|
|
$response = $this->sendRequest($userId.'/devices'); |
|
92
|
|
|
|
|
93
|
|
|
$devices = []; |
|
94
|
|
|
foreach ($response['items'] as $device) { |
|
95
|
|
|
array_push($devices, new Device($user, $device)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $devices; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param Device $device |
|
103
|
|
|
* @param $callee |
|
104
|
|
|
* @param null $callerId |
|
|
|
|
|
|
105
|
|
|
* |
|
106
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
107
|
|
|
* |
|
108
|
|
|
* @return array|mixed |
|
109
|
|
|
*/ |
|
110
|
|
|
public function initiateCall(Device $device, $callee, $callerId = null) |
|
111
|
|
|
{ |
|
112
|
|
|
$response = $this->sendRequest('sessions/calls', 'POST', [ |
|
113
|
|
|
'json' => [ |
|
114
|
|
|
'deviceId' => $device->id, |
|
115
|
|
|
'caller' => $device->userId(), |
|
116
|
|
|
'callerId' => $callerId, |
|
117
|
|
|
'callee' => $callee, |
|
118
|
|
|
], |
|
119
|
|
|
]); |
|
120
|
|
|
|
|
121
|
|
|
return $response; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param array $options |
|
126
|
|
|
* |
|
127
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
|
public function history(array $options = []): array |
|
132
|
|
|
{ |
|
133
|
|
|
$response = $this->sendRequest('history', 'GET', ['query' => $options]); |
|
134
|
|
|
|
|
135
|
|
|
$history = []; |
|
136
|
|
|
foreach ($response['items'] as $item) { |
|
137
|
|
|
array_push($history, new History($item)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $history; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $url |
|
145
|
|
|
* @param string $method |
|
146
|
|
|
* @param array $options |
|
147
|
|
|
* |
|
148
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
149
|
|
|
* |
|
150
|
|
|
* @return array |
|
151
|
|
|
*/ |
|
152
|
|
|
private function sendRequest(string $url, string $method = 'GET', array $options = []): array |
|
153
|
|
|
{ |
|
154
|
|
|
$response = $this->client->request($method, $url, array_merge( |
|
155
|
|
|
[ |
|
156
|
|
|
'headers' => ['Accept' => 'application/json'], |
|
157
|
|
|
'auth' => [$this->username, $this->password], |
|
158
|
|
|
], |
|
159
|
|
|
$options |
|
160
|
|
|
)); |
|
161
|
|
|
|
|
162
|
|
|
return json_decode($response->getBody()->getContents(), true); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param Client $client |
|
167
|
|
|
*/ |
|
168
|
|
|
public function setClient(Client $client): void |
|
169
|
|
|
{ |
|
170
|
|
|
$this->client = $client; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @return string|null |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getUsername(): ?string |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->username; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @return string|null |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getPassword(): ?string |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->password; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|