1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace drupol\Yaroc\Plugin; |
6
|
|
|
|
7
|
|
|
use drupol\Yaroc\Http\Client; |
8
|
|
|
use Exception; |
9
|
|
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
10
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
11
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
12
|
|
|
|
13
|
|
|
abstract class AbstractProvider implements ProviderInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The endpoint. |
17
|
|
|
*/ |
18
|
|
|
private ?string $endpoint; |
19
|
|
|
|
20
|
|
|
private HttpClientInterface $httpClient; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The parameters. |
24
|
|
|
*/ |
25
|
|
|
private array $parameters; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The random.org resource. |
29
|
|
|
*/ |
30
|
|
|
private ?string $resource; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
?HttpClientInterface $client = null, |
34
|
|
|
?string $endpoint = null, |
35
|
|
|
?string $resource = null, |
36
|
|
|
array $parameters = [] |
37
|
5 |
|
) { |
38
|
|
|
$this->httpClient = $client ?? new Client(); |
39
|
|
|
$this->endpoint = $endpoint; |
40
|
5 |
|
$this->resource = $resource; |
41
|
5 |
|
$this->parameters = $parameters; |
42
|
5 |
|
} |
43
|
5 |
|
|
44
|
|
|
public function getEndPoint(): ?string |
45
|
|
|
{ |
46
|
|
|
return $this->endpoint; |
47
|
5 |
|
} |
48
|
5 |
|
|
49
|
5 |
|
public function getHttpClient(): HttpClientInterface |
50
|
5 |
|
{ |
51
|
5 |
|
return $this->httpClient; |
52
|
5 |
|
} |
53
|
5 |
|
|
54
|
|
|
public function getParameters(): array |
55
|
|
|
{ |
56
|
|
|
return $this->parameters; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getResource(): ?string |
60
|
|
|
{ |
61
|
|
|
return $this->resource; |
62
|
|
|
} |
63
|
5 |
|
|
64
|
|
|
public function request(): ResponseInterface |
65
|
|
|
{ |
66
|
|
|
$options = [ |
67
|
|
|
'json' => [ |
68
|
|
|
'jsonrpc' => '2.0', |
69
|
5 |
|
'id' => uniqid($this->getResource() . '_', true), |
70
|
|
|
'params' => $this->getParameters(), |
71
|
5 |
|
'method' => $this->getResource(), |
72
|
5 |
|
], |
73
|
|
|
]; |
74
|
5 |
|
|
75
|
|
|
if (null === $this->getEndPoint()) { |
76
|
|
|
throw new Exception('You must set an endpoint.'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
try { |
80
|
10 |
|
$response = $this->getHttpClient()->request('POST', $this->getEndPoint(), $options); |
81
|
|
|
} catch (TransportExceptionInterface $exception) { |
82
|
10 |
|
throw $exception; |
83
|
10 |
|
} |
84
|
|
|
|
85
|
10 |
|
return $response; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function withEndPoint(string $endpoint): ProviderInterface |
89
|
|
|
{ |
90
|
|
|
$clone = clone $this; |
91
|
10 |
|
$clone->endpoint = $endpoint; |
92
|
|
|
|
93
|
10 |
|
return $clone; |
94
|
10 |
|
} |
95
|
|
|
|
96
|
10 |
|
public function withHttpClient(HttpClientInterface $httpClient): ProviderInterface |
97
|
|
|
{ |
98
|
|
|
$clone = clone $this; |
99
|
|
|
$clone->httpClient = $httpClient; |
100
|
|
|
|
101
|
|
|
return $clone; |
102
|
5 |
|
} |
103
|
|
|
|
104
|
5 |
|
public function withParameters(array $parameters): ProviderInterface |
105
|
|
|
{ |
106
|
|
|
$clone = clone $this; |
107
|
|
|
$clone->parameters = $parameters; |
108
|
|
|
|
109
|
|
|
return $clone; |
110
|
5 |
|
} |
111
|
|
|
|
112
|
5 |
|
public function withResource(string $resource): ProviderInterface |
113
|
|
|
{ |
114
|
|
|
$clone = clone $this; |
115
|
|
|
$clone->resource = $resource; |
116
|
|
|
|
117
|
|
|
return $clone; |
118
|
5 |
|
} |
119
|
|
|
} |
120
|
|
|
|