Curl::getWithSignature()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
c 2
b 0
f 0
dl 0
loc 29
rs 9.6666
cc 2
nc 1
nop 2
1
<?php
2
namespace exussum12\TripAdvisor\Clients;
3
4
use exussum12\TripAdvisor\Client;
5
use exussum12\TripAdvisor\Response;
6
7
class Curl implements Client
8
{
9
    private $curlHandle;
10
    public function __construct($curl = null)
11
    {
12
        if (is_null($curl)) {
13
            $curl = curl_init();
14
        }
15
        $this->curlHandle = $curl;
16
        curl_setopt(
17
            $this->curlHandle,
18
            CURLOPT_USERAGENT,
19
            'Exussum Tripadvisor Client'
20
        );
21
    }
22
23
    public function getWithSignature($url, $signature)
24
    {
25
        curl_setopt($this->curlHandle, CURLOPT_URL, $url);
26
        curl_setopt(
27
            $this->curlHandle,
28
            CURLOPT_HTTPHEADER,
29
            [
30
                'Authorization: ' . $signature,
31
            ]
32
        );
33
        curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
34
        $headers = [];
35
        curl_setopt(
36
            $this->curlHandle,
37
            CURLOPT_HEADERFUNCTION,
38
            function ($curl, $header) use (&$headers) {
39
                if (strpos($header, ':') !== false) {
40
                    list($key, $value) = explode(':', $header, 2);
41
                    $headers[$key] = trim($value);
42
                }
43
44
                return strlen($header);
45
            }
46
        );
47
48
        $body = curl_exec($this->curlHandle);
49
        $statusCode = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
50
51
        return new Response($statusCode, $headers, $body);
52
    }
53
}
54