Completed
Push — master ( 5a2ee7...b08e77 )
by Jakub
02:05
created

NewChatMessageFormFactory::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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