Passed
Push — master ( 697ce1...609429 )
by D
02:42
created

Response::getMimeType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 33
    public static function get(Request $request)
18
    {
19 33
        $handle = $request->getHandle();
20
21 33
        $content = curl_exec($handle);
22
23 33
        if (!$content) {
24 18
            return curl_errno($handle);
25
        }
26
27 24
        $self = new self($request);
28
29 24
        if (Request::RETURN_HEADER_ONLY === $request->mustReturnHeaders()) {
30 3
            $self->headers = $content;
31
        } else {
32 21
            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 21
            $self->content = $content;
38
        }
39
40 24
        $self->info = curl_getinfo($handle); // curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL)
41
42 24
        return $self;
43
    }
44
45 24
    private function __construct(Request $request)
46
    {
47 24
        $this->request = $request;
48 24
    }
49
50 6
    public function getRequest()
51
    {
52 6
        return $this->request;
53
    }
54
55 18
    public function getContent()
56
    {
57 18
        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::parseHeader($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 24
    public function getEffectiveUrl()
88
    {
89 24
        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 3
    public function getMimeType()
134
    {
135 3
        $headers = Helper::parseHeader($this->getContentType());
136
137 3
        return $headers[0][0] ?? null;
138
    }
139
}
140