Passed
Push — develop ( abe9c0...a4cee6 )
by Alessandro
36s queued 10s
created

Adapter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 96
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getClient() 0 3 1
A __construct() 0 15 3
A request() 0 10 3
A getBaseUri() 0 3 1
A setBaseUri() 0 3 1
A post() 0 4 1
A setClient() 0 3 1
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 6
    public function __construct(Auth $auth, $base_uri = null, $client = null)
35
    {
36 6
        if ($base_uri === null) {
37 3
            $base_uri = 'https://api-ssl.bitly.com/v4';
38
        }
39 6
        $this->setBaseUri($base_uri);
40
41 6
        $headers = $auth->getHeaders();
42 6
        if ($client === null) {
43 4
            $client = new Client([
44 4
                'base_uri' => $this->getBaseUri(),
45 4
                'headers' => $headers
46
            ]);
47
        }
48 6
        $this->setClient($client);
49 6
    }
50
51
    /**
52
     * @param string $uri
53
     * @param array $data
54
     * @param array $headers
55
     * @return \Psr\Http\Message\ResponseInterface
56
     */
57 1
    public function post(string $uri, array $data = [], array $headers = []): ResponseInterface
58
    {
59 1
        $response = $this->request('post', $uri, $data, $headers);
60 1
        return $response;
61
    }
62
63
    /**
64
     * @param string $method
65
     * @param string $uri
66
     * @param array $data
67
     * @param array $headers
68
     * @return mixed
69
     */
70 3
    public function request(string $method, string $uri, array $data = [], array $headers = [])
71
    {
72 3
        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 2
        $response = $this->getClient()->$method($uri, [
76 2
            'headers' => $headers,
77 2
            ($method === 'get' ? 'query' : 'json') => $data,
78
        ]);
79 2
        return $response;
80
    }
81
82
    /**
83
     * @return object
84
     */
85 2
    private function getClient()
86
    {
87 2
        return $this->client;
88
    }
89
90
    /**
91
     * @param $client
92
     */
93 6
    private function setClient($client)
94
    {
95 6
        $this->client = $client;
96 6
    }
97
98
    /**
99
     * @return string
100
     */
101 4
    public function getBaseUri()
102
    {
103 4
        return $this->base_uri;
104
    }
105
106
    /**
107
     * @param $base_uri
108
     */
109 6
    private function setBaseUri($base_uri)
110
    {
111 6
        $this->base_uri = $base_uri;
112 6
    }
113
}
114