These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Vicens\Captcha\Middleware; |
||
4 | |||
5 | use Closure; |
||
6 | use Vicens\Captcha\Captcha; |
||
7 | use Symfony\Component\HttpFoundation\Request; |
||
8 | use Vicens\Captcha\Exceptions\InvalidCaptcha; |
||
9 | |||
10 | class CaptchaMiddleware |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @var Captcha |
||
15 | */ |
||
16 | protected $captcha; |
||
17 | |||
18 | |||
19 | public function __construct(Captcha $captcha) |
||
20 | { |
||
21 | $this->captcha = $captcha; |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * @param Request $request |
||
26 | * @param Closure $next |
||
27 | * @return mixed |
||
28 | * @throws InvalidCaptcha |
||
29 | */ |
||
30 | public function handle(Request $request, Closure $next) |
||
31 | { |
||
32 | |||
33 | if (!$this->check($this->getCaptcha($request))) { |
||
34 | |||
35 | $this->throwInvalidException(); |
||
36 | } |
||
37 | |||
38 | return $next($request); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * 抛出验证码错误的异常 |
||
43 | * |
||
44 | * @throws InvalidCaptcha |
||
45 | */ |
||
46 | protected function throwInvalidException() |
||
47 | { |
||
48 | // 验证码错误 |
||
49 | throw new InvalidCaptcha(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * 验证 |
||
54 | * |
||
55 | * @param string $input |
||
56 | * @return bool |
||
57 | */ |
||
58 | protected function check($input) |
||
0 ignored issues
–
show
|
|||
59 | { |
||
60 | return $this->captcha->check($input); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * 返回用户输入的验证码 |
||
65 | * |
||
66 | * @param Request $request |
||
67 | * @return string |
||
68 | */ |
||
69 | protected function getCaptcha(Request $request) |
||
70 | { |
||
71 | return $request->get('captcha'); |
||
72 | } |
||
73 | } |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.