|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Buzz\Client; |
|
6
|
|
|
|
|
7
|
|
|
use Buzz\Configuration\ParameterBag; |
|
8
|
|
|
use Buzz\Exception\InvalidArgumentException; |
|
9
|
|
|
use Http\Message\ResponseFactory; |
|
10
|
|
|
use Interop\Http\Factory\ResponseFactoryInterface; |
|
11
|
|
|
use Nyholm\Psr7\Factory\MessageFactory; |
|
12
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
13
|
|
|
|
|
14
|
|
|
abstract class AbstractClient |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var OptionsResolver |
|
18
|
|
|
*/ |
|
19
|
|
|
private $optionsResolver; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var ParameterBag |
|
23
|
|
|
*/ |
|
24
|
|
|
private $options; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ResponseFactoryInterface|ResponseFactory |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $responseFactory; |
|
30
|
|
|
|
|
31
|
215 |
|
public function __construct(array $options = [], $responseFactory = null) |
|
32
|
|
|
{ |
|
33
|
215 |
|
$this->options = new ParameterBag(); |
|
34
|
215 |
|
$this->options = $this->doValidateOptions($options); |
|
35
|
212 |
|
if (null === $responseFactory) { |
|
36
|
212 |
|
$responseFactory = new MessageFactory(); |
|
37
|
|
|
} |
|
38
|
212 |
|
if (!$responseFactory instanceof ResponseFactoryInterface && !$responseFactory instanceof ResponseFactory) { |
|
39
|
|
|
throw new InvalidArgumentException('$responseFactory not a valid ResponseFactory'); |
|
40
|
|
|
} |
|
41
|
212 |
|
$this->responseFactory = $responseFactory; |
|
42
|
212 |
|
} |
|
43
|
|
|
|
|
44
|
223 |
|
protected function getOptionsResolver(): OptionsResolver |
|
45
|
|
|
{ |
|
46
|
223 |
|
if (null !== $this->optionsResolver) { |
|
47
|
21 |
|
return $this->optionsResolver; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
215 |
|
$this->optionsResolver = new OptionsResolver(); |
|
51
|
215 |
|
$this->configureOptions($this->optionsResolver); |
|
52
|
|
|
|
|
53
|
215 |
|
return $this->optionsResolver; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Validate a set of options and return a new and shiny ParameterBag. |
|
58
|
|
|
*/ |
|
59
|
238 |
|
protected function validateOptions(array $options = []): ParameterBag |
|
60
|
|
|
{ |
|
61
|
238 |
|
if (empty($options)) { |
|
62
|
217 |
|
return $this->options; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
21 |
|
return $this->doValidateOptions($options); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Validate a set of options and return a new and shiny ParameterBag. |
|
70
|
|
|
*/ |
|
71
|
223 |
|
private function doValidateOptions(array $options = []): ParameterBag |
|
72
|
|
|
{ |
|
73
|
223 |
|
$parameterBag = $this->options->add($options); |
|
74
|
|
|
|
|
75
|
|
|
try { |
|
76
|
223 |
|
$parameters = $this->getOptionsResolver()->resolve($parameterBag->all()); |
|
77
|
6 |
|
} catch (\Throwable $e) { |
|
78
|
|
|
// Wrap any errors. |
|
79
|
6 |
|
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
220 |
|
return new ParameterBag($parameters); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
215 |
|
protected function configureOptions(OptionsResolver $resolver): void |
|
86
|
|
|
{ |
|
87
|
215 |
|
$resolver->setDefaults([ |
|
88
|
215 |
|
'allow_redirects' => false, |
|
89
|
|
|
'max_redirects' => 5, |
|
90
|
|
|
'timeout' => 30, |
|
91
|
|
|
'verify' => true, |
|
92
|
|
|
'proxy' => null, |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
215 |
|
$resolver->setAllowedTypes('allow_redirects', 'boolean'); |
|
96
|
215 |
|
$resolver->setAllowedTypes('verify', 'boolean'); |
|
97
|
215 |
|
$resolver->setAllowedTypes('max_redirects', 'integer'); |
|
98
|
215 |
|
$resolver->setAllowedTypes('timeout', ['integer', 'float']); |
|
99
|
215 |
|
$resolver->setAllowedTypes('proxy', ['null', 'string']); |
|
100
|
215 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|