|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the ReCaptcha Library. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Ilya Pokamestov <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace DS\Library\ReCaptcha\Http\Client\Guzzle; |
|
12
|
|
|
|
|
13
|
|
|
use DS\Library\ReCaptcha\Http\Client\ClientInterface; |
|
14
|
|
|
use DS\Library\ReCaptcha\Http\Response; |
|
15
|
|
|
use Psr\Http\Message\RequestInterface; |
|
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* ReCaptcha library, Guzzle client wrapper for Guzzle versions 4.*, 5.* and 6.*. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Ilya Pokamestov <[email protected]> |
|
22
|
|
|
* |
|
23
|
|
|
* Class GuzzleClient |
|
24
|
|
|
* @package DS\Library\ReCaptcha\Http\Client\Guzzle |
|
25
|
|
|
*/ |
|
26
|
|
|
class GuzzleClient implements ClientInterface |
|
27
|
|
|
{ |
|
28
|
|
|
const GUZZLE_THREE_CLIENT_CLASS = 'Guzzle\Http\Client'; |
|
29
|
|
|
const GUZZLE_CLIENT_CLASS = 'GuzzleHttp\Client'; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string|null */ |
|
32
|
|
|
protected $guzzleVersion = null; |
|
33
|
|
|
/** @var \GuzzleHttp\Client|\Guzzle\Http\Client */ |
|
34
|
|
|
protected $guzzle; |
|
35
|
|
|
/** @var RequestBuilder */ |
|
36
|
|
|
protected $requestBuilder; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param \GuzzleHttp\Client|\Guzzle\Http\Client $guzzleClient |
|
40
|
|
|
* @throw BadMethodCallException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($guzzleClient = null) |
|
43
|
|
|
{ |
|
44
|
|
|
if (null === $guzzleClient) { |
|
45
|
|
|
$guzzleClientClass = self::GUZZLE_CLIENT_CLASS; |
|
46
|
|
|
if ($this->checkClassExists(self::GUZZLE_THREE_CLIENT_CLASS, false)) { |
|
47
|
|
|
$guzzleClientClass = self::GUZZLE_THREE_CLIENT_CLASS; |
|
48
|
|
|
} elseif ($this->checkClassExists(self::GUZZLE_CLIENT_CLASS)) { |
|
49
|
|
|
$guzzleClientClass = self::GUZZLE_CLIENT_CLASS; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$guzzleClient = new $guzzleClientClass(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->guzzle = $guzzleClient; |
|
56
|
|
|
$this->requestBuilder = new RequestBuilder($this); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return \Guzzle\Http\Client|\GuzzleHttp\Client |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getGuzzleClient() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->guzzle; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Check if class available |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $className |
|
71
|
|
|
* @param bool $throwException |
|
72
|
|
|
* @throw BadMethodCallException |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function checkClassExists($className, $throwException = true) |
|
76
|
|
|
{ |
|
77
|
|
|
if (class_exists($className)) { |
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($throwException) { |
|
82
|
|
|
throw new \BadMethodCallException( |
|
83
|
|
|
sprintf('%s not found. Please check your dependencies in composer.json', $className) |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Set Guzzle client version |
|
92
|
|
|
* |
|
93
|
|
|
* @param $version string |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setVersion($version) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->guzzleVersion = $version; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param RequestInterface $request |
|
102
|
|
|
* @return ResponseInterface |
|
103
|
|
|
* @throws \Exception |
|
104
|
|
|
*/ |
|
105
|
|
|
public function send(RequestInterface $request) |
|
106
|
|
|
{ |
|
107
|
|
|
$guzzleRequest = $this->requestBuilder->buildGuzzleRequest($request); |
|
108
|
|
|
$guzzleResponse = $this->guzzle->send($guzzleRequest); |
|
109
|
|
|
|
|
110
|
|
|
if ($this->guzzleVersion === Version::VERSION_THREE) { |
|
111
|
|
|
$response = new Response( |
|
112
|
|
|
$guzzleResponse->getStatusCode(), |
|
113
|
|
|
$guzzleResponse->getHeaders()->toArray(), |
|
114
|
|
|
$guzzleResponse->getBody(true) |
|
115
|
|
|
); |
|
116
|
|
|
} else { |
|
117
|
|
|
$response = new Response( |
|
118
|
|
|
$guzzleResponse->getStatusCode(), |
|
119
|
|
|
$guzzleResponse->getHeaders(), |
|
120
|
|
|
$guzzleResponse->getBody()->getContents() |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $response; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|