This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | 1 | $this->storage = $storage; |
|
67 | 1 | $this->translator = $translator; |
|
68 | 1 | $this->useTranslator = $useTranslator; |
|
69 | 1 | } |
|
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
|
|||
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
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 | 1 | $args = func_get_args(); |
|
98 | 1 | array_splice($args, 1, 0, [Entities\IMessage::LEVEL_INFO]); |
|
99 | |||
100 | 1 | 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
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
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 |
||
154 | { |
||
155 | 1 | $args = func_get_args(); |
|
156 | |||
157 | 1 | $level = $args[1]; |
|
158 | |||
159 | 1 | if (is_string($level) === FALSE || $level === NULL |
|
160 | 1 | || !in_array($level, [Entities\IMessage::LEVEL_ERROR, Entities\IMessage::LEVEL_INFO, Entities\IMessage::LEVEL_SUCCESS, Entities\IMessage::LEVEL_WARNING]) |
|
161 | ) { |
||
162 | 1 | array_splice($args, 1, 0, [Entities\IMessage::LEVEL_INFO]); |
|
163 | } |
||
164 | |||
165 | 1 | array_splice($args, 3, 0, [TRUE]); |
|
166 | |||
167 | 1 | 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 | 1 | 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 |
||
198 | { |
||
199 | 1 | $args = func_get_args(); |
|
200 | |||
201 | // Remove message |
||
202 | 1 | unset($args[0]); |
|
203 | // Remove level |
||
204 | 1 | unset($args[1]); |
|
205 | |||
206 | 1 | $title = $this->checkForAttribute($args, 'title', NULL); |
|
207 | 1 | $overlay = $this->checkForAttribute($args, 'overlay', FALSE); |
|
208 | 1 | $count = $this->checkForAttribute($args, 'count', NULL); |
|
209 | 1 | $parameters = $this->checkForAttribute($args, 'parameters', []); |
|
210 | |||
211 | 1 | if (!$message instanceof Adapters\IPhraseAdapter) { |
|
212 | 1 | $phrase = new Adapters\DefaultPhraseAdapter($message, $count, $parameters); |
|
213 | |||
214 | } else { |
||
215 | $phrase = $message; |
||
216 | } |
||
217 | |||
218 | 1 | 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->useTranslator ? $this->translator : NULL), $phrase, $titlePhrase); |
|
230 | 1 | $flash->setLevel($level); |
|
231 | 1 | $flash->setOverlay($overlay); |
|
232 | |||
233 | 1 | if (!$this->useTranslator || !$this->translator instanceof Localization\ITranslator) { |
|
0 ignored issues
–
show
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 dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed.
Loading history...
|
|||
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((string) $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 && !$member->isDisplayed()) { |
|
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 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 |
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.