CaptchaMiddleware::validate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Vicens\Captcha\Middleware;
4
5
use Closure;
6
use Illuminate\Contracts\Validation\Factory;
7
8
class CaptchaMiddleware
9
{
10
11
    /**
12
     * @var Factory
13
     */
14
    protected $validator;
15
16
    /**
17
     * CaptchaMiddleware constructor.
18
     *
19
     * @param Factory $validator
20
     */
21
    public function __construct(Factory $validator)
22
    {
23
        $this->validator = $validator;
24
    }
25
26
    /**
27
     * @param $request
28
     * @param Closure $next
29
     * @param string $captchaKey
30
     * @return mixed
31
     */
32
    public function handle($request, Closure $next, $captchaKey = 'captcha')
33
    {
34
        $this->validate($request, $captchaKey);
35
36
        return $next($request);
37
    }
38
39
    /**
40
     * 验证
41
     *
42
     * @param $request
43
     * @param $captchaKey
44
     */
45
    protected function validate($request, $captchaKey)
46
    {
47
        $this->validator->make(
48
            $request->only($captchaKey),
49
            $this->rules($captchaKey),
50
            $this->message($captchaKey)
51
        )->validate();
52
    }
53
54
    /**
55
     * 验证规则
56
     *
57
     * @param string $captchaKey
58
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
59
     */
60
    protected function rules($captchaKey)
61
    {
62
        return [
63
            $captchaKey => 'required|captcha'
64
        ];
65
    }
66
67
    /**
68
     * 验证的错误消息
69
     *
70
     * @param string $captchaKey
71
     * @return array
72
     */
73
    protected function message($captchaKey)
0 ignored issues
show
Unused Code introduced by
The parameter $captchaKey is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
    {
75
        return [];
76
    }
77
}
78