Passed
Push — 5.x ( 65a76f...3a1e20 )
by Enjoys
59s queued 13s
created

Captcha::renderHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
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\Captcha\CaptchaBase;
8
use Enjoys\Forms\Captcha\CaptchaInterface;
9
use Enjoys\Forms\Element;
10
use Enjoys\Forms\Exception\ExceptionRule;
11
use Enjoys\Forms\Interfaces\Descriptionable;
12
use Enjoys\Forms\Interfaces\Ruleable;
13
use Enjoys\Forms\Rules;
14
use Enjoys\Forms\Traits;
15
16
class Captcha extends Element implements Ruleable, Descriptionable
17
{
18
    use Traits\Description;
19
    use Traits\Rules;
20
21
22
    /**
23
     * @param CaptchaInterface&CaptchaBase $captcha
24
     * @param string|null $message
25
     * @throws ExceptionRule
26
     */
27 19
    public function __construct(private CaptchaInterface $captcha, string $message = null)
28
    {
29 19
        parent::__construct(\uniqid('captcha'));
30 19
        $this->setName($this->captcha->getName());
31
    }
32
33 12
    public function prepare()
34
    {
35 12
        $this->captcha->setRequest($this->getRequest());
0 ignored issues
show
Bug introduced by
The method setRequest() does not exist on Enjoys\Forms\Captcha\CaptchaInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Enjoys\Forms\Captcha\CaptchaInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        $this->captcha->/** @scrutinizer ignore-call */ 
36
                        setRequest($this->getRequest());
Loading history...
36 12
        $this->addRule(Rules::CAPTCHA, $this->captcha->getRuleMessage());
37
    }
38
39
    /**
40
     *
41
     * @return string
42
     */
43 9
    public function renderHtml(): string
44
    {
45 9
        return $this->captcha->renderHtml($this);
46
    }
47
48
    /**
49
     *
50
     * @return bool
51
     */
52 7
    public function validate(): bool
53
    {
54 7
        return $this->captcha->validate($this);
55
    }
56
57
58
    public function baseHtml(): string
59
    {
60
        return $this->renderHtml();
61
    }
62
63
    /**
64
     * @return CaptchaBase&CaptchaInterface|CaptchaInterface
65
     */
66 1
    public function getCaptcha()
67
    {
68 1
        return $this->captcha;
69
    }
70
}
71