Completed
Push — master ( da6bda...f012c7 )
by ARCANEDEV
16s
created

src/Rules/CaptchaRule.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace Arcanedev\NoCaptcha\Rules;
2
3
use Illuminate\Contracts\Validation\Rule;
4
5
/**
6
 * Class     CaptchaRule
7
 *
8
 * @package  Arcanedev\NoCaptcha\Rules
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class CaptchaRule implements Rule
12
{
13
    /* -----------------------------------------------------------------
14
     |  Properties
15
     | -----------------------------------------------------------------
16
     */
17
18
    /** @var  string|null */
19
    protected $version;
20
21
    /** @var  array */
22
    protected $skipIps = [];
23
24
    /* -----------------------------------------------------------------
25
     |  Constructor
26
     | -----------------------------------------------------------------
27
     */
28
29
    /**
30
     * CaptchaRule constructor.
31
     *
32
     * @param  string|null  $version
33
     */
34 16
    public function __construct($version = null)
35
    {
36 16
        $this->version($version);
37 16
        $this->skipIps(
38 16
            config()->get('no-captcha.skip-ips', [])
39
        );
40 16
    }
41
42
    /* -----------------------------------------------------------------
43
     |  Setters
44
     | -----------------------------------------------------------------
45
     */
46
47
    /**
48
     * Set the ReCaptcha version.
49
     *
50
     * @param  string|null  $version
51
     *
52
     * @return $this
53
     */
54 16
    public function version($version)
55
    {
56 16
        $this->version = $version;
57
58 16
        return $this;
59
    }
60
61
    /**
62
     * Set the ips to skip.
63
     *
64
     * @param  string|array  $ip
65
     *
66
     * @return $this
67
     */
68 16
    public function skipIps($ip)
69
    {
70 16
        $this->skipIps = array_wrap($ip);
0 ignored issues
show
Deprecated Code introduced by
The function array_wrap() has been deprecated with message: Arr::wrap() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
71
72 16
        return $this;
73
    }
74
75
    /* -----------------------------------------------------------------
76
     |  Main methods
77
     | -----------------------------------------------------------------
78
     */
79
80
    /**
81
     * Determine if the validation rule passes.
82
     *
83
     * @param  string  $attribute
84
     * @param  mixed   $value
85
     *
86
     * @return bool
87
     */
88 16
    public function passes($attribute, $value)
89
    {
90 16
        $ip = request()->ip();
91
92 16
        if (in_array($ip, $this->skipIps))
93 8
            return true;
94
95 8
        return no_captcha($this->version)
96 8
            ->verify($value, $ip)
97 8
            ->isSuccess();
98
    }
99
100
    /**
101
     * Get the validation error message.
102
     *
103
     * @return string
104
     */
105 4
    public function message()
106
    {
107 4
        return (string) trans('validation.captcha');
108
    }
109
}
110