CaptchaRule   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 99
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A version() 0 6 1
A skipIps() 0 6 1
A passes() 0 11 2
A message() 0 4 1
1
<?php namespace Arcanedev\NoCaptcha\Rules;
2
3
use Illuminate\Contracts\Validation\Rule;
4
use Illuminate\Support\Arr;
5
6
/**
7
 * Class     CaptchaRule
8
 *
9
 * @package  Arcanedev\NoCaptcha\Rules
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class CaptchaRule implements Rule
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
19
    /** @var  string|null */
20
    protected $version;
21
22
    /** @var  array */
23
    protected $skipIps = [];
24
25
    /* -----------------------------------------------------------------
26
     |  Constructor
27
     | -----------------------------------------------------------------
28
     */
29
30
    /**
31
     * CaptchaRule constructor.
32
     *
33
     * @param  string|null  $version
34
     */
35 24
    public function __construct($version = null)
36
    {
37 24
        $this->version($version);
38 24
        $this->skipIps(
39 24
            config()->get('no-captcha.skip-ips', [])
40
        );
41 24
    }
42
43
    /* -----------------------------------------------------------------
44
     |  Setters
45
     | -----------------------------------------------------------------
46
     */
47
48
    /**
49
     * Set the ReCaptcha version.
50
     *
51
     * @param  string|null  $version
52
     *
53
     * @return $this
54
     */
55 24
    public function version($version)
56
    {
57 24
        $this->version = $version;
58
59 24
        return $this;
60
    }
61
62
    /**
63
     * Set the ips to skip.
64
     *
65
     * @param  string|array  $ip
66
     *
67
     * @return $this
68
     */
69 24
    public function skipIps($ip)
70
    {
71 24
        $this->skipIps = Arr::wrap($ip);
72
73 24
        return $this;
74
    }
75
76
    /* -----------------------------------------------------------------
77
     |  Main methods
78
     | -----------------------------------------------------------------
79
     */
80
81
    /**
82
     * Determine if the validation rule passes.
83
     *
84
     * @param  string  $attribute
85
     * @param  mixed   $value
86
     *
87
     * @return bool
88
     */
89 24
    public function passes($attribute, $value)
90
    {
91 24
        $ip = request()->ip();
92
93 24
        if (in_array($ip, $this->skipIps))
94 12
            return true;
95
96 12
        return no_captcha($this->version)
97 12
            ->verify($value, $ip)
98 12
            ->isSuccess();
99
    }
100
101
    /**
102
     * Get the validation error message.
103
     *
104
     * @return string
105
     */
106 6
    public function message()
107
    {
108 6
        return (string) trans('validation.captcha');
109
    }
110
}
111