Completed
Push — master ( 81d88c...57f229 )
by Dev
02:36
created

Response   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 90.7%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 126
ccs 39
cts 43
cp 0.907
rs 10
c 0
b 0
f 0
wmc 22

11 Methods

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