Passed
Push — master ( 936624...2142e5 )
by Orkhan
01:43
created

Sipgate::initiateCall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
c 0
b 0
f 0
rs 10
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 SipgateInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $username;
16
    /**
17
     * @var string
18
     */
19
    private $password;
20
    /**
21
     * @var Client
22
     */
23
    private $client;
24
25
    public function __construct(string $username, string $password)
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
    public function account(): array
34
    {
35
        return $this->sendRequest('account');
36
    }
37
38
    public function users(): array
39
    {
40
        $response = $this->sendRequest('users');
41
42
        $users = [];
43
        foreach ($response['items'] as $user) {
44
            array_push($users, new User($user));
45
        }
46
47
        return $users;
48
    }
49
50
    public function devices(User $user): array
51
    {
52
        $response = $this->sendRequest($user->id.'/devices');
53
54
        $devices = [];
55
        foreach ($response['items'] as $device) {
56
            array_push($devices, new Device($user, $device));
57
        }
58
59
        return $devices;
60
    }
61
62
    public function initiateCall(Device $device, $callee, $callerId = null)
63
    {
64
        $response = $this->sendRequest('sessions/calls', 'POST', [
65
            'json' => [
66
                'deviceId' => $device->id,
67
                'caller' => $device->user->id,
68
                'callerId' => $callerId,
69
                'callee' => $callee,
70
            ]
71
        ]);
72
73
        return $response;
74
    }
75
76
    public function history(array $options = [])
77
    {
78
        $response = $this->sendRequest('history', 'GET', ['query' => $options]);
79
80
        $history = [];
81
        foreach ($response['items'] as $item) {
82
            array_push($history, new History($item));
83
        }
84
85
        return $history;
86
    }
87
88
    /**
89
     * @param string $url
90
     * @param string $method
91
     * @param array $options
92
     * @return array
93
     * @throws \GuzzleHttp\Exception\GuzzleException
94
     */
95
    private function sendRequest(string $url, string $method = 'GET', array $options = []): array
96
    {
97
        $response = $this->client->request($method, $url, array_merge(
98
            [
99
                'headers' => ['Accept' => 'application/json'],
100
                'auth' => [$this->username, $this->password]
101
            ],
102
            $options
103
        ));
104
105
        return json_decode($response->getBody()->getContents(), true);
106
    }
107
108
    /**
109
     * @param Client $client
110
     */
111
    public function setClient(Client $client): void
112
    {
113
        $this->client = $client;
114
    }
115
}
116