Completed
Branch master (ae6db9)
by Matt
01:40
created

Client   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 102
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 10 3
A post() 0 10 3
A getHttpClient() 0 6 2
A setHttpClient() 0 3 1
A send() 0 10 3
1
<?php
2
namespace Billow;
3
use Exception;
4
use GuzzleHttp\Client as HttpClient;
5
use GuzzleHttp\Exception\RequestException;
6
use Psr\Http\Message\RequestInterface;
7
8
/**
9
 * @author Matt Frost<[email protected]>
10
 * @package Billow 
11
 * @license http://opensource.org/licenses/MIT MIT
12
 * @method get extending the GuzzleHttp\Client::get method
13
 * @method post extending the GuzzleHttp\Client::post method
14
 * @method send extending the GuzzleHttp\Client::send method
15
 * @method getHttpClient return or create an HTTP Client to send requests
16
 * @method setHttpClient set the HTTP Client with an instance of ClientInterface
17
 * @const BASEURL baseurl for the DO API
18
 */
19
class Client implements ClientInterface
20
{
21
    /**
22
     * Constant to represent the base string of the API calls
23
     *
24
     * @const string BASEURL
25
     */
26
    const BASEURL = 'https://api.digitalocean.com/v2/';
27
28
    /**
29
     * An HTTP Client to perform the HTTP Requests
30
     *
31
     * @var \GuzzleHttp\Client
32
     */
33
    private $httpClient;
34
    
35
    /**
36
     * Wrapper for the GuzzleHttp\Client::get() method
37
     *
38
     * @param string $url
39
     * @param Array $options
40
     * @return \Psr\Http\Message\ResponseInterface 
41
     * @throws \GuzzleHttp\Exception\RequestException
42
     * @throws \Exception
43
     */
44 3
    public function get($url = null, Array $options = [])
45
    {
46 3
        $this->getHttpClient();
47
        try {
48 3
            $response = $this->httpClient->get($url, $options);
49 1
            return $response;
50 2
        } catch (RequestException $e) {
51 1
            throw $e;
52 1
        } catch (Exception $e) {
53 1
            throw $e;
54
        }
55
    }
56
57
    /**
58
     * Wrapper for the GuzzleHttp\Client::post() method
59
     *
60
     * @param string $url 
61
     * @param Array $options
62
     * @return \Psr\Http\Message\ResponseInterface 
63
     * @throws \GuzzleHttp\Exception\RequestException
64
     * @throws \Exception
65
     */
66 3
    public function post($url = null, Array $options = [])
67
    {
68 3
        $this->getHttpClient();
69
        try {
70 3
            $response = $this->httpClient->post($url, $options);
71 1
            return $response;
72 2
        } catch (RequestException $e) {
73 1
            throw $e;
74 1
        } catch (Exception $e) {
75 1
            throw $e;
76
        }
77
    }
78
79
    /**
80
     * Method to send a request object via HTTP and retrieve a response
81
     *
82
     * @param \Psr\Http\Message\RequestInterface $request
83
     * @return \Psr\Http\Message\ResponseInterface
84
     * @throws \GuzzleHttp\Exception\RequestException
85
     * @throws \Exception
86
     */
87 3
    public function send(RequestInterface $request)
88
    {
89 3
        $this->getHttpClient();
90
        try {
91 3
            $response = $this->httpClient->send($request);
92 1
            return $response;
93 2
        } catch (RequestException $e) {
94 1
            throw $e;
95 1
        } catch (Exception $e) {
96 1
            throw $e;
97
        }
98
    }
99
100
    /**
101
     * Method to set the HttpClient
102
     *
103
     * @param \GuzzleHttp\Client $client
104
     */
105 10
    public function setHttpClient(HttpClient $client)
106
    {
107 10
        $this->httpClient = $client;
108 10
    }
109
110
    /**
111
     * Method to retrieve or instantiate an Http Client
112
     *
113
     * @return \GuzzleHttp\Client
114
     */
115 11
    public function getHttpClient()
116
    {
117 11
        if (!isset($this->httpClient)) {
118 1
            $this->httpClient = new HttpClient(['base_url' => self::BASEURL]);
119
        }
120 11
        return $this->httpClient;
121
    }
122
} 
123