Completed
Push — master ( 280a22...ae35ae )
by Adam
02:21
created

FlashNotifier::checkForAttribute()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 18
cts 18
cp 1
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 20
nc 10
nop 3
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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