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

Curl   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 17 4
A getResponse() 0 8 2
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
}