Passed
Push — master ( f212df...380228 )
by Jakub
01:49
created

ChatControl::setTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Chat;
5
6
/**
7
 * Basic Chat Control
8
 *
9
 * @author Jakub Konečný
10
 * @property int $messagesPerPage
11
 * @property string $characterProfileLink
12
 * @property-read \Nette\Bridges\ApplicationLatte\Template $template
13
 */
14 1
abstract class ChatControl extends \Nette\Application\UI\Control {
15
  /** @var IDatabaseAdapter */
16
  protected $database;
17
  /** @var IChatMessageProcessor[] */
18
  protected $messageProcessors = [];
19
  /** @var string*/
20
  protected $textColumn;
21
  /** @var string */
22
  protected $characterColumn;
23
  /** @var int*/
24
  protected $textValue;
25
  /** @var int */
26
  protected $characterValue;
27
  /** @var string */
28
  protected $characterProfileLink = "";
29
  /** @var string */
30
  protected $templateFile = __DIR__ . "/chat.latte";
31
  /** @var int */
32
  protected $messagesPerPage = 25;
33
34
  /**
35
   * @param mixed $characterValue
36
   */
37
  public function __construct(IDatabaseAdapter $databaseAdapter, string $textColumn, int $textValue, string $characterColumn = null, $characterValue = null) {
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 158 characters
Loading history...
38 1
    $this->database = $databaseAdapter;
39 1
    $this->textColumn = $textColumn;
40 1
    $this->characterColumn = $characterColumn ?? $textColumn;
41 1
    $this->textValue = $textValue;
42 1
    $this->characterValue = $characterValue ?? $textValue;
43 1
  }
44
45
  public function getMessagesPerPage(): int {
46 1
    return $this->messagesPerPage;
47
  }
48
  
49
  public function setMessagesPerPage(int $messagesPerPage): void {
50 1
    if($messagesPerPage < 0) {
51 1
      $messagesPerPage = 0;
52
    }
53 1
    $this->messagesPerPage = $messagesPerPage;
54 1
  }
55
  
56
  public function getCharacterProfileLink(): string {
57 1
    return $this->characterProfileLink;
58
  }
59
  
60
  public function setCharacterProfileLink(string $characterProfileLink): void {
61 1
    $this->characterProfileLink = $characterProfileLink;
62 1
  }
63
  
64
  public function addMessageProcessor(IChatMessageProcessor $processor): void {
65 1
    $this->messageProcessors[] = $processor;
66 1
  }
67
  
68
  /**
69
   * Gets texts for the current chat
70
   */
71
  public function getTexts(): ChatMessagesCollection {
72 1
    return $this->database->getTexts($this->textColumn, $this->textValue, $this->messagesPerPage);
73
  }
74
  
75
  /**
76
   * Gets characters in the current chat
77
   */
78
  public function getCharacters(): ChatCharactersCollection {
79 1
    return $this->database->getCharacters($this->characterColumn, $this->characterValue);
80
  }
81
82
  /**
83
   * Renders the chat
84
   */
85
  public function render(): void {
86 1
    $this->template->setFile($this->templateFile);
87 1
    $this->template->characters = $this->getCharacters();
88 1
    $this->template->texts = $this->getTexts();
89 1
    $this->template->characterProfileLink = $this->characterProfileLink;
90 1
    $this->template->render();
91 1
  }
92
  
93
  protected function processMessage(string $message): ?string {
94
    foreach($this->messageProcessors as $processor) {
95
      $result = $processor->parse($message);
96
      if(is_string($result)) {
97
        return $result;
98
      }
99
    }
100
    return null;
101
  }
102
  
103
  /**
104
   * Submits new message
105
   */
106
  public function newMessage(string $message): void {
107
    $result = $this->processMessage($message);
108
    if(!is_null($result)) {
109
      $this->presenter->flashMessage($result);
110
    } else {
111
      $this->database->addMessage($message, $this->textColumn, $this->textValue);
112
    }
113
    $this->presenter->redirect("this");
114
  }
115
}
116
?>