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