Passed
Push — 2.x ( d6434d...dbf03f )
by Terry
01:59
created

CaptchaTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setCaptcha() 0 4 1
A captchaResponse() 0 18 4
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
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Kernel;
14
15
use Shieldon\Firewall\Captcha\CaptchaInterface;
16
17
/*
18
 * Captcha Trait is loaded in Kernel instance only.
19
 */
20
trait CaptchaTrait
21
{
22
    /**
23
     * Container for captcha addons.
24
     * The collection of \Shieldon\Firewall\Captcha\CaptchaInterface
25
     *
26
     * @var array
27
     */
28
    public $captcha = [];
29
30
    /**
31
     * Set a captcha.
32
     *
33
     * @param CaptchaInterface $instance
34
     *
35
     * @return void
36
     */
37
    public function setCaptcha(CaptchaInterface $instance): void
38
    {
39
        $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

39
        /** @scrutinizer ignore-call */ 
40
        $class = $this->getClassName($instance);
Loading history...
40
        $this->captcha[$class] = $instance;
41
    }
42
43
    /**
44
     * Return the result from Captchas.
45
     *
46
     * @return bool
47
     */
48
    public function captchaResponse(): bool
49
    {
50
        foreach ($this->captcha as $captcha) {
51
            
52
            if (!$captcha->response()) {
53
                return false;
54
            }
55
        }
56
57
        /**
58
         * $sessionLimit @ SessionTrait
59
         * sessionHandler() @ SessionTrait
60
         */
61
        if (!empty($this->sessionLimit['count'])) {
62
            $this->result = $this->sessionHandler(self::RESPONSE_ALLOW);
0 ignored issues
show
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...
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 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

62
            /** @scrutinizer ignore-call */ 
63
            $this->result = $this->sessionHandler(self::RESPONSE_ALLOW);
Loading history...
63
        }
64
65
        return true;
66
    }
67
}
68