Completed
Push — master ( 35bfed...a5f27e )
by Adam
05:07
created

Message::getMessage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

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