Passed
Push — master ( 57ba64...9efab0 )
by Dev
02:29
created

Response   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 116
ccs 28
cts 40
cp 0.7
rs 10
c 0
b 0
f 0
wmc 19

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getInfo() 0 3 1
A __construct() 0 2 1
A getStatusCode() 0 3 1
A getContent() 0 3 1
A get() 0 25 4
A getEffectiveUrl() 0 3 2
A getUrl() 0 3 1
A getCookies() 0 9 4
A getContentType() 0 3 1
A getHeaders() 0 4 3
1
<?php
2
3
namespace PiedWeb\Curl;
4
5
class Response
6
{
7
    private $url;
8
9
    /** @var string * */
10
    private $headers;
11
    /** @var string * */
12
    private $content;
13
    /** @var array * */
14
    private $info;
15
16 17
    public static function get($handle, string $url, int $returnHeaders)
17
    {
18 17
        $content = curl_exec($handle);
19
20 17
        if (!$content) {
21 13
            return curl_errno($handle);
22
        }
23
24 10
        $self = new self();
25
26 10
        if (2 === $returnHeaders) {
27
            $self->headers = $content;
28
        } else {
29 10
            if ($returnHeaders) { // Remove headerss from response
30 10
                $self->headers = substr($content, 0, $sHeaders = curl_getinfo($handle, CURLINFO_HEADER_SIZE));
31 10
                $content = substr($content, $sHeaders);
32
            }
33
34 10
            $self->content = $content;
35
        }
36
37 10
        $self->info = curl_getinfo($handle); // curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL)
38 10
        $self->url = $url;
39
40 10
        return $self;
41
    }
42
43 10
    private function __construct()
44
    {
45 10
    }
46
47 6
    public function getContent()
48
    {
49 6
        return $this->content;
50
    }
51
52
    /**
53
     * Return headers's data return by the request.
54
     *
55
     * @param bool $returnArray True to get an array, false to get a string
56
     *
57
     * @return array|string|null containing headers's data
58
     */
59 4
    public function getHeaders(bool $returnArray = true)
60
    {
61 4
        if (isset($this->headers)) {
62 4
            return true === $returnArray ? Helper::httpParseHeaders($this->headers) : $this->headers;
63
        }
64
    }
65
66
    /**
67
     * @return string requested url
68
     */
69 2
    public function getUrl()
70
    {
71 2
        return $this->url;
72
    }
73
74
    /**
75
     * Return current effective url.
76
     *
77
     * @return string
78
     */
79 10
    public function getEffectiveUrl()
80
    {
81 10
        return isset($this->info['url']) ? $this->info['url'] : null; //curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL);
82
    }
83
84
    /**
85
     * Return the cookie(s) returned by the request (if there are).
86
     *
87
     * @return null|array containing the cookies
88
     */
89
    public function getCookies()
90
    {
91
        if (isset($this->headers)) {
92
            $headers = $this->getHeaders();
93
            if (isset($headers['Set-Cookie'])) {
94
                if (is_array($headers['Set-Cookie'])) {
95
                    return implode('; ', $headers['Set-Cookie']);
96
                } else {
97
                    return $headers['Set-Cookie'];
98
                }
99
            }
100
        }
101
    }
102
103
    /**
104
     * Get information regarding the request.
105
     *
106
     * @return array an associative array with the following elements (which correspond to opt), or FALSE on failure
107
     */
108
    public function getInfo()
109
    {
110
        return $this->info;
111
    }
112
113 8
    public function getStatusCode()
114
    {
115 8
        return $this->info['http_code'];
116
    }
117
118 4
    public function getContentType()
119
    {
120 4
        return $this->info['content_type'];
121
    }
122
}
123