Passed
Push — master ( 534b0c...8bd354 )
by Matt
05:44
created

Client::setHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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 $url string
39
     * @param Array $options
40
     * @return \GuzzleHttp\Message\ResponseInterface 
41
     * @throws \GuzzleHttp\Exception\RequestException
42
     * @throws \Exception
43
     */
44 View Code Duplication
    public function get($url = null, Array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $this->getHttpClient();
47
        try {
48
            $response = $this->httpClient->get($url, $options);
49
            return $response;
50
        } catch (RequestException $e) {
51
            throw $e;
52
        } catch (Exception $e) {
53
            throw $e;
54
        }
55
    }
56
57
    /**
58
     * Wrapper for the GuzzleHttp\Client::post() method
59
     *
60
     * @param $url string
61
     * @param Array $options
62
     * @return \GuzzleHttp\Message\ResponseInterface 
63
     * @throws \GuzzleHttp\Exception\RequestException
64
     * @throws \Exception
65
     */
66 View Code Duplication
    public function post($url = null, Array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        $this->getHttpClient();
69
        try {
70
            $response = $this->httpClient->post($url, $options);
71
            return $response;
72
        } catch (RequestException $e) {
73
            throw $e;
74
        } catch (Exception $e) {
75
            throw $e;
76
        }
77
    }
78
79
    /**
80
     * Method to send a request object via HTTP and retrieve a response
81
     *
82
     * @param \GuzzleHttp\Message\RequestInterface $request
83
     * @return \GuzzleHttp\Message\ResponseInterface
84
     * @throws \GuzzleHttp\Exception\RequestException
85
     * @throws \Exception
86
     */
87
    public function send(RequestInterface $request)
88
    {
89
        $this->getHttpClient();
90
        try {
91
            $response = $this->httpClient->send($request);
92
            return $response;
93
        } catch (RequestException $e) {
94
            throw $e;
95
        } catch (Exception $e) {
96
            throw $e;
97
        }
98
    }
99
100
    /**
101
     * Method to set the HttpClient
102
     *
103
     * @param \GuzzleHttp\Client
104
     */
105
    public function setHttpClient(HttpClient $client)
106
    {
107
        $this->httpClient = $client;
108
    }
109
110
    /**
111
     * Method to retrieve or instantiate an Http Client
112
     *
113
     * @return \GuzzleHttp\Client
114
     */
115
    public function getHttpClient()
116
    {
117
        if (!isset($this->httpClient)) {
118
            $this->httpClient = new HttpClient(['base_url' => self::BASEURL]);
119
        }
120
        return $this->httpClient;
121
    }
122
} 
123