NewChatMessageFormFactory::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 10
c 5
b 0
f 1
dl 0
loc 15
ccs 0
cts 11
cp 0
rs 9.9332
cc 1
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Chat;
5
6
use Nette\Application\UI\Form;
7
use Nette\Localization\Translator;
8
9
/**
10
 * NewChatMessageFormFactory
11
 *
12
 * @author Jakub Konečný
13
 */
14
final class NewChatMessageFormFactory
15
{
16
    public function __construct(private readonly Translator $translator)
17
    {
18
    }
19
20
    public function create(ChatControl $chatControl): Form
21
    {
22
        $form = new Form();
23
        $form->setTranslator($this->translator);
24
        $form->addText("message", "")
25
            ->setRequired("chat.newMessageForm.messageField.empty");
26
        $form->addSubmit("send", "chat.newMessageForm.submitButton.label");
27
        $form->addComponent($chatControl, "chat");
28
        // @phpstan-ignore assign.propertyType
29
        $form->onSuccess[] = function (Form $form, array $values): void {
30
            /** @var ChatControl $chat */
31
            $chat = $form->getComponent("chat");
32
            $chat->newMessage($values["message"]);
33
        };
34
        return $form;
35
    }
36
}
37