Test Failed
Pull Request — master (#5)
by
unknown
05:11
created

FlashNotifier::important()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * TFlashMessages.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:FlashMessages!
9
 * @subpackage     common
10
 * @since          1.0.0
11
 *
12
 * @date           01.02.15
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\FlashMessages;
18
19
use Nette;
20
use Nette\Localization;
21
22
use Kdyby\Translation;
23
24
use IPub\FlashMessages\Adapters;
25
use IPub\FlashMessages\Entities;
26
use IPub\FlashMessages\Storage;
27
28
/**
29
 * Flash message notifier
30
 *
31
 * @package        iPublikuj:FlashMessages!
32
 * @subpackage     common
33
 *
34
 * @author         Adam Kadlec <[email protected]>
35
 */
36 1
class FlashNotifier
37
{
38
	/**
39
	 * Implement nette smart magic
40
	 */
41 1
	use Nette\SmartObject;
42
43
	/**
44
	 * @var Storage\IStorage
45
	 */
46
	protected $storage;
47
48
	/**
49
	 * @var bool
50
	 */
51
	protected $useTranslator;
52
53
	/**
54
	 * @var Localization\ITranslator|NULL
55
	 */
56
	protected $translator;
57
58
	/**
59
	 * @param bool $useTranslator
60
	 * @param Storage\IStorage $storage
61
	 * @param Localization\ITranslator|NULL $translator
62
	 */
63
	public function __construct(
64
		bool $useTranslator = TRUE,
65
		Storage\IStorage $storage,
66
		Localization\ITranslator $translator = NULL
67
	) {
68
		$this->storage = $storage;
69
		$this->translator = $translator;
70
		$this->useTranslator = $useTranslator;
71
	}
72
73
	/**
74
	 * Flash a success message
75
	 *
76
	 * @param string $message
77
	 * @param string|NULL $title
78
	 *
79
	 * @return Entities\IMessage
80
	 */
81 View Code Duplication
	public function success($message, $title = NULL) : Entities\IMessage
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
	{
83
		$args = func_get_args();
84
		array_splice($args, 1, 0, [Entities\IMessage::LEVEL_SUCCESS]);
85
86
		return call_user_func_array([$this, 'setMessage'], $args);
87
	}
88
89
	/**
90
	 * Flash an information message
91
	 *
92
	 * @param string $message
93
	 * @param string|NULL $title
94
	 *
95
	 * @return Entities\IMessage
96
	 */
97 View Code Duplication
	public function info($message, $title = NULL) : Entities\IMessage
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
	{
99
		$args = func_get_args();
100
		array_splice($args, 1, 0, [Entities\IMessage::LEVEL_INFO]);
101
102
		return call_user_func_array([$this, 'setMessage'], $args);
103
	}
104
105
	/**
106
	 * Flash a warning message
107
	 *
108
	 * @param string $message
109
	 * @param string|NULL $title
110
	 *
111
	 * @return Entities\IMessage
112
	 */
113 View Code Duplication
	public function warning($message, $title = NULL) : Entities\IMessage
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
	{
115
		$args = func_get_args();
116
		array_splice($args, 1, 0, [Entities\IMessage::LEVEL_WARNING]);
117
118
		return call_user_func_array([$this, 'setMessage'], $args);
119
	}
120
121
	/**
122
	 * Flash an error message
123
	 *
124
	 * @param string $message
125
	 * @param string|NULL $title
126
	 *
127
	 * @return Entities\IMessage
128
	 */
129 View Code Duplication
	public function error($message, $title = NULL) : Entities\IMessage
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
	{
131
		$args = func_get_args();
132
		array_splice($args, 1, 0, [Entities\IMessage::LEVEL_ERROR]);
133
134
		return call_user_func_array([$this, 'setMessage'], $args);
135
	}
136
137
	/**
138
	 * Add an "important" flash to the session
139
	 *
140
	 * @return void
141
	 */
142
	public function important() : void
143
	{
144
		$this->storage->set(Storage\IStorage::KEY_IMPORTANT, TRUE);
145
	}
146
147
	/**
148
	 * Flash an overlay modal
149
	 *
150
	 * @param string $message
151
	 * @param string|NULL $title
152
	 *
153
	 * @return Entities\IMessage
154
	 */
155
	public function overlay($message, $title = NULL) : Entities\IMessage
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
	{
157
		$args = func_get_args();
158
159
		$level = $args[1];
160
161
		if (is_string($level) === FALSE || $level === NULL
162
			|| !in_array($level, [Entities\IMessage::LEVEL_ERROR, Entities\IMessage::LEVEL_INFO, Entities\IMessage::LEVEL_SUCCESS, Entities\IMessage::LEVEL_WARNING])
163
		) {
164
			array_splice($args, 1, 0, [Entities\IMessage::LEVEL_INFO]);
165
		}
166
167
		array_splice($args, 3, 0, [TRUE]);
168
169
		return call_user_func_array([$this, 'setMessage'], $args);
170
	}
171
172
	/**
173
	 * @param string $message
174
	 * @param string $level
175
	 * @param string|NULL $title
176
	 * @param bool $overlay
177
	 * @param int|NULL $count
178
	 * @param array $parameters
179
	 *
180
	 * @return Entities\IMessage
181
	 */
182
	public function message($message, $level = Entities\IMessage::LEVEL_INFO, $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) : Entities\IMessage
183
	{
184
		return $this->setMessage($message, $level, $title, $overlay, $count, $parameters);
185
	}
186
187
	/**
188
	 * Flash a general message
189
	 *
190
	 * @param string $message
191
	 * @param string $level
192
	 * @param string|NULL $title
193
	 * @param bool $overlay
194
	 * @param int|NULL $count
195
	 * @param array $parameters
196
	 *
197
	 * @return Entities\IMessage
198
	 */
199
	public function setMessage($message, $level = Entities\IMessage::LEVEL_INFO, $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) : Entities\IMessage
0 ignored issues
show
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $overlay is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $count is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
200
	{
201
		$args = func_get_args();
202
203
		// Remove message
204
		unset($args[0]);
205
		// Remove level
206
		unset($args[1]);
207
208
		$title = $this->checkForAttribute($args, 'title', NULL);
209
		$overlay = $this->checkForAttribute($args, 'overlay', FALSE);
210
		$count = $this->checkForAttribute($args, 'count', NULL);
211
		$parameters = $this->checkForAttribute($args, 'parameters', []);
212
213
		// Support for Kdyby/Translation
214
		if ($message instanceof Translation\Phrase) {
0 ignored issues
show
Bug introduced by
The class Kdyby\Translation\Phrase does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
215
			$phrase = new Adapters\KdybyPhraseAdapter($message);
216
217
		// Default phrase adapter
218
		} else if (!$message instanceof Adapters\IPhraseAdapter) {
219
			$phrase = new Adapters\DefaultPhraseAdapter($message, $count, $parameters);
220
221
		} else {
222
			$phrase = $message;
223
		}
224
225
		// Support for Kdyby/Translation
226
		if ($title instanceof Translation\Phrase) {
0 ignored issues
show
Bug introduced by
The class Kdyby\Translation\Phrase does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
227
			$titlePhrase = new Adapters\KdybyPhraseAdapter($title);
228
229
		// Default phrase adapter
230
		} else if (!$title instanceof Adapters\IPhraseAdapter && $title !== NULL) {
231
			$titlePhrase = new Adapters\DefaultPhraseAdapter($title, $count, $parameters);
232
233
		} else {
234
			$titlePhrase = NULL;
235
		}
236
237
		// Get all stored messages
238
		$messages = $this->storage->get(Storage\IStorage::KEY_MESSAGES, []);
239
240
		// Create flash message
241
		$flash = new Entities\Message(($this->useTranslator ? $this->translator : NULL), $phrase, $titlePhrase);
242
		$flash->setLevel($level);
243
		$flash->setOverlay($overlay);
244
245
		if (!$this->useTranslator || !$this->translator instanceof Localization\ITranslator) {
246
			if (is_string($message) === TRUE) {
247
				$flash->setMessage($message);
248
			}
249
250
			if (is_string($title) === TRUE) {
251
				$flash->setTitle((string) $title);
252
			}
253
		}
254
255
		if ($this->checkUnique($flash, $messages) === FALSE) {
256
			$messages[] = $flash;
257
		}
258
259
		// Store messages in session
260
		$this->storage->set(Storage\IStorage::KEY_MESSAGES, $messages);
261
262
		return $flash;
263
	}
264
265
	/**
266
	 * @param Entities\IMessage $flash
267
	 * @param Entities\IMessage[] $messages
268
	 *
269
	 * @return bool
270
	 */
271
	private function checkUnique(Entities\IMessage $flash, array $messages) : bool
272
	{
273
		foreach ($messages as $member) {
274
			if ((string) $member === (string) $flash && !$member->isDisplayed()) {
275
				return TRUE;
276
			}
277
		}
278
279
		return FALSE;
280
	}
281
282
	/**
283
	 * @param array $attributes
284
	 * @param string $type
285
	 * @param mixed $default
286
	 *
287
	 * @return mixed
288
	 */
289
	private function checkForAttribute(array $attributes, string $type, $default)
290
	{
291
		foreach($attributes as $attribute) {
292
			switch($type)
293
			{
294
				case 'title':
295
					if (is_string($attribute) === TRUE || $attribute instanceof Translation\Phrase || $attribute instanceof Adapters\IPhraseAdapter) {
0 ignored issues
show
Bug introduced by
The class Kdyby\Translation\Phrase does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
296
						return $attribute;
297
					}
298
					break;
299
300
				case 'overlay':
301
					if (is_bool($attribute) === TRUE) {
302
						return $attribute;
303
					}
304
					break;
305
306
				case 'count':
307
					if (is_numeric($attribute) === TRUE) {
308
						return $attribute;
309
					}
310
					break;
311
312
				case 'parameters':
313
					if (is_array($attribute) === TRUE) {
314
						return $attribute;
315
					}
316
					break;
317
			}
318
		}
319
320
		// Return default
321
		return $default;
322
	}
323
}
324