CurlInteractor::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace Slacky\Http;
2
3
use Slacky\Contracts\Http\Interactor;
4
use Slacky\Contracts\Http\Response;
5
use Slacky\Contracts\Http\ResponseFactory;
6
7
class CurlInteractor implements Interactor
8
{
9
10
    /**
11
     * The response factory to use.
12
     *
13
     * @var \Slacky\Contracts\Http\ResponseFactory
14
     */
15
    protected $factory;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function get(string $url, array $parameters = [], array $headers = []) : Response
21
    {
22
        return $this->executeRequest(static::prepareRequest($url, $parameters, $headers));
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function post(string $url, array $postParameters = [], array $headers = []) : Response
29
    {
30
        $request = static::prepareRequest($url, [], $headers);
31
32
        curl_setopt($request, CURLOPT_POST, count($postParameters));
33
        curl_setopt($request, CURLOPT_POSTFIELDS, http_build_query($postParameters));
34
35
        return $this->executeRequest($request);
36
    }
37
38
    /**
39
     * Prepares a request using curl.
40
     *
41
     * @param  string $url [description]
42
     * @param  array $parameters [description]
43
     * @param  array $headers [description]
44
     * @return resource
45
     */
46
    protected static function prepareRequest(string $url, array $parameters = [], array $headers = [])
47
    {
48
        $request = curl_init();
49
50
        if (!empty($parameters) && $query = http_build_query($parameters)) {
51
            $url .= '?' . $query;
52
        }
53
54
        curl_setopt($request, CURLOPT_URL, $url);
55
        curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
56
        curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
57
        curl_setopt($request, CURLINFO_HEADER_OUT, true);
58
        curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false);
59
60
        return $request;
61
    }
62
63
    /**
64
     * Executes a curl request.
65
     *
66
     * @param  resource $request
67
     * @return Response
68
     */
69
    public function executeRequest($request) : Response
70
    {
71
        $body = curl_exec($request);
72
        $info = curl_getinfo($request);
73
74
        curl_close($request);
75
76
        $statusCode = $info['http_code'];
77
        $headers    = $info['request_header'];
78
79
        if (function_exists('http_parse_headers')) {
80
            $headers = http_parse_headers($headers);
81
        } else {
82
            $header_text = substr($headers, 0, strpos($headers, "\r\n\r\n"));
83
            $headers     = [];
84
85
            foreach (explode("\r\n", $header_text) as $i => $line) {
86
                if ($i === 0) {
87
                    continue;
88
                }
89
90
                [$key, $value] = explode(': ', $line);
91
                $headers[$key] = $value;
92
            }
93
        }
94
95
        return $this->factory->build($body, $headers, $statusCode);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function setResponseFactory(ResponseFactory $factory) : void
102
    {
103
        $this->factory = $factory;
104
    }
105
106
}
107