|
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 Psr\Http\Message\ResponseFactoryInterface; |
|
11
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AbstractClient |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var OptionsResolver|null |
|
17
|
|
|
*/ |
|
18
|
|
|
private $optionsResolver; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var ParameterBag |
|
22
|
|
|
*/ |
|
23
|
|
|
private $options; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ResponseFactoryInterface|ResponseFactory |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $responseFactory; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ResponseFactoryInterface|ResponseFactory $responseFactory |
|
32
|
|
|
*/ |
|
33
|
207 |
|
public function __construct($responseFactory, array $options = []) |
|
34
|
|
|
{ |
|
35
|
207 |
|
if (!$responseFactory instanceof ResponseFactoryInterface && !$responseFactory instanceof ResponseFactory) { |
|
|
|
|
|
|
36
|
|
|
throw new InvalidArgumentException(sprintf('First argument of %s must be an instance of %s or %s.', __CLASS__, ResponseFactoryInterface::class, ResponseFactory::class)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
207 |
|
$this->options = new ParameterBag(); |
|
40
|
207 |
|
$this->options = $this->doValidateOptions($options); |
|
41
|
204 |
|
$this->responseFactory = $responseFactory; |
|
42
|
204 |
|
} |
|
43
|
|
|
|
|
44
|
224 |
|
protected function getOptionsResolver(): OptionsResolver |
|
45
|
|
|
{ |
|
46
|
224 |
|
if (null !== $this->optionsResolver) { |
|
47
|
32 |
|
return $this->optionsResolver; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
207 |
|
$this->optionsResolver = new OptionsResolver(); |
|
51
|
207 |
|
$this->configureOptions($this->optionsResolver); |
|
52
|
|
|
|
|
53
|
207 |
|
return $this->optionsResolver; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Validate a set of options and return a new and shiny ParameterBag. |
|
58
|
|
|
*/ |
|
59
|
249 |
|
protected function validateOptions(array $options = []): ParameterBag |
|
60
|
|
|
{ |
|
61
|
249 |
|
if (empty($options)) { |
|
62
|
217 |
|
return $this->options; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
32 |
|
return $this->doValidateOptions($options); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Validate a set of options and return a new and shiny ParameterBag. |
|
70
|
|
|
*/ |
|
71
|
224 |
|
private function doValidateOptions(array $options = []): ParameterBag |
|
72
|
|
|
{ |
|
73
|
224 |
|
$parameterBag = $this->options->add($options); |
|
74
|
|
|
|
|
75
|
|
|
try { |
|
76
|
224 |
|
$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
|
221 |
|
return new ParameterBag($parameters); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
207 |
|
protected function configureOptions(OptionsResolver $resolver): void |
|
86
|
|
|
{ |
|
87
|
207 |
|
$resolver->setDefaults([ |
|
88
|
207 |
|
'allow_redirects' => false, |
|
89
|
|
|
'max_redirects' => 5, |
|
90
|
|
|
'timeout' => 0, |
|
91
|
|
|
'verify' => true, |
|
92
|
|
|
'proxy' => null, |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
207 |
|
$resolver->setAllowedTypes('allow_redirects', 'boolean'); |
|
96
|
207 |
|
$resolver->setAllowedTypes('verify', 'boolean'); |
|
97
|
207 |
|
$resolver->setAllowedTypes('max_redirects', 'integer'); |
|
98
|
207 |
|
$resolver->setAllowedTypes('timeout', ['integer', 'float']); |
|
99
|
207 |
|
$resolver->setAllowedTypes('proxy', ['null', 'string']); |
|
100
|
207 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|