Response::getEffectiveUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PiedWeb\Curl;
4
5
class Response
6
{
7
    /** @var Request */
8
    protected $request;
9
10
    /** @var string */
11
    protected $headers;
12
    /** @var string * */
13
    protected $content;
14
    /** @var array * */
15
    protected $info;
16
17 36
    public static function get(Request $request)
18
    {
19 36
        $handle = $request->getHandle();
20
21 36
        $content = curl_exec($handle);
22
23 36
        if (!$content) {
24 18
            return curl_errno($handle);
25
        }
26
27 27
        $self = new self($request);
28
29 27
        if (Request::RETURN_HEADER_ONLY === $request->mustReturnHeaders()) {
30 3
            $self->headers = $content;
31
        } else {
32 24
            if (Request::RETURN_HEADER === $request->mustReturnHeaders()) { // Remove headers from response
33 18
                $self->headers = substr($content, 0, $sHeaders = curl_getinfo($handle, CURLINFO_HEADER_SIZE));
34 18
                $content = substr($content, $sHeaders);
35
            }
36
37 24
            $self->content = $content;
38
        }
39
40 27
        $self->info = curl_getinfo($handle); // curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL)
41
42 27
        return $self;
43
    }
44
45 27
    private function __construct(Request $request)
46
    {
47 27
        $this->request = $request;
48 27
    }
49
50 6
    public function getRequest()
51
    {
52 6
        return $this->request;
53
    }
54
55 21
    public function getContent()
56
    {
57 21
        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 15
    public function getHeaders(bool $returnArray = true)
68
    {
69 15
        if (isset($this->headers)) {
70 15
            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 27
    public function getEffectiveUrl()
88
    {
89 27
        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 6
    public function getInfo(?string $key = null)
119
    {
120 6
        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 6
    public function getMimeType()
134
    {
135 6
        $headers = Helper::parseHeader($this->getContentType());
136
137 6
        return $headers[0][0] ?? null;
138
    }
139
}
140