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

FlashNotifier   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 274
Duplicated Lines 10.22 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 82.28%

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 5
dl 28
loc 274
ccs 65
cts 79
cp 0.8228
rs 8.8
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A success() 7 7 1
A info() 7 7 1
A warning() 7 7 1
A error() 7 7 1
A important() 0 4 1
A overlay() 0 16 4
A message() 0 4 1
C setMessage() 0 64 10
A checkUnique() 0 10 3
C checkForAttribute() 0 34 12

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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