Completed
Push — master ( 724c5f...8cd687 )
by Orkhan
01:30
created

Sipgate::history()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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