Completed
Push — master ( 63f783...d7278c )
by Jakub
08:07
created

ChatControl   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 74.51%

Importance

Changes 0
Metric Value
wmc 22
eloc 55
dl 0
loc 134
ccs 38
cts 51
cp 0.7451
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A addMessageProcessor() 0 2 1
A setCharacterProfileLink() 0 2 1
A render() 0 7 1
A setMessagesPerPage() 0 5 2
A getCharacterProfileLink() 0 2 1
A getMessagesPerPage() 0 2 1
A getLang() 0 2 1
A setLang() 0 2 1
A getTexts() 0 2 1
A getTranslator() 0 2 1
A processMessage() 0 8 3
A setupTranslator() 0 10 3
A getCharacters() 0 2 1
A __construct() 0 7 1
A newMessage() 0 8 2
A setTranslator() 0 2 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Chat;
5
6
use Nette\Localization\ITranslator;
7
use Nexendrie\Translation\Translator;
8
use Nexendrie\Translation\Loaders\NeonLoader;
9
10
/**
11
 * Basic Chat Control
12
 *
13
 * @author Jakub Konečný
14
 * @property ITranslator $translator
15
 * @property string $lang
16
 * @property int $messagesPerPage
17
 * @property string $characterProfileLink
18
 * @property-read \Nette\Bridges\ApplicationLatte\Template $template
19
 */
20 1
abstract class ChatControl extends \Nette\Application\UI\Control {
21
  /** @var IDatabaseAdapter */
22
  protected $database;
23
  /** @var ITranslator|Translator|null */
24
  protected $translator;
25
  /** @var string */
26
  protected $lang = "";
27
  /** @var IChatMessageProcessor[] */
28
  protected $messageProcessors = [];
29
  /** @var string*/
30
  protected $textColumn;
31
  /** @var string */
32
  protected $characterColumn;
33
  /** @var int*/
34
  protected $textValue;
35
  /** @var int */
36
  protected $characterValue;
37
  /** @var string */
38
  protected $characterProfileLink = "";
39
  /** @var string */
40
  protected $templateFile = __DIR__ . "/chat.latte";
41
  /** @var int */
42
  protected $messagesPerPage = 25;
43
44
  /**
45
   * @param mixed $characterValue
46
   */
47
  public function __construct(IDatabaseAdapter $databaseAdapter, string $textColumn, int $textValue, string $characterColumn = null, $characterValue = null, ITranslator $translator = null) {
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 190 characters
Loading history...
48 1
    $this->database = $databaseAdapter;
49 1
    $this->translator = $translator;
50 1
    $this->textColumn = $textColumn;
51 1
    $this->characterColumn = $characterColumn ?? $textColumn;
52 1
    $this->textValue = $textValue;
53 1
    $this->characterValue = $characterValue ?? $textValue;
54 1
  }
55
  
56
  public function getTranslator(): ?ITranslator {
57 1
    return $this->translator;
58
  }
59
  
60
  public function setTranslator(ITranslator $translator): void {
61 1
    $this->translator = $translator;
62 1
  }
63
  
64
  public function getLang(): string {
65 1
    return $this->lang;
66
  }
67
  
68
  public function setLang(string $lang): void {
69 1
    $this->lang = $lang;
70 1
  }
71
  
72
  public function getMessagesPerPage(): int {
73 1
    return $this->messagesPerPage;
74
  }
75
  
76
  public function setMessagesPerPage(int $messagesPerPage): void {
77 1
    if($messagesPerPage < 0) {
78 1
      $messagesPerPage = 0;
79
    }
80 1
    $this->messagesPerPage = $messagesPerPage;
81 1
  }
82
  
83
  public function getCharacterProfileLink(): string {
84 1
    return $this->characterProfileLink;
85
  }
86
  
87
  public function setCharacterProfileLink(string $characterProfileLink): void {
88 1
    $this->characterProfileLink = $characterProfileLink;
89 1
  }
90
  
91
  public function addMessageProcessor(IChatMessageProcessor $processor): void {
92 1
    $this->messageProcessors[] = $processor;
93 1
  }
94
  
95
  /**
96
   * Gets texts for the current chat
97
   */
98
  public function getTexts(): ChatMessagesCollection {
99 1
    return $this->database->getTexts($this->textColumn, $this->textValue, $this->messagesPerPage);
100
  }
101
  
102
  /**
103
   * Gets characters in the current chat
104
   */
105
  public function getCharacters(): ChatCharactersCollection {
106 1
    return $this->database->getCharacters($this->characterColumn, $this->characterValue);
107
  }
108
  
109
  protected function setupTranslator(): void {
110 1
    if(is_null($this->translator)) {
111
      $loader = new NeonLoader();
112
      $loader->folders = [__DIR__ . "/lang"];
113
      $this->translator = new Translator($loader);
114
    }
115 1
    if($this->lang !== "") {
116 1
      $this->translator->lang = $this->lang;
117
    }
118 1
    $this->template->setTranslator($this->translator);
119 1
  }
120
  
121
  /**
122
   * Renders the chat
123
   */
124
  public function render(): void {
125 1
    $this->setupTranslator();
126 1
    $this->template->setFile($this->templateFile);
127 1
    $this->template->characters = $this->getCharacters();
128 1
    $this->template->texts = $this->getTexts();
129 1
    $this->template->characterProfileLink = $this->characterProfileLink;
130 1
    $this->template->render();
131 1
  }
132
  
133
  protected function processMessage(string $message): ?string {
134
    foreach($this->messageProcessors as $processor) {
135
      $result = $processor->parse($message);
136
      if(is_string($result)) {
137
        return $result;
138
      }
139
    }
140
    return null;
141
  }
142
  
143
  /**
144
   * Submits new message
145
   */
146
  public function newMessage(string $message): void {
147
    $result = $this->processMessage($message);
148
    if(!is_null($result)) {
149
      $this->presenter->flashMessage($result);
150
    } else {
151
      $this->database->addMessage($message, $this->textColumn, $this->textValue);
152
    }
153
    $this->presenter->redirect("this");
154
  }
155
}
156
?>