Completed
Push — master ( 039d98...40b98b )
by Matt
02:12
created

Client   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 34.62 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 12
c 4
b 0
f 0
lcom 1
cbo 2
dl 36
loc 104
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 12 12 3
A post() 12 12 3
A send() 12 12 3
A setHttpClient() 0 4 1
A getHttpClient() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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