Conditions | 6 |
Paths | 5 |
Total Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
80 | public function validate($validator) |
||
81 | { |
||
82 | $hCaptchaResponse = Controller::curr()->getRequest()->requestVar('h-captcha-response'); |
||
83 | |||
84 | if (!isset($hCaptchaResponse)) { |
||
85 | $validator->validationError( |
||
86 | $this->name, |
||
87 | _t( |
||
88 | 'X3dgoo\\HCaptcha\\Forms\\HCaptchaField.EMPTY', |
||
89 | 'Please answer the captcha. If you do not see the captcha please enable Javascript' |
||
90 | ), |
||
91 | 'validation' |
||
92 | ); |
||
93 | |||
94 | return false; |
||
95 | } |
||
96 | |||
97 | if (!function_exists('curl_init')) { |
||
98 | user_error('You must enable php-curl to use this field', E_USER_ERROR); |
||
99 | |||
100 | return false; |
||
101 | } |
||
102 | |||
103 | $secretKey = $this->getSecretKey(); |
||
104 | $url = 'https://hcaptcha.com/siteverify' . |
||
105 | '?secret=' . $secretKey . |
||
106 | '&response=' . rawurlencode($hCaptchaResponse) . |
||
107 | '&remoteip=' . rawurlencode($_SERVER['REMOTE_ADDR']); |
||
108 | $ch = curl_init($url); |
||
109 | |||
110 | curl_setopt($ch, CURLOPT_TIMEOUT, 10); |
||
111 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
112 | curl_setopt($ch, CURLOPT_USERAGENT, str_replace(',', '/', 'SilverStripe')); |
||
113 | $response = json_decode(curl_exec($ch), true); |
||
114 | |||
115 | if (is_array($response)) { |
||
116 | if (array_key_exists('success', $response) && $response['success'] == false) { |
||
117 | $validator->validationError( |
||
118 | $this->name, |
||
119 | _t( |
||
120 | 'X3dgoo\\HCaptcha\\Forms\\HCaptchaField.EMPTY', |
||
121 | 'Please answer the captcha. If you do not see the captcha please enable Javascript' |
||
122 | ), |
||
123 | 'validation' |
||
124 | ); |
||
125 | |||
126 | return false; |
||
127 | } |
||
128 | } else { |
||
129 | $validator->validationError( |
||
130 | $this->name, |
||
131 | _t( |
||
132 | 'X3dgoo\\HCaptcha\\Forms\\HCaptchaField.VALIDATE_ERROR', |
||
133 | 'Captcha could not be validated' |
||
134 | ), |
||
135 | 'validation' |
||
136 | ); |
||
137 | $logger = Injector::inst()->get(LoggerInterface::class); |
||
138 | $logger->error( |
||
139 | 'Captcha validation failed as request was not successful.' |
||
140 | ); |
||
141 | |||
142 | return false; |
||
143 | } |
||
144 | |||
145 | return true; |
||
146 | } |
||
147 | |||
193 |