Completed
Push — master ( 15afa8...f212df )
by Jakub
03:00
created

ChatControl   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Test Coverage

Coverage 74.55%

Importance

Changes 0
Metric Value
wmc 23
eloc 59
dl 0
loc 154
ccs 41
cts 55
cp 0.7455
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 3 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 10 2
A newMessage() 0 8 2
A setTranslator() 0 3 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
    if(!is_null($translator)) {
49
      trigger_error("Passing translator to constructor of" . self::class . " is deprecated.", E_USER_DEPRECATED);
50
    }
51 1
    $this->database = $databaseAdapter;
52 1
    $this->translator = $translator;
53 1
    $this->textColumn = $textColumn;
54 1
    $this->characterColumn = $characterColumn ?? $textColumn;
55 1
    $this->textValue = $textValue;
56 1
    $this->characterValue = $characterValue ?? $textValue;
57 1
  }
58
59
  /**
60
   * @deprecated
61
   */
62
  public function getTranslator(): ?ITranslator {
63 1
    return $this->translator;
64
  }
65
66
  /**
67
   * @deprecated
68
   */
69
  public function setTranslator(ITranslator $translator): void {
70 1
    trigger_error(__METHOD__ . "() is deprecated.", E_USER_DEPRECATED);
71 1
    $this->translator = $translator;
72 1
  }
73
74
  /**
75
   * @deprecated
76
   */
77
  public function getLang(): string {
78 1
    return $this->lang;
79
  }
80
81
  /**
82
   * @deprecated
83
   */
84
  public function setLang(string $lang): void {
85 1
    trigger_error(__METHOD__ . "() is deprecated.", E_USER_DEPRECATED);
86 1
    $this->lang = $lang;
87 1
  }
88
  
89
  public function getMessagesPerPage(): int {
90 1
    return $this->messagesPerPage;
91
  }
92
  
93
  public function setMessagesPerPage(int $messagesPerPage): void {
94 1
    if($messagesPerPage < 0) {
95 1
      $messagesPerPage = 0;
96
    }
97 1
    $this->messagesPerPage = $messagesPerPage;
98 1
  }
99
  
100
  public function getCharacterProfileLink(): string {
101 1
    return $this->characterProfileLink;
102
  }
103
  
104
  public function setCharacterProfileLink(string $characterProfileLink): void {
105 1
    $this->characterProfileLink = $characterProfileLink;
106 1
  }
107
  
108
  public function addMessageProcessor(IChatMessageProcessor $processor): void {
109 1
    $this->messageProcessors[] = $processor;
110 1
  }
111
  
112
  /**
113
   * Gets texts for the current chat
114
   */
115
  public function getTexts(): ChatMessagesCollection {
116 1
    return $this->database->getTexts($this->textColumn, $this->textValue, $this->messagesPerPage);
117
  }
118
  
119
  /**
120
   * Gets characters in the current chat
121
   */
122
  public function getCharacters(): ChatCharactersCollection {
123 1
    return $this->database->getCharacters($this->characterColumn, $this->characterValue);
124
  }
125
126
  /**
127
   * @deprecated
128
   */
129
  protected function setupTranslator(): void {
130 1
    if(is_null($this->translator)) {
131
      $loader = new NeonLoader();
132
      $loader->folders = [__DIR__ . "/lang"];
133
      $this->translator = new Translator($loader);
134
    }
135 1
    if($this->lang !== "") {
136 1
      $this->translator->lang = $this->lang;
137
    }
138 1
    $this->template->setTranslator($this->translator);
139 1
  }
140
  
141
  /**
142
   * Renders the chat
143
   */
144
  public function render(): void {
145 1
    $this->setupTranslator();
0 ignored issues
show
Deprecated Code introduced by
The function HeroesofAbenez\Chat\ChatControl::setupTranslator() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

145
    /** @scrutinizer ignore-deprecated */ $this->setupTranslator();
Loading history...
146 1
    $this->template->setFile($this->templateFile);
147 1
    $this->template->characters = $this->getCharacters();
148 1
    $this->template->texts = $this->getTexts();
149 1
    $this->template->characterProfileLink = $this->characterProfileLink;
150 1
    $this->template->render();
151 1
  }
152
  
153
  protected function processMessage(string $message): ?string {
154
    foreach($this->messageProcessors as $processor) {
155
      $result = $processor->parse($message);
156
      if(is_string($result)) {
157
        return $result;
158
      }
159
    }
160
    return null;
161
  }
162
  
163
  /**
164
   * Submits new message
165
   */
166
  public function newMessage(string $message): void {
167
    $result = $this->processMessage($message);
168
    if(!is_null($result)) {
169
      $this->presenter->flashMessage($result);
170
    } else {
171
      $this->database->addMessage($message, $this->textColumn, $this->textValue);
172
    }
173
    $this->presenter->redirect("this");
174
  }
175
}
176
?>