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

SendsRequest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 10
dl 0
loc 47
rs 10
c 0
b 0
f 0

2 Methods

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