Skip to content

Commit

Permalink
refactor: added helper and service
Browse files Browse the repository at this point in the history
  • Loading branch information
ddevsr committed Sep 20, 2023
1 parent da0927b commit 2db3aa3
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 30 deletions.
20 changes: 17 additions & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@

declare(strict_types=1);

/**
* This file is part of PHPDevsr/recaptcha-codeigniter4.
*
* (c) 2023 Denny Septian Panggabean <xamidimura@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

use CodeIgniter\CodingStandard\CodeIgniter4;
use Nexus\CsConfig\Factory;
use PhpCsFixer\Finder;

$finder = Finder::create()
->files()
->in([
__DIR__ . '/src/',
__DIR__ . '/tests/',
__DIR__ . '/src',
__DIR__ . '/tests',
])
->exclude('build')
->append([
Expand All @@ -25,4 +34,9 @@
'cacheFile' => 'build/.php-cs-fixer.cache',
];

return Factory::create(new CodeIgniter4(), $overrides, $options)->forProjects();
return Factory::create(new CodeIgniter4(), $overrides, $options)->forLibrary(
'PHPDevsr/recaptcha-codeigniter4',
'Denny Septian Panggabean',
'xamidimura@gmail.com',
2023
);
15 changes: 0 additions & 15 deletions admin/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@ if [ "$STAGED_PHP_FILES" != "" ]; then
done
fi

if [ "$FILES" != "" ]; then
echo "Running PHPStan..."

if [ -d /proc/cygdrive ]; then
XDEBUG_MODE=off ./vendor/bin/phpstan analyse
else
XDEBUG_MODE=off php ./vendor/bin/phpstan analyse
fi

if [ $? != 0 ]; then
echo "Fix the phpstan error(s) before commit."
exit 1
fi
fi

if [ "$FILES" != "" ]; then
echo "Running PHP CS Fixer..."

Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
"php": "^7.4 || ^8.0"
},
"require-dev": {
"codeigniter4/devkit": "^1.0",
"codeigniter4/framework": "^4.3",
"rector/rector": "^0.18"
"codeigniter/coding-standard": "^1.7",
"nexusphp/tachycardia": "^1.4",
"php-coveralls/php-coveralls": "^2.5",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.6",
"rector/rector": "0.18.3"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<arguments>
<array>
<element key="timeLimit">
<double>0.50</double>
<double>0.40</double>
</element>
<element key="reportable">
<integer>30</integer>
Expand Down
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

/**
* This file is part of PHPDevsr/Minifyku.
* This file is part of PHPDevsr/recaptcha-codeigniter4.
*
* (c) 2023 Denny Septian Panggabean <xamidimura@gmail.com>
*
Expand Down
11 changes: 10 additions & 1 deletion src/Commands/ConfigPublish.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/**
* This file is part of PHPDevsr/recaptcha-codeigniter4.
*
* (c) 2023 Denny Septian Panggabean <xamidimura@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace PHPDevsr\Recaptcha\Commands;

use CodeIgniter\CLI\BaseCommand;
Expand Down Expand Up @@ -42,7 +51,7 @@ protected function determineSourcePath()
{
$this->sourcePath = realpath(__DIR__ . '/../');

if ($this->sourcePath === '/' || empty($this->sourcePath)) {
if (in_array($this->sourcePath, ['/', ''], true) || $this->sourcePath === false) {
CLI::error('Unable to determine the correct source directory. Bailing.');

exit();
Expand Down
9 changes: 9 additions & 0 deletions src/Config/Recaptcha.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/**
* This file is part of PHPDevsr/recaptcha-codeigniter4.
*
* (c) 2023 Denny Septian Panggabean <xamidimura@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace PHPDevsr\Recaptcha\Config;

use CodeIgniter\Config\BaseConfig;
Expand Down
29 changes: 29 additions & 0 deletions src/Config/Services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/**
* This file is part of PHPDevsr/recaptcha-codeigniter4.
*
* (c) 2023 Denny Septian Panggabean <xamidimura@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace PHPDevsr\Recaptcha\Config;

use CodeIgniter\Config\BaseService;
use PHPDevsr\Recaptcha\Recaptcha;

class Services extends BaseService
{
public static function recaptcha(bool $getShared = true): Recaptcha
{
if ($getShared) {
return self::getSharedInstance('recaptcha');
}

return new Recaptcha(config('Recaptcha'));
}
}
54 changes: 54 additions & 0 deletions src/Helpers/recaptcha_helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/**
* This file is part of PHPDevsr/recaptcha-codeigniter4.
*
* (c) 2023 Denny Septian Panggabean <xamidimura@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

if (! function_exists('getWidget')) {
/**
* Render reCAPTCHA Widget
*
* @param array $parameters Attribute
*/
function getWidget(array $parameters = []): string
{
$recaptcha = service('recaptcha');

return $recaptcha->getWidget($parameters);
}
}

