Completed
Push — master ( f18251...40c839 )
by Flo
15s
created

Curl::getResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Faulancer\Http\Adapter;
4
5
use Faulancer\Exception\ClientException;
6
use Faulancer\Http\Request;
7
8
/**
9
 * Class Curl
10
 * @package Faulancer\Http\Adapter
11
 * @author  Florian Knapp <[email protected]>
12
 */
13
class Curl extends AbstractAdapter
14
{
15
    /**
16
     * @param Request $request
17
     * @return void
18
     */
19
    public function send(Request $request)
20
    {
21
        $ch = curl_init($request->getUri());
22
23
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
24
25
        if (!empty($request->getHeaders())) {
26
            curl_setopt($ch, CURLOPT_HTTPHEADER, $request->getHeaders());
27
        }
28
29
        if ($request->getMethod() === 'POST' && !empty($request->getBody())) {
30
            curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getBody());
31
        }
32
33
        $this->response = curl_exec($ch);
34
        curl_close($ch);
35
    }
36
37
    /**
38
     * @return mixed
39
     * @throws ClientException
40
     */
41
    public function getResponse()
42
    {
43
        if (empty($this->response)) {
44
            throw new ClientException('You have to call the \'send\' method first.');
45
        }
46
47
        return $this->response;
48
    }
49
50
}