Passed
Push — 5.x ( 1630b1...94cabc )
by Enjoys
02:36
created

Captcha::prepare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
     * @noinspection PhpMissingParentConstructorInspection
24
     */
25 6
    public function __construct(private CaptchaInterface $captcha, string $message = null)
26
    {
27 6
        $this->setRequest();
28 6
        $this->setName($this->captcha->getName());
29
    }
30
31 1
    public function prepare()
32
    {
33 1
        $this->captcha->setRequest($this->getRequest());
34 1
        $this->addRule(Rules::CAPTCHA, $this->captcha->getRuleMessage());
35
    }
36
37
    /**
38
     *
39
     * @return string
40
     */
41 1
    public function renderHtml(): string
42
    {
43 1
        return $this->captcha->renderHtml($this);
44
    }
45
46
    /**
47
     *
48
     * @return bool
49
     */
50 1
    public function validate(): bool
51
    {
52 1
        return $this->captcha->validate($this);
53
    }
54
55
56
    public function baseHtml(): string
57
    {
58
        return $this->renderHtml();
59
    }
60
61 1
    public function getCaptcha(): CaptchaInterface
62
    {
63 1
        return $this->captcha;
64
    }
65
}
66