Passed
Push — master ( 152012...15afa8 )
by Jakub
01:52
created

NewChatMessageFormFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 3
eloc 17
dl 0
loc 26
ccs 15
cts 18
cp 0.8333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A create() 0 13 1
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 1
    if(is_null($translator)) {
22 1
      $loader = new NeonLoader();
23 1
      $loader->folders = [__DIR__ . "/lang"];
24 1
      $translator = new Translator($loader);
25
    }
26 1
    $this->translator = $translator;
27 1
  }
28
  
29
  public function create(ChatControl $chatControl): Form {
30 1
    $form = new Form();
31 1
    $form->setTranslator($this->translator);
32 1
    $form->addText("message", "")
33 1
      ->setRequired("chat.newMessageForm.messageField.empty");
34 1
    $form->addSubmit("send", "chat.newMessageForm.submitButton.label");
35 1
    $form->addComponent($chatControl, "chat");
36 1
    $form->onSuccess[] = function(Form $form, array $values) {
37
      /** @var ChatControl $chat */
38
      $chat = $form->getComponent("chat");
39
      $chat->newMessage($values["message"]);
40
    };
41 1
    return $form;
42
  }
43
}
44
?>