Passed
Push — 2.x ( e707aa...d6bbff )
by Terry
02:22
created

CaptchaTrait::captchaResponse()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 18
rs 10
1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * 
10
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Kernel;
24
25
use Shieldon\Firewall\Captcha\CaptchaInterface;
26
27
/*
28
 * Captcha Trait is loaded in Kernel instance only.
29
 */
30
trait CaptchaTrait
31
{
32
    /**
33
     * Container for captcha addons.
34
     * The collection of \Shieldon\Firewall\Captcha\CaptchaInterface
35
     *
36
     * @var array
37
     */
38
    public $captcha = [];
39
40
    /**
41
     * Set a captcha.
42
     *
43
     * @param CaptchaInterface $instance The captcha instance.
44
     *
45
     * @return void
46
     */
47
    public function setCaptcha(CaptchaInterface $instance): void
48
    {
49
        $class = $this->getClassName($instance);
0 ignored issues
show
Bug introduced by
It seems like getClassName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        /** @scrutinizer ignore-call */ 
50
        $class = $this->getClassName($instance);
Loading history...
50
        $this->captcha[$class] = $instance;
51
    }
52
53
    /**
54
     * Return the result from Captchas.
55
     *
56
     * @return bool
57
     */
58
    public function captchaResponse(): bool
59
    {
60
        foreach ($this->captcha as $captcha) {
61
            
62
            if (!$captcha->response()) {
63
                return false;
64
            }
65
        }
66
67
        /**
68
         * $sessionLimit @ SessionTrait
69
         * sessionHandler() @ SessionTrait
70
         */
71
        if (!empty($this->sessionLimit['count'])) {
72
            $this->result = $this->sessionHandler(self::RESPONSE_ALLOW);
0 ignored issues
show
Bug introduced by
It seems like sessionHandler() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
            /** @scrutinizer ignore-call */ 
73
            $this->result = $this->sessionHandler(self::RESPONSE_ALLOW);
Loading history...
Bug introduced by
The constant Shieldon\Firewall\Kernel...haTrait::RESPONSE_ALLOW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
73
        }
74
75
        return true;
76
    }
77
78
    /**
79
     * Mostly be used in unit testing purpose.
80
     *
81
     * @return void
82
     */
83
    public function disableCaptcha(): void
84
    {
85
        $this->captcha = [];
86
    }
87
}
88