1 | <?php |
||
28 | class Client |
||
29 | { |
||
30 | /** |
||
31 | * Google reCaptcha verify url. |
||
32 | * @var string |
||
33 | */ |
||
34 | public static $siteVerifyUri = 'https://www.google.com/recaptcha/api/siteverify'; |
||
35 | |||
36 | /** @var string */ |
||
37 | protected $secret; |
||
38 | /** @var ClientInterface */ |
||
39 | protected $httpClient; |
||
40 | /** @var UriInterface */ |
||
41 | protected $uri; |
||
42 | /** @var RequestInterface */ |
||
43 | protected $request; |
||
44 | /** @var ResponseInterface */ |
||
45 | protected $response; |
||
46 | /** @var string */ |
||
47 | protected $method = 'POST'; |
||
48 | |||
49 | /** |
||
50 | * Client constructor. |
||
51 | * @param string $secret |
||
52 | * @param UriInterface|string|null $verifyUri |
||
53 | * @param ClientInterface|null $httpClient |
||
54 | * @param string $requestMethod |
||
55 | */ |
||
56 | 9 | public function __construct($secret, $httpClient = null, $verifyUri = null, $requestMethod = 'POST') |
|
57 | { |
||
58 | 9 | $this->secret = $secret; |
|
59 | |||
60 | 9 | if (is_string($verifyUri)) { |
|
61 | 1 | $this->uri = new Uri($verifyUri); |
|
62 | 9 | } elseif ($verifyUri instanceof UriInterface) { |
|
63 | 1 | $this->uri = $verifyUri; |
|
64 | 1 | } else { |
|
65 | 8 | $this->uri = new Uri(self::$siteVerifyUri); |
|
66 | } |
||
67 | |||
68 | 9 | $this->httpClient = (null === $httpClient) ? new SimpleClient() : $httpClient; |
|
69 | 9 | $this->method = $requestMethod; |
|
70 | 9 | } |
|
71 | |||
72 | /** |
||
73 | * Get reCaptcha secret key. |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | 1 | public function getSecret() |
|
81 | |||
82 | /** |
||
83 | * @param string $googleResponseToken |
||
84 | * @param null|string $ip |
||
85 | * @return bool |
||
86 | */ |
||
87 | 1 | public function validate($googleResponseToken, $ip = null) |
|
97 | |||
98 | /** |
||
99 | * @param string $googleResponseToken |
||
100 | * @param null|string $ip |
||
101 | * @return StreamInterface |
||
102 | */ |
||
103 | 2 | public function createBody($googleResponseToken, $ip = null) |
|
116 | |||
117 | /** |
||
118 | * @param RequestInterface $request |
||
119 | * @return bool |
||
120 | */ |
||
121 | 2 | public function validateRequest(RequestInterface $request) |
|
127 | |||
128 | /** |
||
129 | * @param ResponseInterface $response |
||
130 | * @return bool |
||
131 | * @throws ValidationException |
||
132 | */ |
||
133 | 5 | public function isValid(ResponseInterface $response) |
|
147 | |||
148 | /** |
||
149 | * Send verification request |
||
150 | * |
||
151 | * @param RequestInterface $request |
||
152 | * @return ResponseInterface |
||
153 | */ |
||
154 | 4 | public function send(RequestInterface $request) |
|
166 | } |
||
167 |