-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSymfonyClientRequestMethod.php
More file actions
43 lines (34 loc) · 1.17 KB
/
SymfonyClientRequestMethod.php
File metadata and controls
43 lines (34 loc) · 1.17 KB
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
<?php
namespace Beelab\Recaptcha2Bundle\Recaptcha;
use ReCaptcha\ReCaptcha;
use ReCaptcha\RequestMethod;
use ReCaptcha\RequestParameters;
use Symfony\Contracts\HttpClient\HttpClientInterface;
final class SymfonyClientRequestMethod implements RequestMethod
{
private ?HttpClientInterface $client = null;
public function __construct(
private string $siteVerifyUrl = ReCaptcha::SITE_VERIFY_URL,
) {
}
public function setClient(?HttpClientInterface $client): void
{
$this->client = $client;
}
public function submit(RequestParameters $params): string
{
if (null === $this->client) {
throw new \UnexpectedValueException('Needed service is not injected.');
}
$response = $this->client->request('POST', $this->siteVerifyUrl, [
'body' => $params->toQueryString(),
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
]);
if (200 !== $response->getStatusCode()) {
return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}';
}
return $response->getContent();
}
}