Completed
Push — master ( 664709...70cc89 )
by Adam
05:40
created

Message::hasOverlay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Message.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:FlashMessages!
9
 * @subpackage     Entities
10
 * @since          1.0.0
11
 *
12
 * @date           06.02.15
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\FlashMessages\Entities;
18
19
use Nette;
20
use Nette\Localization;
21
22
use IPub;
23
use IPub\FlashMessages\Adapters;
24
use IPub\FlashMessages\Exceptions;
25
26
/**
27
 * Flash message entity
28
 *
29
 * @package        iPublikuj:FlashMessages!
30
 * @subpackage     Entities
31
 *
32
 * @author         Adam Kadlec <[email protected]>
33
 */
34 1
class Message extends Nette\Object implements IMessage
35
{
36
	/**
37
	 * @var string
38
	 */
39
	private $message;
40
41
	/**
42
	 * @var string
43
	 */
44
	private $level;
45
46
	/**
47
	 * @var string
48
	 */
49
	private $title;
50
51
	/**
52
	 * @var bool
53
	 */
54
	private $overlay = FALSE;
55
56
	/**
57
	 * @var bool
58
	 */
59
	private $displayed = FALSE;
60
61
	/**
62
	 * @var Localization\ITranslator
63
	 */
64
	private $translator;
65
66
	/**
67
	 * @var Adapters\IPhraseAdapter
68
	 */
69
	private $phraseAdapter;
70
71
	/**
72
	 * @var Adapters\IPhraseAdapter
73
	 */
74
	private $titlePhraseAdapter;
75
76
	/**
77
	 * @param Localization\ITranslator $translator
78
	 * @param Adapters\IPhraseAdapter $phraseAdapter
79
	 * @param Adapters\IPhraseAdapter $titlePhraseAdapter
80
	 */
81
	public function __construct(
82
		Localization\ITranslator $translator = NULL,
83
		Adapters\IPhraseAdapter $phraseAdapter,
84
		Adapters\IPhraseAdapter $titlePhraseAdapter = NULL
85
	) {
86 1
		$this->translator = $translator;
87 1
		$this->phraseAdapter = $phraseAdapter;
88 1
		$this->titlePhraseAdapter = $titlePhraseAdapter;
89 1
	}
90
91
	/**
92
	 * {@inheritdoc}
93
	 */
94
	public function setMessage(string $message)
95
	{
96 1
		if ($this->isUnserialized()) {
97 1
			$this->message = $message;
98
99
		} else {
100
			$this->phraseAdapter->setMessage($message);
101
			$this->message = NULL;
102
		}
103 1
	}
104
105
	/**
106
	 * {@inheritdoc}
107
	 */
108
	public function getMessage() : string
109
	{
110 1
		if ($this->message === NULL && $this->translator) {
111 1
			$this->message = $this->phraseAdapter->translate($this->translator);
112
		}
113
114 1
		return $this->message;
115
	}
116
117
	/**
118
	 * {@inheritdoc}
119
	 */
120
	public function setLevel(string $level)
121
	{
122 1
		$this->level = $level;
123 1
	}
124
125
	/**
126
	 * {@inheritdoc}
127
	 */
128
	public function getLevel() : string
129
	{
130 1
		return $this->level;
131
	}
132
133
	/**
134
	 * {@inheritdoc}
135
	 */
136
	public function info()
137
	{
138
		$this->setLevel(self::LEVEL_INFO);
139
	}
140
141
	/**
142
	 * {@inheritdoc}
143
	 */
144
	public function success()
145
	{
146
		$this->setLevel(self::LEVEL_SUCCESS);
147
	}
148
149
	/**
150
	 * {@inheritdoc}
151
	 */
152
	public function warning()
153
	{
154
		$this->setLevel(self::LEVEL_WARNING);
155
	}
156
157
	/**
158
	 * {@inheritdoc}
159
	 */
160
	public function error()
161
	{
162
		$this->setLevel(self::LEVEL_ERROR);
163
	}
164
165
	/**
166
	 * {@inheritdoc}
167
	 */
168
	public function setTitle(string $title = NULL)
169
	{
170 1
		if ($this->isUnserialized()) {
171 1
			$this->title = $title;
172
173
		} else {
174
			if ($this->titlePhraseAdapter instanceof Adapters\IPhraseAdapter) {
175
				$this->titlePhraseAdapter->setMessage($title);
176
			}
177
			$this->title = NULL;
178
		}
179 1
	}
180
181
	/**
182
	 * {@inheritdoc}
183
	 */
184
	public function getTitle()
185
	{
186 1
		if ($this->title === NULL && $this->translator && $this->titlePhraseAdapter instanceof Adapters\IPhraseAdapter) {
187 1
			$this->title = $this->titlePhraseAdapter->translate($this->translator);
188
		}
189
190 1
		return $this->title;
191
	}
192
193
	/**
194
	 * {@inheritdoc}
195
	 */
196
	public function setOverlay(bool $overlay)
197
	{
198 1
		$this->overlay = $overlay;
199 1
	}
200
201
	/**
202
	 * {@inheritdoc}
203
	 */
204
	public function hasOverlay() : bool
205
	{
206 1
		return $this->overlay;
207
	}
208
209
	/**
210
	 * {@inheritdoc}
211
	 */
212
	public function setParameters(array $parameter)
213
	{
214
		$this->validateState(__FUNCTION__);
215
		$this->phraseAdapter->setParameters($parameter);
216
		$this->message = NULL;
217
	}
218
219
	/**
220
	 * {@inheritdoc}
221
	 */
222
	public function setCount(int $count)
223
	{
224
		$this->validateState(__FUNCTION__);
225
		$this->phraseAdapter->setCount($count);
226
		$this->message = NULL;
227
	}
228
229
	/**
230
	 * {@inheritdoc}
231
	 */
232
	public function setDisplayed(bool $displayed = TRUE)
233
	{
234 1
		$this->displayed = $displayed;
235 1
	}
236
237
	/**
238
	 * {@inheritdoc}
239
	 */
240
	public function isDisplayed() : bool
241
	{
242 1
		return $this->displayed;
243
	}
244
245
	/**
246
	 * @param string $method
247
	 *
248
	 * @throws Exceptions\InvalidStateException
249
	 */
250
	private function validateState(string $method)
251
	{
252
		if ($this->isUnserialized()) {
253
			throw new Exceptions\InvalidStateException(sprintf('You cannot call method %s on unserialized Entities\Message object', $method));
254
		}
255
	}
256
257
	/**
258
	 * @return bool
259
	 */
260
	private function isUnserialized() : bool
261
	{
262 1
		return $this->translator === NULL;
263
	}
264
265
	/**
266
	 * @return string
267
	 */
268
	public function __toString()
269
	{
270 1
		return $this->level . ' ' . ($this->title ? $this->title . ' ' : '') . $this->getMessage();
271
	}
272
273
	/**
274
	 * @return array
275
	 */
276
	public function __sleep()
277
	{
278
		$this->message = $this->getMessage();
279
280
		return ['message', 'level', 'title', 'overlay', 'displayed'];
281
	}
282
}
283