Test Failed
Push — main ( 6435b1...db043e )
by Paul
16:23 queued 07:02
created

FriendlycaptchaValidator::data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Validator;
4
5
use GeminiLabs\SiteReviews\Defaults\CaptchaConfigDefaults;
6
use GeminiLabs\SiteReviews\Modules\Captcha;
7
8
class FriendlycaptchaValidator extends CaptchaValidatorAbstract
9
{
10
    /**
11
     * @see https://docs.friendlycaptcha.com/
12
     */
13
    public function config(): array
14
    {
15
        return glsr(CaptchaConfigDefaults::class)->merge([
16
            'class' => glsr_get_option('forms.captcha.theme').' frc-captcha',
17
            'language' => $this->getLocale(),
18
            'sitekey' => $this->siteKey(),
19
            'theme' => glsr_get_option('forms.captcha.theme'),
20
            'type' => 'friendlycaptcha',
21
            'urls' => [ // order is intentional, module should always load first
22
                'module' => 'https://unpkg.com/[email protected]/widget.module.min.js',
23
                'nomodule' => 'https://unpkg.com/[email protected]/widget.min.js',
24
            ],
25
        ]);
26
    }
27
28
    public function isEnabled(): bool
29
    {
30
        return glsr(Captcha::class)->isEnabled('friendlycaptcha');
31
    }
32
33
    protected function data(): array
34
    {
35
        return [
36
            'secret' => $this->siteSecret(),
37
            'sitekey' => $this->siteKey(),
38
            'solution' => $this->token(),
39
        ];
40
    }
41
42
    protected function errorCodes(): array
43
    {
44
        return [
45
            'bad_request' => 'Something else is wrong with your request, e.g. your request body is empty.',
46
            'secret_invalid' => 'Your secret key is invalid.',
47
            'secret_missing' => 'Your secret key is missing.',
48
            'sitekey_invalid' => 'Your site key is likely invalid.',
49
            'sitekey_missing' => 'Your site key is missing.',
50
            'solution_invalid' => 'The solution you provided was invalid (perhaps the user tried to tamper with the puzzle).',
51
            'solution_missing' => 'You forgot to add the solution parameter.',
52
            'solution_timeout_or_duplicate' => 'The puzzle that the solution was for has expired or has already been used.',
53
        ];
54
    }
55
56
    protected function errors(array $errors): array
57
    {
58
        if (empty($this->siteKey())) {
59
            $errors[] = 'sitekey_missing';
60
        } elseif ('sitekey_invalid' === $this->token()) {
61
            $errors[] = 'sitekey_invalid';
62
        }
63
        return parent::errors($errors);
64
    }
65
66
    protected function siteKey(): string
67
    {
68
        return glsr_get_option('forms.friendlycaptcha.key');
69
    }
70
71
    protected function siteSecret(): string
72
    {
73
        return glsr_get_option('forms.friendlycaptcha.secret');
74
    }
75
76
    protected function siteVerifyUrl(): string
77
    {
78
        return 'https://api.friendlycaptcha.com/api/v1/siteverify';
79
    }
80
81
    protected function token(): string
82
    {
83
        return $this->request['_frcaptcha'] ?? '';
84
    }
85
}
86