Completed
Push — master ( daab91...b549ed )
by Derek Stephen
02:54 queued 11s
created

FigletCaptcha::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php declare(strict_types=1);
2
3
namespace Del\Form\Field\Captcha;
4
5
use DateTime;
6
use Del\Form\Validator\ValidatorInterface;
7
use Del\SessionManager;
8
use Zend\Text\Figlet\Figlet;
9
10
class FigletCaptcha implements CaptchaAdapterInterface, ValidatorInterface
11
{
12
    /** @var SessionManager $session */
13
    private $session;
14
15
    /** @var string $timeout */
16
    private $timeout;
17
18
    /** @var string $timeout */
19
    private $length;
20
21
    /** @var string $word */
22
    private $word;
23
24
    /** @var array $errors */
25
    private $errors = [];
26
27
    public function __construct(SessionManager $session, string $timeout = '+3 minutes', int $length = 6)
28
    {
29
        $this->session = $session;
30
        $this->timeout = $timeout;
31
        $this->length = $length;
0 ignored issues
show
Documentation Bug introduced by
The property $length was declared of type string, but $length is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
32
        $this->generate();
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function generate(): string
39
    {
40
        $expiry = new DateTime();
41
        $session = $this->session->get('captcha');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $session is correct as $this->session->get('captcha') (which targets Del\SessionManager::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
42
43
        if ($session && $session['expiry'] > $expiry) {
44
            $this->word = $session['word'];
45
        } else {
46
            $this->word = $this->generateWord();
47
            $expiry->modify($this->timeout);
48
49
            $this->session->set('captcha', [
50
                'word' => $this->word,
51
                'expiry' => $expiry,
52
            ]);
53
        }
54
55
        return $this->word;
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function render(): string
62
    {
63
        $word = $this->word;
64
        $figlet = new Figlet();
65
66
        return '<div class="mono">' . ($figlet->render($word)) . '</div>';
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    private function generateWord(): string
73
    {
74
        $word = '';
75
76
         for ($x = 0; $x < $this->length; $x ++) {
77
             $word .= chr(rand(97, 122));
78
         }
79
80
        return $word;
81
    }
82
83
    /**
84
     * @param  mixed $value
85
     * @return bool
86
     * @throws Exception If validation of $value is impossible
87
     */
88
    public function isValid($value)
89
    {
90
        $now = new DateTime();
91
        $captcha = $this->session->get('captcha');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $captcha is correct as $this->session->get('captcha') (which targets Del\SessionManager::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
92
        $expiry = $captcha['expiry'];
93
        $word = $captcha['word'];
94
95
        if ($now > $expiry) {
96
            $this->errors = ['The CAPTCHA has expired'];
97
98
            return false;
99
        }
100
101
        if (!is_null($value) && $value !== $word) {
102
            $this->errors = ['The CAPTCHA value did not match.'];
103
104
            return false;
105
        }
106
107
        if (!is_null($value)) {
108
            $this->session->destroy('captcha');
109
        }
110
111
        return true;
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    public function getMessages()
118
    {
119
        return $this->errors;
120
    }
121
}
122