Passed
Push — master ( e1ba1d...47c856 )
by Orkhan
01:32
created

Sipgate::recordCall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 Telephony
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');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->sendRequest('account') could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
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|string   $device
103
     * @param string|int      $callee
104
     * @param string|int|null $callerId
105
     *
106
     * @throws \GuzzleHttp\Exception\GuzzleException
107
     *
108
     * @return string
109
     */
110
    public function initiateCall($device, $callee, $callerId = null): string
111
    {
112
        $response = $this->sendRequest('sessions/calls', 'POST', [
113
            'json' => [
114
                'caller'   => $device instanceof Device ? $device->id : $device,
115
                'callee'   => $callee,
116
                'callerId' => $callerId,
117
            ],
118
        ]);
119
120
        return $response['sessionId'];
121
    }
122
123
    /**
124
     * @param string $callId
125
     * @param bool   $value
126
     * @param bool   $announcement
127
     *
128
     * @throws \GuzzleHttp\Exception\GuzzleException
129
     *
130
     * @return bool
131
     */
132
    public function recordCall(string $callId, bool $value, bool $announcement): bool
133
    {
134
        $this->sendRequest('calls/'.$callId.'/recording', 'PUT', [
135
            'json' => [
136
                'announcement' => $announcement,
137
                'value' => $value
138
            ]
139
        ]);
140
141
        return $value;
142
    }
143
144
    /**
145
     * @param array $options
146
     *
147
     * @throws \GuzzleHttp\Exception\GuzzleException
148
     *
149
     * @return array
150
     */
151
    public function history(array $options = []): array
152
    {
153
        $response = $this->sendRequest('history', 'GET', ['query' => $options]);
154
155
        $history = [];
156
        foreach ($response['items'] as $item) {
157
            array_push($history, new History($item));
158
        }
159
160
        return $history;
161
    }
162
163
    /**
164
     * @param string $url
165
     * @param string $method
166
     * @param array  $options
167
     *
168
     * @throws \GuzzleHttp\Exception\GuzzleException
169
     *
170
     * @return array|null
171
     */
172
    private function sendRequest(string $url, string $method = 'GET', array $options = []): ?array
173
    {
174
        $response = $this->client->request($method, $url, array_merge(
175
            [
176
                'headers' => ['Accept' => 'application/json'],
177
                'auth'    => [$this->username, $this->password],
178
            ],
179
            $options
180
        ));
181
182
        return json_decode($response->getBody()->getContents(), true);
183
    }
184
185
    /**
186
     * @param Client $client
187
     */
188
    public function setClient(Client $client): void
189
    {
190
        $this->client = $client;
191
    }
192
193
    /**
194
     * @return string|null
195
     */
196
    public function getUsername(): ?string
197
    {
198
        return $this->username;
199
    }
200
201
    /**
202
     * @return string|null
203
     */
204
    public function getPassword(): ?string
205
    {
206
        return $this->password;
207
    }
208
}
209