Passed
Push — master ( a2ba4c...af1bf0 )
by Vicens
03:10
created

CaptchaValidate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 65
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 12 2
A throwInvalidException() 0 5 1
A check() 0 4 1
A getCaptcha() 0 4 1
1
<?php
2
/**
3
 * @description HTTP中间件
4
 * @author vicens <[email protected]>
5
 */
6
7
namespace Vicens\Captcha\Middleware;
8
9
10
use Illuminate\Http\Request;
11
use Vicens\Captcha\Captcha;
12
use Closure;
13
use Vicens\Captcha\Exceptions\InvalidCaptcha;
14
15
class CaptchaValidate
16
{
17
18
    /**
19
     * @var Captcha
20
     */
21
    protected $captcha;
22
23
24
    public function __construct(Captcha $captcha)
25
    {
26
        $this->captcha = $captcha;
27
    }
28
29
    /**
30
     * @param Request $request
31
     * @param Closure $next
32
     * @return mixed
33
     */
34
    public function handle(Request $request, Closure $next)
35
    {
36
37
        // 验证
38
        if (!$this->check($this->getCaptcha($request))) {
39
40
            $this->throwInvalidException();
41
42
        }
43
44
        return $next($request);
45
    }
46
47
    /**
48
     * 抛出验证码错误的异常
49
     *
50
     * @throws InvalidCaptcha
51
     */
52
    protected function throwInvalidException()
53
    {
54
        // 验证码错误
55
        throw new InvalidCaptcha();
56
    }
57
58
    /**
59
     * 验证
60
     *
61
     * @param string $input
62
     * @return bool
63
     */
64
    protected function check($input)
65
    {
66
        return $this->captcha->check($input);
67
    }
68
69
    /**
70
     * 返回用户输入的验证码
71
     *
72
     * @param Request $request
73
     * @return string
74
     */
75
    protected function getCaptcha(Request $request)
76
    {
77
        return $request->get('captcha');
78
    }
79
}