Passed
Push — master ( 5782b9...e0fa92 )
by Oss
10:02
created

CurlAdapter::getUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
1
<?php
2
/**
3
 * @category    Brownie/HttpClient
4
 * @author      Brownie <[email protected]>
5
 * @license     http://www.gnu.org/copyleft/lesser.html
6
 */
7
8
namespace Brownie\HttpClient\Client;
9
10
use Brownie\HttpClient\Request;
11
use Brownie\HttpClient\Response;
12
use Brownie\HttpClient\Client;
13
use Brownie\HttpClient\Exception\ClientException;
14
15
/**
16
 * API(Adapter) for using CURL functions in HTTP requests.
17
 */
18
class CurlAdapter implements Client
19
{
20
21
    /**
22
     * API CURL functions.
23
     *
24
     * @var CurlAdaptee
25
     */
26
    private $adaptee;
27
28
    /**
29
     * Sets incoming data.
30
     *
31
     * @param CurlAdaptee       $adaptee        API CURL functions.
32
     */
33 3
    public function __construct(CurlAdaptee $adaptee)
34
    {
35 3
        $this->setAdaptee($adaptee);
36 3
    }
37
38
    /**
39
     * Sets API CURL functions wrapper.
40
     * Returns the current object.
41
     *
42
     * @param CurlAdaptee $adaptee
43
     *
44
     * @return self
45
     */
46 3
    private function setAdaptee(CurlAdaptee $adaptee)
47
    {
48 3
        $this->adaptee = $adaptee;
49 3
        return $this;
50
    }
51
52
    /**
53
     * Returns API CURL functions wrapper.
54
     *
55
     * @return CurlAdaptee
56
     */
57 3
    private function getAdaptee()
58
    {
59 3
        return $this->adaptee;
60
    }
61
62
    /**
63
     * Performs a network request.
64
     * Returns the response.
65
     *
66
     * @param Request       $request    HTTP request params.
67
     *
68
     * @throws ClientException
69
     *
70
     * @return Response
71
     */
72 3
    public function httpRequest(Request $request)
73
    {
74 3
        $curl = $this->getCurlClient($request);
75
76
        /**
77
         * Executes a network resource request.
78
         */
79 3
        $responseBody = $this->getAdaptee()->exec($curl);
80
81 3
        $httpCode = $this->getAdaptee()->getinfo($curl, CURLINFO_HTTP_CODE);
82
83
        /**
84
         * Network error checking.
85
         */
86 3
        if ((0 != $this->getAdaptee()->errno($curl)) || !is_string($responseBody)) {
87 1
            throw new ClientException($this->getAdaptee()->error($curl));
88
        }
89
90
        /**
91
         * Gets the execution time of the request.
92
         */
93 2
        $runtime = $this->getAdaptee()->getinfo($curl, CURLINFO_TOTAL_TIME);
94
95 2
        $this->getAdaptee()->close($curl);
96
97 2
        $response = new Response();
98
        return $response
99 2
            ->setBody($responseBody)
100 2
            ->setHttpCode($httpCode)
101 2
            ->setRuntime($runtime);
102
    }
103
104
    /**
105
     * Creates and returns a CURL resource.
106
     *
107
     * @param Request       $request        HTTP request params.
108
     *
109
     * @return resource
110
     */
111 3
    private function getCurlClient(Request $request)
112
    {
113
114
        /**
115
         * Build URL.
116
         */
117 3
        $url = $this->getUrl($request);
118
119
        /**
120
         * Initializing CURL.
121
         */
122 3
        $curl = $this->getAdaptee()->init($url);
123
124
        /**
125
         * Sets the request body.
126
         */
127 3
        $body = $request->getBody();
128 3
        if (!empty($body)) {
129 3
            $this->getAdaptee()->setopt($curl, CURLOPT_POSTFIELDS, $body);
130
131
        }
132
133
        /**
134
         * CURL setting.
135
         */
136 3
        $this->setBaseOpt($curl, $request, $url);
137
138
        /**
139
         * Configuring HTTP headers.
140
         */
141 3
        $this->setHedaers($curl, $request, $body);
142
143 3
        return $curl;
144
    }
145
146
    /**
147
     * Generates and returns URL.
148
     *
149
     * @param Request   $request    HTTP request params.
150
     *
151
     * @return string
152
     */
153 3
    private function getUrl(Request $request)
154
    {
155 3
        $url = $request->getUrl();
156
157
        /**
158
         * Adds GET parameters.
159
         */
160 3
        $params = $request->getParams();
161 3
        if (!empty($params)) {
162 3
            $url .= '?' . http_build_query($params);
163
        }
164
165 3
        return $url;
166
    }
167
168
    /**
169
     * Sets the basic options.
170
     *
171
     * @param resource  $curl       CURL resource.
172
     * @param Request   $request    HTTP request params.
173
     * @param string    $url        Request URL.
174
     */
175 3
    private function setBaseOpt($curl, Request $request, $url)
176
    {
177
        $this
178 3
            ->getAdaptee()
179 3
            ->setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getMethod())
180 3
            ->setopt($curl, CURLOPT_TIMEOUT, $request->getTimeOut())
181 3
            ->setopt($curl, CURLOPT_NOPROGRESS, true)
182 3
            ->setopt($curl, CURLOPT_RETURNTRANSFER, true)
183 3
            ->setopt($curl, CURLOPT_URL, $url);
184
185 3
        if ($request->isDisableSSLValidation()) {
186
            /**
187
             * Disable SSL validation.
188
             */
189
            $this
190 1
                ->getAdaptee()
191 1
                ->setopt($curl, CURLOPT_SSL_VERIFYPEER, false)
192 1
                ->setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
193
        }
194 3
    }
195
196
    /**
197
     * Sets HTTP headers.
198
     *
199
     * @param resource  $curl       CURL resource.
200
     * @param Request   $request    HTTP request params.
201
     * @param string    $body       The body of the request.
202
     */
203 3
    private function setHedaers($curl, Request $request, $body)
204
    {
205
        $headers = array(
206 3
            'Connection: close',
207 3
            'Accept-Ranges: bytes',
208 3
            'Content-Length: ' . strlen($body),
209 3
            'Accept: ' . $request->getBodyFormat(),
210 3
            'Content-Type: ' . $request->getBodyFormat() . '; charset=utf-8',
211 3
            'User-Agent: ' . $this->getAdaptee()->getAgentString(),
212
        );
213 3
        foreach ($request->getHeaders() as $name => $value) {
214 3
            $headers[] = $name . ': ' . $value;
215
        }
216 3
        $this->getAdaptee()->setopt($curl, CURLOPT_HTTPHEADER, $headers);
217 3
    }
218
}
219