Passed
Push — master ( e89a22...38e786 )
by Petr
08:13
created

Numerical::renderInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_forms\Controls\Security\Captcha;
4
5
6
use ArrayAccess;
7
use kalanis\kw_rules\Interfaces\IRules;
8
9
10
/**
11
 * Class Numerical
12
 * @package kalanis\kw_forms\Controls\Security\Captcha
13
 * Numerical operation solving captcha
14
 */
15
class Numerical extends AGraphical
16
{
17 2
    public function set(string $alias, ArrayAccess &$session, string $errorMessage, string $font = '/usr/share/fonts/truetype/freefont/FreeMono.ttf'): AGraphical
18
    {
19 2
        $this->font = $font;
20
21 2
        $num1 = mt_rand(0, 9);
22 2
        $num2 = mt_rand(0, 9);
23 2
        $text = ' ' . $num1 . ' + ' . $num2 . ' =';
24
25 2
        $this->setEntry($alias, null, $text);
26 2
        $this->fillSession($alias, $session, strval($num1 + $num2));
27 2
        $this->setAttribute('id', $this->getKey());
28 2
        parent::addRule(IRules::SATISFIES_CALLBACK, $errorMessage, [$this, 'checkFillCaptcha']);
29 2
        return $this;
30
    }
31
32
    public function addRule(/** @scrutinizer ignore-unused */ string $ruleName, /** @scrutinizer ignore-unused */ string $errorText, /** @scrutinizer ignore-unused */ ...$args): void
33
    {
34
        // no additional rules applicable
35
    }
36
37
    /**
38
     * @param mixed $value
39
     * @return bool
40
     */
41
    public function checkFillCaptcha($value): bool
42
    {
43
        $formName = $this->getKey() . '_last';
44
        return $this->session->offsetExists($formName) && (intval($this->session->offsetGet($formName)) == intval($value));
45
    }
46
47
    public function renderLabel($attributes = null): string
48
    {
49
        return '';
50
    }
51
52
    public function renderInput($attributes = null): string
53
    {
54
        return parent::renderLabel() . ' '. parent::renderInput($attributes);
55
    }
56
}
57