Passed
Pull Request — master (#4)
by
unknown
03:09
created

FlashNotifier::setMessage()   C

Complexity

Conditions 12
Paths 90

Size

Total Lines 64
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 12.144

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 64
ccs 27
cts 30
cp 0.9
rs 6.0561
cc 12
eloc 33
nc 90
nop 6
crap 12.144

How to fix   Long Method    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
39
{
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
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 1
		$this->storage = $storage;
69 1
		$this->translator = $translator;
70 1
		$this->useTranslator = $useTranslator;
71 1
	}
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
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, ['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
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 1
		$args = func_get_args();
100 1
		array_splice($args, 1, 0, ['info']);
101
102 1
		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
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, ['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
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, ['danger']);
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()
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
156
	{
157 1
		$args = func_get_args();
158
159 1
		$level = $args[1];
160
161 1
		if (is_string($level) === FALSE || $level === NULL
162 1
			|| !in_array($level, [Entities\IMessage::LEVEL_ERROR, Entities\IMessage::LEVEL_INFO, Entities\IMessage::LEVEL_SUCCESS, Entities\IMessage::LEVEL_WARNING])
163
		) {
164 1
			array_splice($args, 1, 0, ['info']);
165
		}
166
167 1
		array_splice($args, 3, 0, [TRUE]);
168
169 1
		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 boolean $overlay
177
	 * @param int|NULL $count
178
	 * @param array $parameters
179
	 *
180
	 * @return Entities\IMessage
181
	 */
182
	public function message($message, $level = 'info', $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) : Entities\IMessage
183
	{
184 1
		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 boolean $overlay
194
	 * @param int|NULL $count
195
	 * @param array $parameters
196
	 *
197
	 * @return Entities\IMessage
198
	 */
199
	public function setMessage($message, $level = 'info', $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) : Entities\IMessage
200
	{
201 1
		$args = func_get_args();
202
		// Remove message
203 1
		unset($args[0]);
204
		// Remove level
205 1
		unset($args[1]);
206
207 1
		$title = $this->checkForAttribute($args, 'title', NULL);
208 1
		$overlay = $this->checkForAttribute($args, 'overlay', FALSE);
209 1
		$count = $this->checkForAttribute($args, 'count', NULL);
210 1
		$parameters = $this->checkForAttribute($args, 'parameters', []);
211
212
		// Support for Kdyby/Translation
213 1
		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...
214
			$phrase = new Adapters\KdybyPhraseAdapter($message);
215
216
		// Default phrase adapter
217 1
		} else if (!$message instanceof Adapters\IPhraseAdapter) {
218 1
			$phrase = new Adapters\DefaultPhraseAdapter($message, $count, $parameters);
219
220
		} else {
221
			$phrase = $message;
222
		}
223
224
		// Support for Kdyby/Translation
225 1
		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...
226
			$titlePhrase = new Adapters\KdybyPhraseAdapter($title);
227
228
		// Default phrase adapter
229 1
		} else if (!$title instanceof Adapters\IPhraseAdapter && $title !== NULL) {
230 1
			$titlePhrase = new Adapters\DefaultPhraseAdapter($title, $count, $parameters);
231
232
		} else {
233 1
			$titlePhrase = NULL;
234
		}
235
236
		// Get all stored messages
237 1
		$messages = $this->storage->get(Storage\IStorage::KEY_MESSAGES, []);
238
239
		// Create flash message
240 1
		$flash = new Entities\Message(($this->useTranslator ? $this->translator : NULL), $phrase, $titlePhrase);
241 1
		$flash->setLevel($level);
242 1
		$flash->setOverlay($overlay);
243
244 1
		if (!$this->useTranslator || !$this->translator instanceof Localization\ITranslator) {
0 ignored issues
show
Bug introduced by
The class Nette\Localization\ITranslator 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...
245 1
			if (is_string($message) === TRUE) {
246 1
				$flash->setMessage($message);
247
			}
248
249 1
			if (is_string($title) === TRUE) {
250 1
				$flash->setTitle($title);
251
			}
252
		}
253
254 1
		if ($this->checkUnique($flash, $messages) === FALSE) {
255 1
			$messages[] = $flash;
256
		}
257
258
		// Store messages in session
259 1
		$this->storage->set(Storage\IStorage::KEY_MESSAGES, $messages);
260
261 1
		return $flash;
262
	}
263
264
	/**
265
	 * @param Entities\IMessage $flash
266
	 * @param Entities\IMessage[] $messages
267
	 *
268
	 * @return bool
269
	 */
270
	private function checkUnique(Entities\IMessage $flash, array $messages) : bool
271
	{
272 1
		foreach ($messages as $member) {
273 1
			if ((string) $member === (string) $flash && !$member->isDisplayed()) {
274 1
				return TRUE;
275
			}
276
		}
277
278 1
		return FALSE;
279
	}
280
281
	/**
282
	 * @param array $attributes
283
	 * @param string $type
284
	 * @param mixed $default
285
	 *
286
	 * @return mixed
287
	 */
288
	private function checkForAttribute(array $attributes, string $type, $default)
289
	{
290 1
		foreach($attributes as $attribute) {
291
			switch($type)
292
			{
293 1
				case 'title':
294 1
					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...
295 1
						return $attribute;
296
					}
297 1
					break;
298
299 1
				case 'overlay':
300 1
					if (is_bool($attribute) === TRUE) {
301 1
						return $attribute;
302
					}
303 1
					break;
304
305 1
				case 'count':
306 1
					if (is_numeric($attribute) === TRUE) {
307 1
						return $attribute;
308
					}
309 1
					break;
310
311 1
				case 'parameters':
312 1
					if (is_array($attribute) === TRUE) {
313 1
						return $attribute;
314
					}
315 1
					break;
316
			}
317
		}
318
319
		// Return default
320 1
		return $default;
321
	}
322
}
323