CaptchaTrait::setCaptcha()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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\Kernel\Enum;
26
use Shieldon\Firewall\Captcha\CaptchaInterface;
27
28
/*
29
 * Captcha Trait is loaded in Kernel instance only.
30
 */
31
trait CaptchaTrait
32
{
33
    /**
34
     *   Public methods       | Desctiotion
35
     *  ----------------------|---------------------------------------------
36
     *   setCaptcha           | Set a captcha.
37
     *   captchaResponse      | Return the result from Captchas.
38
     *   disableCaptcha       | Mostly be used in unit testing purpose.
39
     *  ----------------------|---------------------------------------------
40
     */
41
42
    /**
43
     * Container for captcha addons.
44
     * The collection of \Shieldon\Firewall\Captcha\CaptchaInterface
45
     *
46
     * @var array
47
     */
48
    public $captcha = [];
49
50
    /**
51
     * Get a class name without namespace string.
52
     *
53
     * @param object $instance Class
54
     *
55
     * @return string
56
     */
57
    abstract protected function getClassName($instance): string;
58
59
    /**
60
     * Deal with online sessions.
61
     *
62
     * @param int $statusCode The response code.
63
     *
64
     * @return int The response code.
65
     */
66
    abstract protected function sessionHandler($statusCode): int;
67
68
    /**
69
     * Save and return the result identifier.
70
     * This method is for passing value from traits.
71
     *
72
     * @param int $resultCode The result identifier.
73
     *
74
     * @return int
75
     */
76
    abstract protected function setResultCode(int $resultCode): int;
77
78
    /**
79
     * Set a captcha.
80
     *
81
     * @param CaptchaInterface $instance The captcha instance.
82
     *
83
     * @return void
84
     */
85 202
    public function setCaptcha(CaptchaInterface $instance): void
86
    {
87 202
        $class = $this->getClassName($instance);
88 202
        $this->captcha[$class] = $instance;
89
    }
90
91
    /**
92
     * Return the result from Captchas.
93
     *
94
     * @return bool
95
     */
96 6
    public function captchaResponse(): bool
97
    {
98 6
        foreach ($this->captcha as $captcha) {
99
            if (!$captcha->response()) {
100 6
                return false;
101 6
            }
102
        }
103
104
        /**
105
         * $sessionLimit @ SessionTrait
106
         * sessionHandler() @ SessionTrait
107
         */
108
        if (!empty($this->sessionLimit['count'])) {
109 5
            return (bool) $this->setResultCode(
110 5
                $this->sessionHandler(Enum::RESPONSE_ALLOW)
111 5
            );
112 5
        }
113
114
        return true;
115 4
    }
116
117
    /**
118
     * Mostly be used in unit testing purpose.
119
     *
120
     * @return void
121
     */
122
    public function disableCaptcha(): void
123 61
    {
124
        $this->captcha = [];
125 61
    }
126
}
127