Adapter::request()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 4
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: aless
5
 * Date: 13/03/2019
6
 * Time: 12:39
7
 */
8
9
namespace Bitlywrap\Adapter;
10
11
use Bitlywrap\Auth\Auth;
12
use Bitlywrap\Interfaces\AdapterInterface;
13
use GuzzleHttp\Client;
14
use Psr\Http\Message\ResponseInterface;
15
16
class Adapter implements AdapterInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private $base_uri;
22
23
    /**
24
     * @var object
25
     */
26
    private $client;
27
28
    /**
29
     * Adapter constructor.
30
     * @param Auth $auth
31
     * @param string|null $base_uri
32
     * @param object|null $client
33
     */
34 8
    public function __construct(Auth $auth, $base_uri = null, $client = null)
35
    {
36 8
        if ($base_uri === null) {
37 5
            $base_uri = 'https://api-ssl.bitly.com/v4/';
38
        }
39 8
        $this->setBaseUri($base_uri);
40
41 8
        $headers = $auth->getHeaders();
42 8
        if ($client === null) {
43 6
            $client = new Client([
44 6
                'base_uri' => $this->getBaseUri(),
45 6
                'headers' => $headers,
46
                'verify' => false
47
            ]);
48
        }
49 8
        $this->setClient($client);
50 8
    }
51
52
    /**
53
     * @param string $uri
54
     * @param array $data
55
     * @param array $headers
56
     * @return \Psr\Http\Message\ResponseInterface
57
     */
58 2
    public function post($uri, $data = [], $headers = [])
59
    {
60 2
        return $this->request('post', $uri, $data, $headers);
61
    }
62
63
    /**
64
     * @param string $method
65
     * @param string $uri
66
     * @param array $data
67
     * @param array $headers
68
     * @return mixed
69
     */
70 4
    public function request($method, $uri, $data = [], $headers = [])
71
    {
72 4
        if (!in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
73 1
            throw new \InvalidArgumentException('Request method must be get, post, put, patch, or delete');
74
        }
75 3
        return $this->getClient()->$method($uri, [
76 3
            'headers' => $headers,
77 3
            ($method === 'get' ? 'query' : 'json') => $data,
78
        ]);
79
    }
80
81
    /**
82
     * @return object
83
     */
84 3
    private function getClient()
85
    {
86 3
        return $this->client;
87
    }
88
89
    /**
90
     * @param $client
91
     */
92 8
    private function setClient($client)
93
    {
94 8
        $this->client = $client;
95 8
    }
96
97
    /**
98
     * @return string
99
     */
100 6
    public function getBaseUri()
101
    {
102 6
        return $this->base_uri;
103
    }
104
105
    /**
106
     * @param $base_uri
107
     */
108 8
    private function setBaseUri($base_uri)
109
    {
110 8
        $this->base_uri = $base_uri;
111 8
    }
112
}
113