Completed
Push — master ( 023705...8045d7 )
by Adam
02:56
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 84.44%

Importance

Changes 5
Bugs 2 Features 2
Metric Value
wmc 36
c 5
b 2
f 2
lcom 1
cbo 5
dl 28
loc 274
ccs 76
cts 90
cp 0.8444
rs 8.8

11 Methods

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