Captcha   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 7
Bugs 2 Features 0
Metric Value
eloc 20
c 7
b 2
f 0
dl 0
loc 52
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A message() 0 7 2
B passes() 0 27 8
A getResponse() 0 3 1
1
<?php
2
3
namespace Torralbodavid\SimpleRecaptchaV3\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Torralbodavid\SimpleRecaptchaV3\Services\Captcha as CaptchaService;
7
8
class Captcha implements Rule
9
{
10
    protected $serviceResponse;
11
    protected $userResponse;
12
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function passes($attribute, $response)
17
    {
18
        if (! config('simple-recaptcha-v3.active')) {
19
            return true;
20
        }
21
22
        $this->userResponse = $response;
23
24
        if ($this->userResponse === null) {
25
            return false;
26
        }
27
28
        $this->serviceResponse = $this->getResponse(new CaptchaService());
29
30
        if (! empty($this->serviceResponse['error-codes'])) {
31
            return false;
32
        }
33
34
        if (config('simple-recaptcha-v3.hostname_check') && request()->getHttpHost() !== $this->serviceResponse['hostname']) {
35
            return false;
36
        }
37
38
        if (! $this->serviceResponse['success'] || $this->serviceResponse['score'] < config('simple-recaptcha-v3.minimum_score')) {
39
            return false;
40
        }
41
42
        return true;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function message()
49
    {
50
        if ($this->serviceResponse['error-codes'] === null) {
51
            return 'simple-recaptcha-v3::messages.response-null';
52
        }
53
54
        return "simple-recaptcha-v3::messages.{$this->serviceResponse['error-codes'][0]}";
55
    }
56
57
    protected function getResponse(CaptchaService $service): array
58
    {
59
        return (array) $service($this->userResponse);
60
    }
61
}
62