Skip to content

Commit

Permalink
Refactoring & Testing NoCaptcha Class
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed May 24, 2015
1 parent 2ee6f38 commit 48e36ad
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 28 deletions.
85 changes: 59 additions & 26 deletions src/NoCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ protected function setSiteKey($siteKey)

/**
* Set language code
* @todo: Adding locale check OR not ?
*
* @param string $lang
*
* @return NoCaptcha
*/
protected function setLang($lang)
{
// TODO: Add check lang or not !!
$this->lang = $lang;

return $this;
Expand All @@ -139,26 +139,26 @@ protected function setLang($lang)
/**
* Get script source link
*
* @param string null $callback
* @param string|null $callbackName
*
* @return string
*/
private function getScriptSrc($callback = null)
private function getScriptSrc($callbackName = null)
{
$first = true;
$link = static::CLIENT_URL;
$queries = [];

// TODO: Refactor to query builder
if ( ! empty($this->lang)) {
$link .= ('?hl=' . $this->lang);
$first = false;
if ($this->hasLang()) {
$queries['hl'] = $this->lang;
}

if ( ! is_null($callback)) {
$link .= ($first ? '?' : '&') . "onload={$callback}&render=explicit";
if ($this->hasCallbackName($callbackName)) {
$queries['onload'] = $callbackName;
$queries['render'] = 'explicit';
}

return $link;
$queries = count($queries) ? '?' . http_build_query($queries) : '';

return static::CLIENT_URL . $queries;
}

/**
Expand Down Expand Up @@ -258,22 +258,22 @@ public function verify($response, $clientIp = null)
]);

return isset($response['success']) and
$response['success'] === true;
$response['success'] === true;
}

/**
* Get script tag
*
* @param string|null $callback
* @param string|null $callbackName
*
* @return string
*/
public function script($callback = null)
public function script($callbackName = null)
{
$script = '';

if ( ! $this->scriptLoaded) {
$script = '<script src="' . $this->getScriptSrc($callback) . '" async defer></script>';
$script = '<script src="' . $this->getScriptSrc($callbackName) . '" async defer></script>';
$this->scriptLoaded = true;
}

Expand All @@ -283,35 +283,68 @@ public function script($callback = null)
/**
* Get script tag with a callback function
*
* @param array $captchas
* @param array $captchas
* @param string $callbackName
*
* @return string
*/
public function scriptWithCallback(array $captchas)
public function scriptWithCallback(array $captchas, $callbackName = 'captchaRenderCallback')
{
$script = $this->script('CaptchaCallback');
$script = $this->script($callbackName);

if (empty($script) or empty($captchas)) {
return $script;
}

$captchas = implode(PHP_EOL, array_map(function($captcha) {
return "grecaptcha.render('{$captcha}', {'sitekey' : '{$this->siteKey}'});";
}, $captchas));

return implode(PHP_EOL, [$script, implode(PHP_EOL, [
'<script>',
'var CaptchaCallback = function(){',
$captchas,
'};',
"var $callbackName = function() {",
$this->renderCaptchas($captchas),
'};',
'</script>'
])]);
}

/**
* Rendering captchas with callback function
*
* @param array $captchas
*
* @return string
*/
private function renderCaptchas(array $captchas)
{
return implode(PHP_EOL, array_map(function($captcha) {
return "grecaptcha.render('{$captcha}', {'sitekey' : '{$this->siteKey}'});";
}, $captchas));
}

/* ------------------------------------------------------------------------------------------------
| Check Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Check if has lang
*
* @return bool
*/
private function hasLang()
{
return ! empty($this->lang);
}

/**
* Check if callback is not empty
*
* @param string|null $callbackName
*
* @return bool
*/
private function hasCallbackName($callbackName)
{
return ! (is_null($callbackName) or trim($callbackName) === '');
}

/**
* Check key
*
Expand Down
4 changes: 2 additions & 2 deletions tests/NoCaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ public function it_can_render_script_with_callback()
{
$captchas = ['captcha-1', 'captcha-2'];
$script =
'<script src="' . NoCaptcha::CLIENT_URL . '?onload=CaptchaCallback&render=explicit" async defer></script>
'<script src="' . NoCaptcha::CLIENT_URL . '?onload=captchaRenderCallback&render=explicit" async defer></script>
<script>
var CaptchaCallback = function(){
var captchaRenderCallback = function() {
grecaptcha.render(\'captcha-1\', {\'sitekey\' : \'site-key\'});
grecaptcha.render(\'captcha-2\', {\'sitekey\' : \'site-key\'});
};
Expand Down

0 comments on commit 48e36ad

Please sign in to comment.