Completed
Push — master ( 06d9f7...a7a165 )
by ARCANEDEV
9s
created

Client   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 170
ccs 37
cts 37
cp 1
rs 10
wmc 11
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 4 1
A getBaseUrl() 0 4 1
A setBaseUrl() 0 6 1
A setOptionArray() 0 6 1
A get() 0 15 1
A init() 0 4 1
A execute() 0 6 1
A close() 0 6 2
A prepareUrl() 0 6 2
1
<?php namespace Arcanedev\Currencies\Http;
2
3
use Arcanedev\Currencies\Contracts\Http\Client as ClientInterface;
4
5
/**
6
 * Class     Client
7
 *
8
 * @package  Arcanedev\Currencies\Http
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class Client implements ClientInterface
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Base URL.
19
     *
20
     * @var string
21
     */
22
    protected $baseUrl;
23
24
    /**
25
     * CURL object.
26
     *
27
     * @var resource
28
     */
29
    protected $curl;
30
31
    /**
32
     * The response result.
33
     *
34
     * @var mixed
35
     */
36
    protected $response;
37
38
    /**
39
     * The error code.
40
     *
41
     * @var int
42
     */
43
    protected $errorCode;
44
45
    /**
46
     * The error message.
47
     *
48
     * @var string
49
     */
50
    protected $errorMessage;
51
52
    /* ------------------------------------------------------------------------------------------------
53
     |  Constructor & Destructor
54
     | ------------------------------------------------------------------------------------------------
55
     */
56
    /**
57
     * Destroy the instance.
58
     */
59 36
    public function __destruct()
60
    {
61 36
        $this->close();
62 36
    }
63
64
    /* ------------------------------------------------------------------------------------------------
65
     |  Getters & Setters
66
     | ------------------------------------------------------------------------------------------------
67
     */
68
    /**
69
     * Get base URL.
70
     *
71
     * @return string
72
     */
73 24
    public function getBaseUrl()
74
    {
75 24
        return rtrim($this->baseUrl, '/');
76
    }
77
78
    /**
79
     * Set base URL.
80
     *
81
     * @param  string $baseUrl
82
     *
83
     * @return self
84
     */
85 24
    public function setBaseUrl($baseUrl)
86
    {
87 24
        $this->baseUrl = $baseUrl;
88
89 24
        return $this;
90
    }
91
92
    /**
93
     * Set array options.
94
     *
95
     * @param  array  $options
96
     *
97
     * @return self
98
     */
99 12
    public function setOptionArray(array $options)
100
    {
101 12
        curl_setopt_array($this->curl, $options);
102
103 12
        return $this;
104
    }
105
106
    /* ------------------------------------------------------------------------------------------------
107
     |  Main Functions
108
     | ------------------------------------------------------------------------------------------------
109
     */
110
    /**
111
     * Make a get request.
112
     *
113
     * @param  string  $uri
114
     * @param  array   $params
115
     *
116
     * @return string
117
     */
118 12
    public function get($uri, array $params = [])
119
    {
120 12
        $this->init();
121
122 12
        $this->setOptionArray([
123 12
            CURLOPT_URL            => $this->prepareUrl($uri, $params),
124 12
            CURLOPT_RETURNTRANSFER => 1,
125 12
            CURLOPT_CONNECTTIMEOUT => 10,
126 9
        ]);
127
128 12
        $this->execute();
129 12
        $this->close();
130
131 12
        return $this->response;
132
    }
133
134
    /**
135
     * Init curl.
136
     */
137 12
    private function init()
138
    {
139 12
        $this->curl = curl_init();
140 12
    }
141
142
    /**
143
     * Execute curl request.
144
     */
145 12
    private function execute()
146
    {
147 12
        $this->response     = curl_exec($this->curl);
148 12
        $this->errorCode    = curl_errno($this->curl);
149 12
        $this->errorMessage = curl_error($this->curl);
150 12
    }
151
152
    /**
153
     * Close the CURL object.
154
     */
155 36
    private function close()
156
    {
157 36
        if (is_resource($this->curl)) {
158 12
            curl_close($this->curl);
159 9
        }
160 36
    }
161
162
    /* ------------------------------------------------------------------------------------------------
163
     |  Other Functions
164
     | ------------------------------------------------------------------------------------------------
165
     */
166
    /**
167
     * Prepare the URL.
168
     *
169
     * @param  string  $uri
170
     * @param  array   $params
171
     *
172
     * @return string
173
     */
174 12
    private function prepareUrl($uri, array $params)
175
    {
176 12
        $query = empty($params) ? '' : '?' . http_build_query(array_filter($params));
177
178 12
        return $this->getBaseUrl() . '/' . ltrim($uri, '/') . $query;
179
    }
180
}
181