NewChatMessageFormFactory   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 2
eloc 11
c 5
b 0
f 1
dl 0
loc 21
ccs 0
cts 13
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A create() 0 15 1
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