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

Numerical   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 52.63%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 40
ccs 10
cts 19
cp 0.5263
rs 10
c 1
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A renderInput() 0 3 1
A renderLabel() 0 3 1
A set() 0 13 1
A checkFillCaptcha() 0 4 2
A addRule() 0 2 1
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