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(string $uri, array $data = [], array $headers = []): ResponseInterface |
59
|
|
|
{ |
60
|
2 |
|
$response = $this->request('post', $uri, $data, $headers); |
61
|
2 |
|
return $response; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $method |
66
|
|
|
* @param string $uri |
67
|
|
|
* @param array $data |
68
|
|
|
* @param array $headers |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
4 |
|
public function request(string $method, string $uri, array $data = [], array $headers = []) |
72
|
|
|
{ |
73
|
4 |
|
if (!in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) { |
74
|
1 |
|
throw new \InvalidArgumentException('Request method must be get, post, put, patch, or delete'); |
75
|
|
|
} |
76
|
3 |
|
$response = $this->getClient()->$method($uri, [ |
77
|
3 |
|
'headers' => $headers, |
78
|
3 |
|
($method === 'get' ? 'query' : 'json') => $data, |
79
|
|
|
]); |
80
|
3 |
|
return $response; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return object |
85
|
|
|
*/ |
86
|
3 |
|
private function getClient() |
87
|
|
|
{ |
88
|
3 |
|
return $this->client; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $client |
93
|
|
|
*/ |
94
|
8 |
|
private function setClient($client) |
95
|
|
|
{ |
96
|
8 |
|
$this->client = $client; |
97
|
8 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
6 |
|
public function getBaseUri() |
103
|
|
|
{ |
104
|
6 |
|
return $this->base_uri; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $base_uri |
109
|
|
|
*/ |
110
|
8 |
|
private function setBaseUri($base_uri) |
111
|
|
|
{ |
112
|
8 |
|
$this->base_uri = $base_uri; |
113
|
8 |
|
} |
114
|
|
|
} |
115
|
|
|
|