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 |
14
|
|
|
*/ |
15
|
|
|
private $username = null; |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $password = null; |
20
|
|
|
/** |
21
|
|
|
* @var Client |
22
|
|
|
*/ |
23
|
|
|
private $client; |
24
|
|
|
|
25
|
|
|
public function __construct(?string $username = null, ?string $password = null) |
26
|
|
|
{ |
27
|
|
|
$this->username = $username; |
28
|
|
|
$this->password = $password; |
29
|
|
|
|
30
|
|
|
$this->client = new Client(['base_uri' => 'https://api.sipgate.com/v2/']); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $username |
35
|
|
|
* @param string $password |
36
|
|
|
* @return Sipgate |
37
|
|
|
*/ |
38
|
|
|
public function setUserCredentials(string $username, string $password) |
39
|
|
|
{ |
40
|
|
|
$this->username = $username; |
41
|
|
|
$this->password = $password; |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function account(): array |
47
|
|
|
{ |
48
|
|
|
return $this->sendRequest('account'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function users(): array |
52
|
|
|
{ |
53
|
|
|
$response = $this->sendRequest('users'); |
54
|
|
|
|
55
|
|
|
$users = []; |
56
|
|
|
foreach ($response['items'] as $user) { |
57
|
|
|
array_push($users, new User($user)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $users; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param User|string $user |
65
|
|
|
* @return array |
66
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
67
|
|
|
*/ |
68
|
|
|
public function devices($user): array |
69
|
|
|
{ |
70
|
|
|
$userId = $user instanceof User ? $user->id : $user; |
71
|
|
|
|
72
|
|
|
$response = $this->sendRequest($userId.'/devices'); |
73
|
|
|
|
74
|
|
|
$devices = []; |
75
|
|
|
foreach ($response['items'] as $device) { |
76
|
|
|
array_push($devices, new Device($user, $device)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $devices; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function initiateCall(Device $device, $callee, $callerId = null) |
83
|
|
|
{ |
84
|
|
|
$response = $this->sendRequest('sessions/calls', 'POST', [ |
85
|
|
|
'json' => [ |
86
|
|
|
'deviceId' => $device->id, |
87
|
|
|
'caller' => $device->userId(), |
88
|
|
|
'callerId' => $callerId, |
89
|
|
|
'callee' => $callee, |
90
|
|
|
], |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
return $response; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function history(array $options = []): array |
97
|
|
|
{ |
98
|
|
|
$response = $this->sendRequest('history', 'GET', ['query' => $options]); |
99
|
|
|
|
100
|
|
|
$history = []; |
101
|
|
|
foreach ($response['items'] as $item) { |
102
|
|
|
array_push($history, new History($item)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $history; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $url |
110
|
|
|
* @param string $method |
111
|
|
|
* @param array $options |
112
|
|
|
* |
113
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
private function sendRequest(string $url, string $method = 'GET', array $options = []): array |
118
|
|
|
{ |
119
|
|
|
$response = $this->client->request($method, $url, array_merge( |
120
|
|
|
[ |
121
|
|
|
'headers' => ['Accept' => 'application/json'], |
122
|
|
|
'auth' => [$this->username, $this->password], |
123
|
|
|
], |
124
|
|
|
$options |
125
|
|
|
)); |
126
|
|
|
|
127
|
|
|
return json_decode($response->getBody()->getContents(), true); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param Client $client |
132
|
|
|
*/ |
133
|
|
|
public function setClient(Client $client): void |
134
|
|
|
{ |
135
|
|
|
$this->client = $client; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getUsername(): string |
142
|
|
|
{ |
143
|
|
|
return $this->username; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function getPassword(): string |
150
|
|
|
{ |
151
|
|
|
return $this->password; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|