Passed
Push — 5.x ( 3a1e20...1630b1 )
by Enjoys
02:51
created

Captcha::getCaptcha()   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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Elements;
6
7
use Enjoys\Forms\Element;
8
use Enjoys\Forms\Interfaces\CaptchaInterface;
9
use Enjoys\Forms\Interfaces\Descriptionable;
10
use Enjoys\Forms\Interfaces\Ruleable;
11
use Enjoys\Forms\Rules;
12
use Enjoys\Forms\Traits;
13
14
class Captcha extends Element implements Ruleable, Descriptionable
15
{
16
    use Traits\Description;
17
    use Traits\Rules;
18
19
20
    /**
21
     * @param CaptchaInterface $captcha
22
     * @param string|null $message
23
     */
24
    public function __construct(private CaptchaInterface $captcha, string $message = null)
25
    {
26
        parent::__construct(\uniqid('captcha'));
27
        $this->setName($this->captcha->getName());
28
    }
29
30
    public function prepare()
31
    {
32
        $this->captcha->setRequest($this->getRequest());
33
        $this->addRule(Rules::CAPTCHA, $this->captcha->getRuleMessage());
34
    }
35
36
    /**
37
     *
38
     * @return string
39
     */
40
    public function renderHtml(): string
41
    {
42
        return $this->captcha->renderHtml($this);
43
    }
44
45
    /**
46
     *
47
     * @return bool
48
     */
49
    public function validate(): bool
50
    {
51
        return $this->captcha->validate($this);
52
    }
53
54
55
    public function baseHtml(): string
56
    {
57
        return $this->renderHtml();
58
    }
59
60
    public function getCaptcha(): CaptchaInterface
61
    {
62
        return $this->captcha;
63
    }
64
}
65