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

NewChatMessageFormFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1.0202

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 13
ccs 8
cts 11
cp 0.7272
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1.0202
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
?>