AbstractAdapter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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 31
    final public function __construct(string $subdomain, string $token)
42
    {
43 31
        $this->setSubdomain($subdomain);
44 31
        $this->setToken($token);
45 31
    }
46
47 24
    final public function request(string $method, string $uri, array $options = [])
48
    {
49
        try {
50 24
            $this->send($method, $uri, $options);
51 23
            if ($method == 'DELETE') {
52 2
                return ($this->getLastResponse()->getStatusCode() == 200);
53
            }
54 21
            if ($body = (string) $this->getLastResponse()->getBody()) {
55 19
                return $body;
56
            }
57 2
            return false;
58 1
        } catch (BadResponseException $ex) {
59 1
            throw new RequestException($ex->getMessage(), $ex->getCode());
60
        }
61
    }
62
63 19
    final public function getLastRequest(): RequestInterface
64
    {
65 19
        return $this->lastRequest;
66
    }
67
68 23
    final public function getLastResponse(): ResponseInterface
69
    {
70 23
        return $this->lastResponse;
71
    }
72
73 30
    final public function setClient($client)
74
    {
75 30
        $this->client = $client;
76 30
    }
77
78
    /**
79
     * Sends and registers the request and registers the response.
80
     *
81
     * @param string $method  The HTTP method.
82
     * @param string $uri  The request URI.
83
     * @param array $options  The request options.
84
     */
85
    abstract protected function send(string $method, string $uri, array $options = []);
86
}
87