NoCaptcha   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 21.05%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 51
ccs 4
cts 19
cp 0.2105
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHtml() 0 4 1
A renderInput() 0 3 2
A set() 0 5 1
A init() 0 4 1
A addRule() 0 2 1
A checkNoCaptcha() 0 6 3
1
<?php
2
3
namespace kalanis\kw_forms\Controls\Security\Captcha;
4
5
6
use kalanis\kw_rules\Interfaces\IRules;
7
8
/**
9
 * The NOCAPTCHA server URL's
10
 */
11 1
define('NOCAPTCHA_API_SERVER', 'https://www.google.com/recaptcha/api.js');
12 1
define('NOCAPTCHA_API_SECURE_SERVER', 'https://www.google.com/recaptcha/api/siteverify');
13
14
15
/**
16
 * Class NoCaptcha
17
 * @package kalanis\kw_forms\Controls\Security\Captcha
18
 * Define NoCaptcha service to render captcha
19
 * Uses service from Google to check if response contains correct answer
20
 * @codeCoverageIgnore remote service
21
 */
22
class NoCaptcha extends ACaptcha
23
{
24
    protected static string $publicKey = '';
25
    protected static string $privateKey = '';
26
27
    public static function init(string $privateKey, string $publicKey): void
28
    {
29
        static::$publicKey = $publicKey;
30
        static::$privateKey = $privateKey;
31
    }
32
33 2
    public function set(string $alias, string $errorMessage): self
34
    {
35 2
        $this->setEntry($alias);
36 2
        parent::addRule(IRules::SATISFIES_CALLBACK, $errorMessage, [$this, 'checkNoCaptcha']);
37 2
        return $this;
38
    }
39
40
    public function addRule(/** @scrutinizer ignore-unused */ string $ruleName, /** @scrutinizer ignore-unused */ string $errorText, /** @scrutinizer ignore-unused */ ...$args): void
41
    {
42
        // no additional rules applicable
43
    }
44
45
    /**
46
     * @param mixed $value
47
     * @return bool
48
     */
49
    public function checkNoCaptcha($value): bool
50
    {
51
        // entry has key: g-recaptcha-response
52
        $response = strval(file_get_contents(NOCAPTCHA_API_SECURE_SERVER . '?secret=' . static::$privateKey . '&response=' . strval($value)));
53
        $responseStructure = json_decode($response, true);
54
        return !is_null($responseStructure) && !empty($responseStructure['success']) && true === $responseStructure['success'];
55
    }
56
57
    public function renderInput($attributes = null): string
58
    {
59
        return $this->canPass() ? '' : $this->getHtml();
60
    }
61
62
    /**
63
     * Gets the challenge HTML (javascript only version).
64
     * This is called from the browser, and the resulting NoReCAPTCHA HTML widget
65
     * is embedded within the HTML form it was called from.
66
     *
67
     * @return string - The HTML to be embedded in the user's form.
68
     */
69
    protected function getHtml(): string
70
    {
71
        return '<script src="' . NOCAPTCHA_API_SERVER . '"></script>
72
	<div class="g-recaptcha" data-sitekey="' . static::$publicKey. '"></div>';
73
    }
74
}
75