Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
14 / 14
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractCurl
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
14 / 14
23
100.00% covered (success)
100.00%
1 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
0
 setResponse
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getResponse
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasResponse
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOption
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 setOptions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOption
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeOption
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 hasOption
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 curl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getErrorNumber
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getErrorMessage
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 version
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Http\Client\Handler;
15
16/**
17 * HTTP client abstract curl class
18 *
19 * @category   Pop
20 * @package    Pop\Http
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    5.2.0
25 */
26abstract class AbstractCurl extends AbstractHandler
27{
28
29    /**
30     * Curl options
31     * @var array
32     */
33    protected array $options = [];
34
35    /**
36     * Curl response
37     * @var mixed
38     */
39    protected mixed $response = null;
40
41    /**
42     * Constructor
43     *
44     * Instantiate the Curl handler object
45     *
46     * @param  ?array $options
47     * @throws Exception
48     */
49    abstract public function __construct(?array $options = null);
50
51    /**
52     * Get Curl response
53     *
54     * @param  mixed $response
55     * @return AbstractCurl
56     */
57    public function setResponse(mixed $response): AbstractCurl
58    {
59        $this->response = $response;
60        return $this;
61    }
62
63    /**
64     * Get Curl response
65     *
66     * @return mixed
67     */
68    public function getResponse(): mixed
69    {
70        return $this->response;
71    }
72
73    /**
74     * Has a Curl response
75     *
76     * @return bool
77     */
78    public function hasResponse(): bool
79    {
80        return ($this->response !== null);
81    }
82
83    /**
84     * Set Curl option
85     *
86     * @param  int   $opt
87     * @param  mixed $val
88     * @return AbstractCurl
89     */
90    public function setOption(int $opt, mixed $val): AbstractCurl
91    {
92        // Set the protected property to keep track of the Curl options.
93        $this->options[$opt] = $val;
94
95        if ($this->hasOption(CURLOPT_HTTP_VERSION)) {
96            switch ($this->getOption(CURLOPT_HTTP_VERSION)) {
97                case 1:
98                    $this->httpVersion = '1.0';
99                    break;
100                case 2:
101                    $this->httpVersion = '1.1';
102                    break;
103                case 3:
104                    $this->httpVersion = '2.0';
105                    break;
106            }
107        }
108
109        return $this;
110    }
111
112    /**
113     * Set Curl options
114     *
115     * @param  array $options
116     * @return AbstractCurl
117     */
118    public function setOptions(array $options): AbstractCurl
119    {
120        // Set the protected property to keep track of the Curl options.
121        foreach ($options as $k => $v) {
122            $this->setOption($k, $v);
123        }
124
125        return $this;
126    }
127
128    /**
129     * Get Curl options
130     *
131     * @return array
132     */
133    public function getOptions(): array
134    {
135        return $this->options;
136    }
137
138    /**
139     * Get a Curl option
140     *
141     * @param  int $opt
142     * @return mixed
143     */
144    public function getOption(int $opt): mixed
145    {
146        return $this->options[$opt] ?? null;
147    }
148
149    /**
150     * Remove Curl option
151     *
152     * @param  int $opt
153     * @return AbstractCurl
154     */
155    public function removeOption(int $opt): AbstractCurl
156    {
157        if (isset($this->options[$opt])) {
158            if (str_contains(get_class($this), 'Multi')) {
159                curl_multi_setopt($this->resource, $opt, null);
160            } else {
161                curl_setopt($this->resource, $opt, null);
162            }
163
164            unset($this->options[$opt]);
165        }
166        return $this;
167    }
168
169    /**
170     * Has a Curl option
171     *
172     * @param  int $opt
173     * @return bool
174     */
175    public function hasOption(int $opt): bool
176    {
177        return (isset($this->options[$opt]));
178    }
179
180    /**
181     * Has Curl options
182     *
183     * @return bool
184     */
185    public function hasOptions(): bool
186    {
187        return !empty($this->options);
188    }
189
190    /**
191     * Return curl resource (alias to $this->getResource())
192     *
193     * @return mixed
194     */
195    public function curl(): mixed
196    {
197        return $this->resource;
198    }
199
200    /**
201     * Get Curl error number
202     *
203     * @return int
204     */
205    public function getErrorNumber(): int
206    {
207        if (str_contains(get_class($this), 'Multi')) {
208            return curl_multi_errno($this->resource);
209        } else {
210            return curl_errno($this->resource);
211        }
212
213    }
214
215    /**
216     * Get Curl error number
217     *
218     * @return string
219     */
220    public function getErrorMessage(): string
221    {
222        if (str_contains(get_class($this), 'Multi')) {
223            return curl_multi_strerror(curl_multi_errno($this->resource));
224        } else {
225            return curl_error($this->resource);
226        }
227    }
228
229    /**
230     * Return the Curl version
231     *
232     * @return array
233     */
234    public function version(): array
235    {
236        return curl_version();
237    }
238
239}