Total Complexity | 40 |
Total Lines | 384 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like RecaptchaAdapter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RecaptchaAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class RecaptchaAdapter implements CaptchaInterface |
||
9 | { |
||
10 | /** |
||
11 | * ReCAPTCHA URL verifying |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; |
||
16 | |||
17 | const CLIENT_API = 'https://www.google.com/recaptcha/api.js'; |
||
18 | |||
19 | /** |
||
20 | * Public key |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | private $sitekey; |
||
25 | |||
26 | /** |
||
27 | * Private key |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | private $secretkey; |
||
32 | |||
33 | /** |
||
34 | * Remote IP address |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $remoteIp = null; |
||
39 | |||
40 | /** |
||
41 | * Supported themes |
||
42 | * |
||
43 | * @var array |
||
44 | * @see https://developers.google.com/recaptcha/docs/display#config |
||
45 | */ |
||
46 | protected static $themes = array('light', 'dark'); |
||
47 | |||
48 | /** |
||
49 | * Captcha theme. Default : light |
||
50 | * |
||
51 | * @var string |
||
52 | * @see https://developers.google.com/recaptcha/docs/display#config |
||
53 | */ |
||
54 | protected $theme = null; |
||
55 | |||
56 | /** |
||
57 | * Supported types |
||
58 | * |
||
59 | * @var array |
||
60 | * @see https://developers.google.com/recaptcha/docs/display#config |
||
61 | */ |
||
62 | protected static $types = array('image', 'audio'); |
||
63 | |||
64 | /** |
||
65 | * Captcha type. Default : image |
||
66 | * |
||
67 | * @var string |
||
68 | * @see https://developers.google.com/recaptcha/docs/display#config |
||
69 | */ |
||
70 | protected $type = null; |
||
71 | |||
72 | /** |
||
73 | * Captcha language. Default : auto-detect |
||
74 | * |
||
75 | * @var string |
||
76 | * @see https://developers.google.com/recaptcha/docs/language |
||
77 | */ |
||
78 | protected $language = null; |
||
79 | |||
80 | /** |
||
81 | * CURL timeout (in seconds) to verify response |
||
82 | * |
||
83 | * @var int |
||
84 | */ |
||
85 | private $verifyTimeout = 1; |
||
86 | |||
87 | /** |
||
88 | * Captcha size. Default : normal |
||
89 | * |
||
90 | * @var string |
||
91 | * @see https://developers.google.com/recaptcha/docs/display#render_param |
||
92 | */ |
||
93 | protected $size = null; |
||
94 | |||
95 | private static $instance = null; |
||
96 | |||
97 | /** |
||
98 | * List of errors |
||
99 | * |
||
100 | * @var array |
||
101 | */ |
||
102 | protected $errorCodes = array(); |
||
103 | |||
104 | |||
105 | /** |
||
106 | * RecaptchaAdapter |
||
107 | * |
||
108 | * @param array $params |
||
109 | * @return void |
||
110 | */ |
||
111 | private function __construct(array $params) |
||
112 | { |
||
113 | $this->http = new HttpClient(); |
||
|
|||
114 | |||
115 | $this->secretkey = $params['secret_key']; |
||
116 | $this->sitekey = $params['site_key']; |
||
117 | $this->type = $params['type']; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Get Instance |
||
122 | * @param array $params |
||
123 | * @return RecaptchaAdapter |
||
124 | */ |
||
125 | public static function getInstance(array $params): RecaptchaAdapter |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Set theme |
||
136 | * |
||
137 | * @param string $theme (see https://developers.google.com/recaptcha/docs/display#config) |
||
138 | * @return object |
||
139 | */ |
||
140 | public function setTheme($theme = 'light') |
||
141 | { |
||
142 | if (in_array($theme, self::$themes)) |
||
143 | $this->theme = $theme; |
||
144 | else |
||
145 | throw new \Exception('Theme "' . $theme . '"" is not supported. Available themes : ' . join(', ', self::$themes)); |
||
146 | |||
147 | return $this; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Set type |
||
152 | * |
||
153 | * @param string $type (see https://developers.google.com/recaptcha/docs/display#config) |
||
154 | * @return object |
||
155 | */ |
||
156 | public function setType($type = 'image') |
||
157 | { |
||
158 | if (in_array($type, self::$types)) |
||
159 | $this->type = $type; |
||
160 | |||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Set language |
||
166 | * |
||
167 | * @param string $language (see https://developers.google.com/recaptcha/docs/language) |
||
168 | * @return object |
||
169 | */ |
||
170 | public function setLanguage($language) |
||
171 | { |
||
172 | $this->language = $language; |
||
173 | |||
174 | return $this; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Set timeout |
||
179 | * |
||
180 | * @param int $timeout |
||
181 | * @return object |
||
182 | */ |
||
183 | public function setVerifyTimeout($timeout) |
||
184 | { |
||
185 | $this->verifyTimeout = $timeout; |
||
186 | |||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Set size |
||
192 | * |
||
193 | * @param string $size (see https://developers.google.com/recaptcha/docs/display#render_param) |
||
194 | * @return object |
||
195 | */ |
||
196 | public function setSize($size) |
||
197 | { |
||
198 | $this->size = $size; |
||
199 | |||
200 | return $this; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Generate the JS code of the captcha |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function renderJs($lang = null, $callback = false, $onLoadClass = 'onloadCallBack') |
||
209 | { |
||
210 | $data = array(); |
||
211 | if (!is_null($this->language)) |
||
212 | $data = array('hl' => $this->language); |
||
213 | |||
214 | return '<script src="https://www.google.com/recaptcha/api.js?' . http_build_query($data) . '"></script>'; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Get hCaptcha js link. |
||
219 | * |
||
220 | * @param string $lang |
||
221 | * @param boolean $callback |
||
222 | * @param string $onLoadClass |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | public function getJsLink($lang = null, $callback = false, $onLoadClass = 'onloadCallBack') |
||
227 | { |
||
228 | $client_api = static::CLIENT_API; |
||
229 | $params = []; |
||
230 | |||
231 | $callback ? $this->setCallBackParams($params, $onLoadClass) : false; |
||
232 | $lang ? $params['hl'] = $lang : null; |
||
233 | |||
234 | return $client_api . '?' . http_build_query($params); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Generate the HTML code block for the captcha |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | public function display($formIdentifier = '', $attributes = []) |
||
243 | { |
||
244 | if (!empty($this->sitekey)) { |
||
245 | if (strtolower($this->type) == 'visible'){ |
||
246 | $data = 'data-sitekey="' . $this->sitekey . '"'; |
||
247 | |||
248 | if (!is_null($this->theme)) |
||
249 | $data .= ' data-theme="' . $this->theme . '"'; |
||
250 | |||
251 | if (!is_null($this->type)) |
||
252 | $data .= ' data-type="' . $this->type . '"'; |
||
253 | |||
254 | if (!is_null($this->size)) |
||
255 | $data .= ' data-size="' . $this->size . '"'; |
||
256 | |||
257 | $captchaEleme = '<div class="col s1 offset-s2 g-recaptcha" ' . $data . '></div>'; |
||
258 | } elseif (strtolower($this->type) == 'invisible') { |
||
259 | $captchaEleme = ''; |
||
260 | if (!isset($attributes['data-callback'])) { |
||
261 | $captchaEleme = '<script> |
||
262 | document.addEventListener("DOMContentLoaded", function() { |
||
263 | let button = document.getElementsByTagName("button"); |
||
264 | |||
265 | button[0].setAttribute("data-sitekey", "' . $this->sitekey . '"); |
||
266 | button[0].setAttribute("data-callback", "onSubmit"); |
||
267 | button[0].setAttribute("data-action", "submit"); |
||
268 | button[0].classList.add("g-recaptcha"); |
||
269 | }) |
||
270 | |||
271 | function onSubmit (token){ |
||
272 | document.getElementById("'. $formIdentifier .'").submit(); |
||
273 | } |
||
274 | </script>'; |
||
275 | } |
||
276 | } |
||
277 | return $captchaEleme; |
||
278 | } |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Checks the code given by the captcha |
||
283 | * |
||
284 | * @param string $response Response code after submitting form (usually $_POST['g-recaptcha-response']) |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function verifyResponse($response, $clientIp = null) |
||
288 | { |
||
289 | if (is_null($this->secretkey)) |
||
290 | throw new \Exception('You must set your secret key'); |
||
291 | |||
292 | if (empty($response)) { |
||
293 | |||
294 | $this->errorCodes = array('internal-empty-response'); |
||
295 | |||
296 | return false; |
||
297 | } |
||
298 | |||
299 | $query = array( |
||
300 | 'secret' => $this->secretkey, |
||
301 | 'response' => $response, |
||
302 | 'remoteip' => $this->remoteIp, |
||
303 | ); |
||
304 | |||
305 | $url = self::VERIFY_URL . '?' . http_build_query($query); |
||
306 | |||
307 | $this->http->createRequest($url)->setMethod('GET')->start(); |
||
308 | $response = (array)$this->http->getResponseBody(); |
||
309 | |||
310 | if (empty($response) || is_null($response) || !$response) { |
||
311 | return false; |
||
312 | } |
||
313 | |||
314 | if (isset($response['error-codes'])) { |
||
315 | $this->errorCodes = $response['error-codes']; |
||
316 | } |
||
317 | |||
318 | return $response['success']; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Returns the errors encountered |
||
323 | * |
||
324 | * @return array Errors code and name |
||
325 | */ |
||
326 | public function getErrorCodes() |
||
392 | } |
||
393 | |||
394 | } |