Passed
Push — master ( 47fbf0...a2ba4c )
by Vicens
06:47
created

CaptchaValidate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

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