-
Notifications
You must be signed in to change notification settings - Fork 0
/
CurlConfigAwareHandle.php
59 lines (51 loc) · 1.58 KB
/
CurlConfigAwareHandle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace MicroDeps\Curl;
use CurlHandle;
use MicroDeps\Curl\Interface\CurlConfigAwareHandleInterface;
use Safe\Exceptions\CurlException as SafeCurlException;
use ValueError;
/**
* This is a very simple wrapper that allows us to keep track of config for a specific CurlHandle.
*/
final class CurlConfigAwareHandle implements CurlConfigAwareHandleInterface
{
private CurlHandle $handle;
/**
* @throws SafeCurlException
* @throws CurlException
*/
public function __construct(public readonly string $url, private CurlOptionCollection $options)
{
$handle = \Safe\curl_init($this->url);
$this->handle = $handle;
$this->applyOptions();
}
public function getHandle(): CurlHandle
{
return $this->handle;
}
public function getOptions(): CurlOptionCollection
{
return $this->options;
}
/**
* @throws CurlException
*/
private function applyOptions(): void
{
try {
curl_setopt_array($this->handle, $this->options->get());
} catch (ValueError $valueError) {
$valid = array_flip(get_defined_constants(true)['curl']);
$invalid = array_diff_key($this->options->get(), $valid);
throw CurlException::withFormatAndPrevious(
CurlException::MSG_INVALID_OPTIONS,
$valueError,
print_r($invalid, true),
/* @phpstan-ignore-next-line confused by curl_version return type */
curl_version()['version']
);
}
}
}