NewChatMessageFormFactory   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A create() 0 14 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
  public function __construct(private readonly Translator $translator) {
16
  }
17
  
18
  public function create(ChatControl $chatControl): Form {
19
    $form = new Form();
20
    $form->setTranslator($this->translator);
21
    $form->addText("message", "")
22
      ->setRequired("chat.newMessageForm.messageField.empty");
23
    $form->addSubmit("send", "chat.newMessageForm.submitButton.label");
24
    $form->addComponent($chatControl, "chat");
25
    // @phpstan-ignore assign.propertyType
26
    $form->onSuccess[] = function(Form $form, array $values): void {
27
      /** @var ChatControl $chat */
28
      $chat = $form->getComponent("chat");
29
      $chat->newMessage($values["message"]);
30
    };
31
    return $form;
32
  }
33
}
34
?>