Passed
Push — master ( 91fe13...12bba4 )
by Shahrad
10:04
created

HttpResponse::getCurlInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EasyHttp\Model;
4
5
use CurlHandle;
6
use EasyHttp\Enums\CurlInfo;
7
8
/**
9
 * Class HttpResponse
10
 *
11
 * @link    https://github.com/shahradelahi/easy-http
12
 * @author  Shahrad Elahi (https://github.com/shahradelahi)
13
 * @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License)
14
 *
15
 * @property int $status                The response code
16
 * @property string $body               The body of the response
17
 * @property array $headers             The headers of the response
18
 * @property int $headerSize            The size of the headers
19
 * @property CurlHandle $curlHandle     The curl handle
20
 *
21
 * @method int        getStatusCode()       This method returns the status code of the response.
22
 * @method string     getBody()             This method returns the body of the response.
23
 * @method int        getHeaderSize()       This method returns the header size of the response.
24
 * @method array      getHeaders()          This method returns the headers of the response.
25
 * @method string     getErrorMessage()     This method returns the error message of the response.
26
 * @method int        getErrorCode()        This method returns the error code of the response.
27
 * @method CurlHandle getCurlHandle()       This method returns the curl handle of the response.
28
 *
29
 * @method $this setBody($body)             This method sets the body of the response.
30
 * @method $this setHeaderSize($size)       This method sets the header size of the response.
31
 * @method $this setStatusCode($code)       This method sets the status code of the response.
32
 * @method $this setErrorMessage($error)    This method sets the error of the response.
33
 * @method $this setErrorCode($code)        This method sets the error code of the response.
34
 */
35
class HttpResponse
36
{
37
38
    /**
39
     * Set the curl handle
40
     *
41
     * @param \CurlHandle $curlHandle
42
     * @return HttpResponse
43
     */
44
    public function setCurlHandle(\CurlHandle $curlHandle): HttpResponse
45
    {
46
        $this->curlHandle = $curlHandle;
47
        return $this;
48
    }
49
50
    /**
51
     * Get info from the curl handle
52
     *
53
     * @return CurlInfo|false
54
     */
55
    public function getInfoFromCurl(): CurlInfo|false
56
    {
57
        if (empty($this->getCurlHandle())) return false;
58
        return new CurlInfo(curl_getinfo($this->curlHandle));
59
    }
60
61
    /**
62
     * Set the headers of the response
63
     *
64
     * @param string $headers
65
     * @return HttpResponse
66
     */
67
    public function setHeaders(string $headers): HttpResponse
68
    {
69
        $result = [];
70
        $lines = explode("\r\n", $headers);
71
        foreach ($lines as $line) {
72
            if (str_contains($line, ':')) {
73
                $parts = explode(':', $line);
74
                $result[trim($parts[0])] = trim($parts[1]);
75
            }
76
        }
77
        $this->headers = $result;
78
        return $this;
79
    }
80
81
    /**
82
     * Get a key from the response headers
83
     *
84
     * @param string $key
85
     * @return mixed
86
     */
87
    public function getHeaderLine(string $key): mixed
88
    {
89
        return array_change_key_case($this->headers, CASE_LOWER)[strtolower($key)] ?? null;
90
    }
91
92
    /**
93
     * @param string $name
94
     * @param array $arguments
95
     * @return mixed
96
     */
97
    public function __call(string $name, array $arguments): mixed
98
    {
99
        if (property_exists($this, $name)) {
100
            return $this->{$name};
101
        }
102
103
        if (method_exists($this, $name)) {
104
            return $this->{$name}();
105
        }
106
107
        if (str_starts_with($name, 'get')) {
108
            $property = lcfirst(substr($name, 3));
109
            return $this->{$property} ?? null;
110
        }
111
112
        if (str_starts_with($name, 'set')) {
113
            $property = lcfirst(substr($name, 3));
114
            $this->{$property} = $arguments[0] ?? null;
115
            return $this;
116
        }
117
118
        throw new \BadMethodCallException("Method $name does not exist");
119
    }
120
121
}