Skip to content

Commit

Permalink
refactor: using curlrequest class on CI4
Browse files Browse the repository at this point in the history
  • Loading branch information
ddevsr committed Apr 1, 2023
1 parent ac9dfc2 commit 9586784
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
7 changes: 6 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use CodeIgniter\CodingStandard\CodeIgniter4;
use Nexus\CsConfig\Factory;
use PhpCsFixer\Finder;
Expand All @@ -11,7 +13,10 @@
__DIR__ . '/tests/',
])
->exclude('build')
->append([__FILE__]);
->append([
__FILE__,
__DIR__ . '/rector.php',
]);

$overrides = [];

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
},
"require-dev": {
"codeigniter4/devkit": "^1.0",
"codeigniter4/framework": "^4.2",
"rector/rector": "^0.15.0"
"codeigniter4/framework": "^4.3",
"rector/rector": "^0.15"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
3 changes: 1 addition & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector;
use Rector\Set\ValueObject\LevelSetList;
Expand Down Expand Up @@ -107,4 +106,4 @@
$rectorConfig->rule(MakeInheritedMethodVisibilitySameAsParentRector::class);
$rectorConfig->rule(SimplifyEmptyArrayCheckRector::class);
$rectorConfig->rule(NormalizeNamespaceByPSR4ComposerAutoloadRector::class);
};
};
38 changes: 18 additions & 20 deletions src/Recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHPDevsr\Recaptcha;

use CodeIgniter\Config\Services;
use CodeIgniter\HTTP\CURLRequest;
use Exception;
use PHPDevsr\Recaptcha\Config\Recaptcha as RecaptchaConfig;

Expand All @@ -18,6 +20,7 @@ class Recaptcha
protected const api_url = 'https://www.google.com/recaptcha/api.js';

protected RecaptchaConfig $config;
protected CURLRequest $curl;

public function __construct(?RecaptchaConfig $config = null)
{
Expand All @@ -29,37 +32,32 @@ public function __construct(?RecaptchaConfig $config = null)
if (empty($this->config->recaptchaSiteKey) || empty($this->config->recaptchaSecretKey)) {
throw new Exception('To use reCAPTCHA you must get an API key from ' . self::sign_up_url);
}

$this->curl = Services::curlrequest([
'timeout' => 5,
'headers' => [
'User-Agent' => 'PHPDevsr',
],
'http_errors' => false,
'allow_redirects' => true,
'verify' => true,
'version' => 2.0,
]);
}

/**
* HTTP Builder
*
* @param array $data Data
*
* @return bool|string
*
* @codeCoverageIgnore
*/
protected function _submitHTTPGet(array $data = [])
{
$url = $data !== [] ? self::site_verify_url . '?' . http_build_query($data) : self::site_verify_url;

if (ini_get('allow_url_fopen')) {
return file_get_contents($url);
}

$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$response = curl_exec($ch);
curl_close($ch);
$query = $data !== [] ? ['query' => $data] : [];
$responseObj = $this->curl->get(self::site_verify_url, $query);

return $response;
return $responseObj->getBody();
}

/**
Expand Down Expand Up @@ -94,7 +92,7 @@ public function verifyResponse($response, $remoteIp = null)
// get reCAPTCHA server response
$responses = json_decode($getResponse, true);

if (isset($responses['success']) && $responses['success'] === true) {
if (array_key_exists('success', $responses) && $responses['success'] === true) {
$status = true;
} else {
$status = false;
Expand Down

0 comments on commit 9586784

Please sign in to comment.