Completed
Push — master ( 97ebcd...f67939 )
by Jakub
03:22
created

ChatControl::getCharacterProfileLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Chat;
5
6
use Nette\Localization\ITranslator,
7
    Nexendrie\Translation\Translator,
8
    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
  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...
45 1
    parent::__construct();
46 1
    $this->database = $databaseAdapter;
47 1
    $this->translator = $translator;
48 1
    $this->textColumn = $textColumn;
49 1
    $this->characterColumn = $characterColumn ?? $textColumn;
50 1
    $this->textValue = $textValue;
51 1
    $this->characterValue = $characterValue ?? $textValue;
52 1
  }
53
  
54
  public function getTranslator(): ?ITranslator {
55 1
    return $this->translator;
56
  }
57
  
58
  public function setTranslator(ITranslator $translator): void {
59 1
    $this->translator = $translator;
60 1
  }
61
  
62
  public function getLang(): string {
63 1
    return $this->lang;
64
  }
65
  
66
  public function setLang(string $lang): void {
67 1
    $this->lang = $lang;
68 1
  }
69
  
70
  public function getMessagesPerPage(): int {
71 1
    return $this->messagesPerPage;
72
  }
73
  
74
  public function setMessagesPerPage(int $messagesPerPage): void {
75 1
    if($messagesPerPage < 0) {
76 1
      $messagesPerPage = 0;
77
    }
78 1
    $this->messagesPerPage = $messagesPerPage;
79 1
  }
80
  
81
  public function getCharacterProfileLink(): string {
82 1
    return $this->characterProfileLink;
83
  }
84
  
85
  public function setCharacterProfileLink(string $characterProfileLink): void {
86 1
    $this->characterProfileLink = $characterProfileLink;
87 1
  }
88
  
89
  public function addMessageProcessor(IChatMessageProcessor $processor): void {
90 1
    $this->messageProcessors[] = $processor;
91 1
  }
92
  
93
  /**
94
   * Gets texts for the current chat
95
   */
96
  public function getTexts(): ChatMessagesCollection {
97 1
    return $this->database->getTexts($this->textColumn, $this->textValue, $this->messagesPerPage);
98
  }
99
  
100
  /**
101
   * Gets characters in the current chat
102
   */
103
  public function getCharacters(): ChatCharactersCollection {
104 1
    return $this->database->getCharacters($this->characterColumn, $this->characterValue);
105
  }
106
  
107
  protected function setupTranslator(): void {
108 1
    if(is_null($this->translator)) {
109
      $loader = new NeonLoader();
110
      $loader->folders = [__DIR__ . "/lang"];
111
      $this->translator = new Translator($loader);
112
    }
113 1
    if($this->lang !== "") {
114 1
      $this->translator->lang = $this->lang;
115
    }
116 1
    $this->template->setTranslator($this->translator);
117 1
  }
118
  
119
  /**
120
   * Renders the chat
121
   */
122
  public function render(): void {
123 1
    $this->setupTranslator();
124 1
    $this->template->setFile($this->templateFile);
125 1
    $this->template->characters = $this->getCharacters();
126 1
    $this->template->texts = $this->getTexts();
127 1
    $this->template->characterProfileLink = $this->characterProfileLink;
128 1
    $this->template->render();
129 1
  }
130
  
131
  protected function processMessage(string $message): ?string {
132
    foreach($this->messageProcessors as $processor) {
133
      $result = $processor->parse($message);
134
      if(is_string($result)) {
135
        return $result;
136
      }
137
    }
138
    return NULL;
139
  }
140
  
141
  /**
142
   * Submits new message
143
   */
144
  public function newMessage(string $message): void {
145
    $result = $this->processMessage($message);
146
    if(!is_null($result)) {
147
      $this->presenter->flashMessage($result);
148
    } else {
149
      $this->database->addMessage($message, $this->textColumn, $this->textValue);
150
    }
151
    $this->presenter->redirect("this");
152
  }
153
}
154
?>