Completed
Push — master ( 9cb038...08e890 )
by Dev
02:40
created

Response::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 18
    public static function get(Request $request)
18
    {
19 18
        $handle = $request->getHandle();
20 18
        $url = $request->getUrl();
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
21 18
        $returnHeaders = $request->getReturnHeader();
22
23 18
        $content = curl_exec($handle);
24
25 18
        if (!$content) {
26 12
            return curl_errno($handle);
27
        }
28
29 15
        $self = new self($request);
30
31 15
        if (Request::RETURN_HEADER_ONLY === $returnHeaders) {
32
            $self->headers = $content;
33
        } else {
34 15
            if (Request::RETURN_HEADER === $returnHeaders) { // Remove headers from response
35 15
                $self->headers = substr($content, 0, $sHeaders = curl_getinfo($handle, CURLINFO_HEADER_SIZE));
36 15
                $content = substr($content, $sHeaders);
37
            }
38
39 15
            $self->content = $content;
40
        }
41
42 15
        $self->info = curl_getinfo($handle); // curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL)
43
44 15
        return $self;
45
    }
46
47 15
    private function __construct(Request $request)
48
    {
49 15
        $this->request = $request;
50 15
    }
51
52 3
    public function getRequest()
53
    {
54 3
        return $this->request;
55
    }
56
57 9
    public function getContent()
58
    {
59 9
        return $this->content;
60
    }
61
62
    /**
63
     * Return headers's data return by the request.
64
     *
65
     * @param bool $returnArray True to get an array, false to get a string
66
     *
67
     * @return array|string|null containing headers's data
68
     */
69 6
    public function getHeaders(bool $returnArray = true)
70
    {
71 6
        if (isset($this->headers)) {
72 6
            return true === $returnArray ? Helper::httpParseHeaders($this->headers) : $this->headers;
73
        }
74
    }
75
76
    /**
77
     * @return string requested url
78
     */
79 3
    public function getUrl()
80
    {
81 3
        return $this->request->getUrl();
82
    }
83
84
    /**
85
     * Return current effective url.
86
     *
87
     * @return string
88
     */
89 15
    public function getEffectiveUrl()
90
    {
91 15
        return isset($this->info['url']) ? $this->info['url'] : null; //curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL);
92
    }
93
94
    /**
95
     * Return the cookie(s) returned by the request (if there are).
96
     *
97
     * @return null|array containing the cookies
98
     */
99
    public function getCookies()
100
    {
101
        if (isset($this->headers)) {
102
            $headers = $this->getHeaders();
103
            if (isset($headers['Set-Cookie'])) {
104
                if (is_array($headers['Set-Cookie'])) {
105
                    return implode('; ', $headers['Set-Cookie']);
106
                } else {
107
                    return $headers['Set-Cookie'];
108
                }
109
            }
110
        }
111
    }
112
113
    /**
114
     * Get information regarding the request.
115
     *
116
     * @param string $key to get
117
     *
118
     * @return string|array
119
     */
120
    public function getInfo(?string $key = null)
121
    {
122
        return $key ? (isset($this->info[$key]) ? $this->info[$key] : null) : $this->info;
123
    }
124
125 12
    public function getStatusCode()
126
    {
127 12
        return $this->info['http_code'];
128
    }
129
130 6
    public function getContentType()
131
    {
132 6
        return $this->info['content_type'];
133
    }
134
}
135