1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Guillermoandrae\Highrise\Http; |
4
|
|
|
|
5
|
|
|
use Guillermoandrae\Highrise\Helpers\Xml; |
6
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
abstract class AbstractAdapter implements AdapterInterface |
11
|
|
|
{ |
12
|
|
|
use CredentialsAwareTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The HTTP client registered with this object. |
16
|
|
|
* |
17
|
|
|
* @var mixed |
18
|
|
|
*/ |
19
|
|
|
protected $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The last request. |
23
|
|
|
* |
24
|
|
|
* @var RequestInterface |
25
|
|
|
*/ |
26
|
|
|
protected $lastRequest; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The last response. |
30
|
|
|
* |
31
|
|
|
* @var ResponseInterface |
32
|
|
|
*/ |
33
|
|
|
protected $lastResponse; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Client constructor. |
37
|
|
|
* |
38
|
|
|
* @param string $subdomain The account subdomain. |
39
|
|
|
* @param string $token The authentication token. |
40
|
|
|
*/ |
41
|
|
|
final public function __construct(string $subdomain, string $token) |
42
|
|
|
{ |
43
|
|
|
$this->setSubdomain($subdomain); |
44
|
|
|
$this->setToken($token); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
final public function request(string $method, string $uri, array $options = []) |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
|
|
$this->send($method, $uri, $options); |
51
|
|
|
if ($body = (string) $this->getLastResponse()->getBody()) { |
52
|
|
|
return Xml::parse($body); |
53
|
|
|
} |
54
|
|
|
return ($this->getLastResponse()->getStatusCode() == 201); |
55
|
|
|
} catch (BadResponseException $ex) { |
56
|
|
|
throw new RequestException($ex->getMessage(), $ex->getCode()); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
final public function getLastRequest(): RequestInterface |
61
|
|
|
{ |
62
|
|
|
return $this->lastRequest; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
final public function getLastResponse(): ResponseInterface |
66
|
|
|
{ |
67
|
|
|
return $this->lastResponse; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
final public function setClient($client) |
71
|
|
|
{ |
72
|
|
|
$this->client = $client; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Sends and registers the request and registers the response. |
77
|
|
|
* |
78
|
|
|
* @param string $method The HTTP method. |
79
|
|
|
* @param string $uri The request URI. |
80
|
|
|
* @param array $options The request options. |
81
|
|
|
*/ |
82
|
|
|
abstract protected function send(string $method, string $uri, array $options = []); |
83
|
|
|
} |
84
|
|
|
|