|
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 Psr\Http\Message\RequestInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* ReCaptcha library, Guzzle request builder, build request object for any Guzzle versions. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Ilya Pokamestov <[email protected]> |
|
19
|
|
|
* |
|
20
|
|
|
* Class RequestBuilder |
|
21
|
|
|
* @package DS\Library\ReCaptcha\Http\Client\Guzzle |
|
22
|
|
|
*/ |
|
23
|
|
|
class RequestBuilder |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
protected $guzzlePrsRequestClass = '\GuzzleHttp\Psr7\Request'; |
|
27
|
|
|
/** @var GuzzleClient */ |
|
28
|
|
|
protected $guzzleReCaptchaClient; |
|
29
|
|
|
/** @var \Guzzle\Http\Client|\GuzzleHttp\Client */ |
|
30
|
|
|
protected $guzzleClient; |
|
31
|
|
|
/** @var array */ |
|
32
|
|
|
protected $callableChain = array(); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param GuzzleClient $guzzle |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(GuzzleClient $guzzle) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->guzzleReCaptchaClient = $guzzle; |
|
40
|
|
|
$this->guzzleClient = $guzzle->getGuzzleClient(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Build guzzle request based on Guzzle client version. |
|
45
|
|
|
* |
|
46
|
|
|
* @param RequestInterface $request |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
* @throws RequestException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function buildGuzzleRequest(RequestInterface $request) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->addCallable(function () use ($request) { |
|
53
|
|
|
return $this->buildForVersionThree($request); |
|
54
|
|
|
}); |
|
55
|
|
|
|
|
56
|
|
|
$this->addCallable(function () use ($request) { |
|
57
|
|
|
return $this->buildForVersionFourOrFive($request); |
|
58
|
|
|
}); |
|
59
|
|
|
|
|
60
|
|
|
$this->addCallable(function () use ($request) { |
|
61
|
|
|
return $this->buildForVersionSix($request); |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
$guzzleRequest = $this->executeCallableChain(); |
|
65
|
|
|
|
|
66
|
|
|
if (null === $guzzleRequest) { |
|
67
|
|
|
throw new RequestException('Unknown Guzzle client, request instance can not be created.'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $guzzleRequest; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Add request builder callable |
|
75
|
|
|
* |
|
76
|
|
|
* @param callable $callable |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function addCallable($callable) |
|
79
|
|
|
{ |
|
80
|
|
|
if (!is_callable($callable)) { |
|
81
|
|
|
throw new \InvalidArgumentException('Argument must be callable.'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->callableChain[] = $callable; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Execute callable chain and return callable result. |
|
89
|
|
|
* |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function executeCallableChain() |
|
93
|
|
|
{ |
|
94
|
|
|
$callableResult = null; |
|
95
|
|
|
foreach ($this->callableChain as $callable) { |
|
96
|
|
|
$callableResult = call_user_func($callable); |
|
97
|
|
|
|
|
98
|
|
|
if (null !== $callableResult) { |
|
99
|
|
|
break; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $callableResult; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Build request object for Guzzle 4.* or 5.* |
|
108
|
|
|
* |
|
109
|
|
|
* @param RequestInterface $request |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function buildForVersionFourOrFive(RequestInterface $request) |
|
113
|
|
|
{ |
|
114
|
|
|
if (method_exists($this->guzzleClient, 'createRequest')) { |
|
115
|
|
|
$this->guzzleReCaptchaClient->setVersion(Version::VERSION_FOUR_FIVE); |
|
116
|
|
|
return $this->guzzleClient->createRequest( |
|
117
|
|
|
$request->getMethod(), |
|
118
|
|
|
(string)$request->getUri(), |
|
119
|
|
|
array ( |
|
120
|
|
|
'headers' => $request->getHeaders(), |
|
121
|
|
|
'body' => $request->getBody()->getContents(), |
|
122
|
|
|
) |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return null; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Build request object for Guzzle 6.* |
|
131
|
|
|
* |
|
132
|
|
|
* @param RequestInterface $request |
|
133
|
|
|
* @return mixed |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function buildForVersionSix(RequestInterface $request) |
|
136
|
|
|
{ |
|
137
|
|
|
if (class_exists($this->guzzlePrsRequestClass)) { |
|
138
|
|
|
$this->guzzleReCaptchaClient->setVersion(Version::VERSION_SIX); |
|
139
|
|
|
return new $this->guzzlePrsRequestClass( |
|
140
|
|
|
$request->getMethod(), |
|
141
|
|
|
(string)$request->getUri(), |
|
142
|
|
|
$request->getHeaders(), |
|
143
|
|
|
$request->getBody() |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return null; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Build request object for Guzzle 3.* |
|
152
|
|
|
* |
|
153
|
|
|
* @param RequestInterface $request |
|
154
|
|
|
* @return mixed |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function buildForVersionThree(RequestInterface $request) |
|
157
|
|
|
{ |
|
158
|
|
|
if (get_class($this->guzzleClient) === GuzzleClient::GUZZLE_THREE_CLIENT_CLASS |
|
159
|
|
|
&& method_exists($this->guzzleClient, 'createRequest')) { |
|
160
|
|
|
$this->guzzleReCaptchaClient->setVersion(Version::VERSION_THREE); |
|
161
|
|
|
return $this->guzzleClient->createRequest( |
|
162
|
|
|
$request->getMethod(), |
|
163
|
|
|
(string)$request->getUri(), |
|
164
|
|
|
$request->getHeaders(), |
|
165
|
|
|
$request->getBody()->getContents() |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return null; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|