SendsRequest::sendRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 6
c 3
b 0
f 0
dl 0
loc 13
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Orkhanahmadov\Sipgate\Traits;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\GuzzleException;
7
8
trait SendsRequest
9
{
10
    /**
11
     * @var string|null
12
     */
13
    private $username = null;
14
    /**
15
     * @var string|null
16
     */
17
    private $password = null;
18
    /**
19
     * @var Client
20
     */
21
    private $client;
22
23
    /**
24
     * Sets Guzzle client.
25
     *
26
     * @param Client $client
27
     */
28
    public function setClient(Client $client): void
29
    {
30
        $this->client = $client;
31
    }
32
33
    /**
34
     * Sends API requests to sipgate endpoint.
35
     *
36
     * @param string $url
37
     * @param string $method
38
     * @param array $options
39
     *
40
     * @return array|null
41
     * @throws GuzzleException
42
     */
43
    private function sendRequest(string $url, string $method = 'GET', array $options = []): ?array
44
    {
45
        $response = $this->client->request($method, $url, array_merge(
46
            [
47
                'headers' => [
48
                    'Accept' => 'application/json',
49
                ],
50
                'auth' => [$this->username, $this->password],
51
            ],
52
            $options
53
        ));
54
55
        return json_decode($response->getBody()->getContents(), true);
56
    }
57
}
58