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