Passed
Push — master ( ede639...ef74f3 )
by Dev
02:20
created

Response::getContent()   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
    private $exec;
0 ignored issues
show
introduced by
The private property $exec is not used, and could be removed.
Loading history...
8
9
    private $url;
10
11
    /** @var string * */
12
    private $header;
13
    /** @var string * */
14
    private $content;
15
    /** @var array * */
16
    private $info;
17
18 5
    public static function get($handle, string $url, int $returnHeader, bool $gzip)
0 ignored issues
show
Unused Code introduced by
The parameter $gzip is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

18
    public static function get($handle, string $url, int $returnHeader, /** @scrutinizer ignore-unused */ bool $gzip)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
    {
20 5
        $content = curl_exec($handle);
21
22 5
        if (!$content) {
23 3
            return curl_errno($handle);
24
        }
25
26 2
        $self = new self();
27
28 2
        if (2 === $returnHeader) {
29
            $self->header = $content;
30
        } else {
31 2
            if ($returnHeader) { // Remove headers from response
32 2
                $self->header = substr($content, 0, $sHeader = curl_getinfo($handle, CURLINFO_HEADER_SIZE));
33 2
                $content = substr($content, $sHeader);
34
            }
35
36 2
            $self->content = $content;
37
        }
38
39 2
        $self->info = curl_getinfo($handle); // curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL)
40 2
        $self->url = $url;
41
42 2
        return $self;
43
    }
44
45 2
    private function __construct()
46
    {
47 2
    }
48
49 2
    public function getContent()
50
    {
51 2
        return $this->content;
52
    }
53
54
    /**
55
     * Return header's data return by the request.
56
     *
57
     * @param bool $returnArray True to get an array, false to get a string
58
     *
59
     * @return array|string|null containing header's data
60
     */
61
    public function getHeader(bool $returnArray = true)
0 ignored issues
show
Unused Code introduced by
The parameter $returnArray is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

61
    public function getHeader(/** @scrutinizer ignore-unused */ bool $returnArray = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        if (isset($this->header)) {
64
            return true === $arrayFormatted ? Helper::httpParseHeaders($this->header) : $this->header;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $arrayFormatted seems to be never defined.
Loading history...
65
        }
66
    }
67
68
    /**
69
     * @return string requested url
70
     */
71 1
    public function getUrl()
72
    {
73 1
        return $this->url;
74
    }
75
76
    /**
77
     * Return current effective url.
78
     *
79
     * @return string
80
     */
81 2
    public function getEffectiveUrl()
82
    {
83 2
        return isset($this->info['url']) ? $this->info['url'] : null; //curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL);
84
    }
85
86
    /**
87
     * Return the cookie(s) returned by the request (if there are).
88
     *
89
     * @return null|array containing the cookies
90
     */
91
    public function getCookies()
92
    {
93
        if (isset($this->header)) {
94
            $header = $this->getHeader();
95
            if (isset($header['Set-Cookie'])) {
96
                return is_array($header['Set-Cookie']) ? implode('; ', $header['Set-Cookie']) : $header['Set-Cookie'];
97
            }
98
        }
99
    }
100
101
    /**
102
     * Get information regarding the request.
103
     *
104
     * @return array an associative array with the following elements (which correspond to opt), or FALSE on failure
105
     */
106
    public function getInfo()
107
    {
108
        return $this->info;
109
    }
110
111 1
    public function getStatusCode()
112
    {
113 1
        return $this->info['http_code'];
114
    }
115
116
    public function getContentType()
117
    {
118
        return $this->info['content_type'];
119
    }
120
}
121