Factory::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
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_forms\Controls;
8
use kalanis\kw_forms\Interfaces\ITimeout;
9
10
11
/**
12
 * Class Factory
13
 * @package kalanis\kw_forms\Controls\Security\Captcha
14
 * Select captcha for displaying and processing (mobile, PC)
15
 * Valid captcha can return true even after a little time (not need to draw it again)
16
 */
17
class Factory
18
{
19
    public const TYPE_DISABLED = 1;
20
    public const TYPE_TEXT = 2;
21
    public const TYPE_MATH = 3;
22
    public const TYPE_COLOUR = 4;
23
    public const TYPE_NOCAPTCHA = 5;
24
25
    protected ?ITimeout $libTimeout = null;
26
    protected string $captchaError = 'The CAPTCHA wasn\'t entered correctly. Please try it again.';
27
28 6
    public function __construct(ITimeout $libTimeout = null, string $captchaError = '')
29
    {
30 6
        $this->libTimeout = $libTimeout;
31 6
        $this->captchaError = empty($captchaError) ? $this->captchaError : $captchaError ;
32 6
    }
33
34 6
    public function getCaptcha(int $type, ArrayAccess &$session, string $alias = 'captcha'): ACaptcha
35
    {
36
        switch ($type) {
37 6
            case static::TYPE_DISABLED:
38 1
                $captcha = new Controls\Security\Captcha\Disabled();
39 1
                $captcha->setEntry($alias);
40 1
                $captcha->setTimeout($this->libTimeout);
41 1
                return $captcha;
42 5
            case static::TYPE_TEXT:
43 1
                $captcha = new Controls\Security\Captcha\Text();
44 1
                $captcha->set($alias, $session, $this->captchaError);
45 1
                $captcha->setTimeout($this->libTimeout);
46 1
                return $captcha;
47 4
            case static::TYPE_MATH:
48 1
                $captcha = new Controls\Security\Captcha\Numerical();
49 1
                $captcha->set($alias, $session, $this->captchaError);
50 1
                $captcha->setTimeout($this->libTimeout);
51 1
                return $captcha;
52 3
            case static::TYPE_COLOUR:
53 1
                $captcha = new Controls\Security\Captcha\ColourfulText();
54 1
                $captcha->set($alias, $session, $this->captchaError);
55 1
                $captcha->setTimeout($this->libTimeout);
56 1
                return $captcha;
57 2
            case static::TYPE_NOCAPTCHA:
58 1
                $captcha = new Controls\Security\Captcha\NoCaptcha();
59 1
                $captcha->set($alias, $this->captchaError);
60 1
                $captcha->setTimeout($this->libTimeout);
61 1
                return $captcha;
62
            default:
63 1
                $captcha = new Controls\Security\Captcha\Text();
64 1
                $captcha->set($alias, $session, $this->captchaError);
65 1
                $captcha->setTimeout($this->libTimeout);
66 1
                return $captcha;
67
        }
68
    }
69
}
70