Test Failed
Pull Request — master (#5)
by
unknown
03:46
created

FlashNotifier::message()   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 1
cp 0
cc 1
nc 1
nop 6
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 IPub\FlashMessages\Adapters;
23
use IPub\FlashMessages\Entities;
24
use IPub\FlashMessages\Storage;
25
26
/**
27
 * Flash message notifier
28
 *
29
 * @package        iPublikuj:FlashMessages!
30
 * @subpackage     common
31
 *
32
 * @author         Adam Kadlec <[email protected]>
33
 */
34 1
class FlashNotifier
35
{
36
	/**
37
	 * Implement nette smart magic
38
	 */
39 1
	use Nette\SmartObject;
40
41
	/**
42
	 * @var Storage\IStorage
43
	 */
44
	protected $storage;
45
46
	/**
47
	 * @var bool
48
	 */
49
	protected $useTranslator;
50
51
	/**
52
	 * @var Localization\ITranslator|NULL
53
	 */
54
	protected $translator;
55
56
	/**
57
	 * @param bool $useTranslator
58
	 * @param Storage\IStorage $storage
59
	 * @param Localization\ITranslator|NULL $translator
60
	 */
61
	public function __construct(
62
		bool $useTranslator = TRUE,
63
		Storage\IStorage $storage,
64
		Localization\ITranslator $translator = NULL
65
	) {
66
		$this->storage = $storage;
67
		$this->translator = $translator;
68
		$this->useTranslator = $useTranslator;
69
	}
70
71
	/**
72
	 * Flash a success message
73
	 *
74
	 * @param string $message
75
	 * @param string|NULL $title
76
	 *
77
	 * @return Entities\IMessage
78
	 */
79 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...
80
	{
81
		$args = func_get_args();
82
		array_splice($args, 1, 0, [Entities\IMessage::LEVEL_SUCCESS]);
83
84
		return call_user_func_array([$this, 'setMessage'], $args);
85
	}
86
87
	/**
88
	 * Flash an information message
89
	 *
90
	 * @param string $message
91
	 * @param string|NULL $title
92
	 *
93
	 * @return Entities\IMessage
94
	 */
95 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...
96
	{
97
		$args = func_get_args();
98
		array_splice($args, 1, 0, [Entities\IMessage::LEVEL_INFO]);
99
100
		return call_user_func_array([$this, 'setMessage'], $args);
101
	}
102
103
	/**
104
	 * Flash a warning message
105
	 *
106
	 * @param string $message
107
	 * @param string|NULL $title
108
	 *
109
	 * @return Entities\IMessage
110
	 */
111 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...
112
	{
113
		$args = func_get_args();
114
		array_splice($args, 1, 0, [Entities\IMessage::LEVEL_WARNING]);
115
116
		return call_user_func_array([$this, 'setMessage'], $args);
117
	}
118
119
	/**
120
	 * Flash an error message
121
	 *
122
	 * @param string $message
123
	 * @param string|NULL $title
124
	 *
125
	 * @return Entities\IMessage
126
	 */
127 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...
128
	{
129
		$args = func_get_args();
130
		array_splice($args, 1, 0, [Entities\IMessage::LEVEL_ERROR]);
131
132
		return call_user_func_array([$this, 'setMessage'], $args);
133
	}
134
135
	/**
136
	 * Add an "important" flash to the session
137
	 *
138
	 * @return void
139
	 */
140
	public function important() : void
141
	{
142
		$this->storage->set(Storage\IStorage::KEY_IMPORTANT, TRUE);
143
	}
144
145
	/**
146
	 * Flash an overlay modal
147
	 *
148
	 * @param string $message
149
	 * @param string|NULL $title
150
	 *
151
	 * @return Entities\IMessage
152
	 */
153
	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...
154
	{
155
		$args = func_get_args();
156
157
		$level = $args[1];
158
159
		if (is_string($level) === FALSE || $level === NULL
160
			|| !in_array($level, [Entities\IMessage::LEVEL_ERROR, Entities\IMessage::LEVEL_INFO, Entities\IMessage::LEVEL_SUCCESS, Entities\IMessage::LEVEL_WARNING])
161
		) {
162
			array_splice($args, 1, 0, [Entities\IMessage::LEVEL_INFO]);
163
		}
164
165
		array_splice($args, 3, 0, [TRUE]);
166
167
		return call_user_func_array([$this, 'setMessage'], $args);
168
	}
169
170
	/**
171
	 * @param string $message
172
	 * @param string $level
173
	 * @param string|NULL $title
174
	 * @param bool $overlay
175
	 * @param int|NULL $count
176
	 * @param array $parameters
177
	 *
178
	 * @return Entities\IMessage
179
	 */
180
	public function message($message, $level = Entities\IMessage::LEVEL_INFO, $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) : Entities\IMessage
181
	{
182
		return $this->setMessage($message, $level, $title, $overlay, $count, $parameters);
183
	}
184
185
	/**
186
	 * Flash a general message
187
	 *
188
	 * @param string $message
189
	 * @param string $level
190
	 * @param string|NULL $title
191
	 * @param bool $overlay
192
	 * @param int|NULL $count
193
	 * @param array $parameters
194
	 *
195
	 * @return Entities\IMessage
196
	 */
197
	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...
198
	{
199
		$args = func_get_args();
200
201
		// Remove message
202
		unset($args[0]);
203
		// Remove level
204
		unset($args[1]);
205
206
		$title = $this->checkForAttribute($args, 'title', NULL);
207
		$overlay = $this->checkForAttribute($args, 'overlay', FALSE);
208
		$count = $this->checkForAttribute($args, 'count', NULL);
209
		$parameters = $this->checkForAttribute($args, 'parameters', []);
210
211
		if (!$message instanceof Adapters\IPhraseAdapter) {
212
			$phrase = new Adapters\DefaultPhraseAdapter($message, $count, $parameters);
213
214
		} else {
215
			$phrase = $message;
216
		}
217
218
		if (!$title instanceof Adapters\IPhraseAdapter && $title !== NULL) {
219
			$titlePhrase = new Adapters\DefaultPhraseAdapter($title, $count, $parameters);
220
221
		} else {
222
			$titlePhrase = NULL;
223
		}
224
225
		// Get all stored messages
226
		$messages = $this->storage->get(Storage\IStorage::KEY_MESSAGES, []);
227
228
		// Create flash message
229
		$flash = new Entities\Message(($this->useTranslator ? $this->translator : NULL), $phrase, $titlePhrase);
230
		$flash->setLevel($level);
231
		$flash->setOverlay($overlay);
232
233
		if (!$this->useTranslator || !$this->translator instanceof Localization\ITranslator) {
234
			if (is_string($message) === TRUE) {
235
				$flash->setMessage($message);
236
			}
237
238
			if (is_string($title) === TRUE) {
239
				$flash->setTitle((string) $title);
240
			}
241
		}
242
243
		if ($this->checkUnique($flash, $messages) === FALSE) {
244
			$messages[] = $flash;
245
		}
246
247
		// Store messages in session
248
		$this->storage->set(Storage\IStorage::KEY_MESSAGES, $messages);
249
250
		return $flash;
251
	}
252
253
	/**
254
	 * @param Entities\IMessage $flash
255
	 * @param Entities\IMessage[] $messages
256
	 *
257
	 * @return bool
258
	 */
259
	private function checkUnique(Entities\IMessage $flash, array $messages) : bool
260
	{
261
		foreach ($messages as $member) {
262
			if ((string) $member === (string) $flash && !$member->isDisplayed()) {
263
				return TRUE;
264
			}
265
		}
266
267
		return FALSE;
268
	}
269
270
	/**
271
	 * @param array $attributes
272
	 * @param string $type
273
	 * @param mixed $default
274
	 *
275
	 * @return mixed
276
	 */
277
	private function checkForAttribute(array $attributes, string $type, $default)
278
	{
279
		foreach($attributes as $attribute) {
280
			switch($type)
281
			{
282
				case 'title':
283
					if (is_string($attribute) === TRUE || $attribute instanceof Translation\Phrase || $attribute instanceof Adapters\IPhraseAdapter) {
0 ignored issues
show
Bug introduced by
The class IPub\FlashMessages\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...
284
						return $attribute;
285
					}
286
					break;
287
288
				case 'overlay':
289
					if (is_bool($attribute) === TRUE) {
290
						return $attribute;
291
					}
292
					break;
293
294
				case 'count':
295
					if (is_numeric($attribute) === TRUE) {
296
						return $attribute;
297
					}
298
					break;
299
300
				case 'parameters':
301
					if (is_array($attribute) === TRUE) {
302
						return $attribute;
303
					}
304
					break;
305
			}
306
		}
307
308
		// Return default
309
		return $default;
310
	}
311
}
312