if (! function_exists('getScriptTag')) {
/**
* Render Script Tag
*
* @param array $parameters Attribute
*/
function getScriptTag(array $parameters = []): string
{
$recaptcha = service('recaptcha');

return $recaptcha->getScriptTag($parameters);
}
}

if (! function_exists('verifyResponse')) {
/**
* Verify Response
*
* @param string $response response string from Recaptcha Verification
*/
function verifyResponse($response): array
{
$recaptcha = service('recaptcha');

return $recaptcha->verifyResponse($response);
}
}
19 changes: 13 additions & 6 deletions src/Recaptcha.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/**
* This file is part of PHPDevsr/recaptcha-codeigniter4.
*
* (c) 2023 Denny Septian Panggabean <xamidimura@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace PHPDevsr\Recaptcha;

use CodeIgniter\Config\Services;
Expand All @@ -22,14 +31,12 @@ class Recaptcha
protected RecaptchaConfig $config;
protected CURLRequest $curl;

public function __construct(?RecaptchaConfig $config = null)
public function __construct(RecaptchaConfig $config)
{
// Set Config
if (null !== $config) {
$this->config = $config;
}
$this->config = $config;

if (empty($this->config->recaptchaSiteKey) || empty($this->config->recaptchaSecretKey)) {
if ($this->config->recaptchaSiteKey === '' || $this->config->recaptchaSecretKey === '') {
throw new Exception('To use reCAPTCHA you must get an API key from ' . self::sign_up_url);
}

Expand Down Expand Up @@ -71,7 +78,7 @@ protected function _submitHTTPGet(array $data = [])
*/
public function verifyResponse($response, $remoteIp = null)
{
$remoteIp = (! empty($remoteIp)) ? $remoteIp : $_SERVER['REMOTE_ADDR'];
$remoteIp = (empty($remoteIp)) ? $_SERVER['REMOTE_ADDR'] : $remoteIp;

// Discard empty solution submissions
if (empty($response)) {
Expand Down
16 changes: 15 additions & 1 deletion tests/RecaptchaTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/**
* This file is part of PHPDevsr/recaptcha-codeigniter4.
*
* (c) 2023 Denny Septian Panggabean <xamidimura@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests;

use CodeIgniter\Test\CIUnitTestCase;
Expand All @@ -17,7 +26,12 @@ public function testExceptionAPIKey()
$this->expectException(Exception::class);
$this->expectExceptionMessage('To use reCAPTCHA you must get an API key from https://www.google.com/recaptcha/admin');

new Recaptcha();
$config = new RecaptchaConfig();
$config->recaptchaSiteKey = '';
$config->recaptchaSecretKey = '';
$config->recaptchaLang = 'id';

new Recaptcha($config);
}

public function testGetScriptTagDefault()
Expand Down

0 comments on commit 2db3aa3

Please sign in to comment.