Passed
Push — master ( 2d7a7d...fde0fe )
by Orkhan
01:25
created

Sipgate::setClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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