Passed
Push — master ( 940106...2dc8db )
by Simon
02:20
created

FlashMessageUtility::getFlashMessageQueue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the "GS Library" Extension for TYPO3 CMS.
5
 *
6
 * Copyright (C) 2017 by Gilbertsoft (gilbertsoft.org)
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * For the full license information, please read the LICENSE file that
19
 * was distributed with this source code.
20
 *
21
 * The TYPO3 project - inspiring people to share!
22
 */
23
24
namespace Gilbertsoft\Lib\Utility;
25
26
27
/**
28
 * Use declarations
29
 */
30
use TYPO3\CMS\Core\Messaging\FlashMessage;
31
use TYPO3\CMS\Core\Messaging\FlashMessageService;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34
35
/**
36
 * GS Flash Message Utility class.
37
 */
38
class FlashMessageUtility
39
{
40
	/**
41
	 * @var \TYPO3\CMS\Core\Messaging\FlashMessageService
42
	 */
43
	protected static $flashMessageService = null;
44
	/**
45
	 * Returns the Flash Message Service
46
	 *
47
	 * @return \TYPO3\CMS\Core\Messaging\FlashMessageService
48
	 */
49
	public static function getFlashMessageService()
50
	{
51
		if (self::$flashMessageService === null) {
52
			// cache the object for performance-reasons
53
			self::$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
54
		}
55
		return self::$flashMessageService;
56
	}
57
58
	/**
59
	 * Returns the Flash Message Queue
60
	 *
61
	 * @param string $extensionKey
62
	 * @return \TYPO3\CMS\Core\Messaging\FlashMessageQueue
63
	 * @throws \InvalidArgumentException
64
	 */
65
	public static function getFlashMessageQueue($extensionKey)
66
	{
67
		if (!is_string($extensionKey) || empty($extensionKey)) {
68
			throw new \InvalidArgumentException('$extensionKey must be a non empty string.', 1491502264);
69
		}
70
		return self::getFlashMessageService()->getMessageQueueByIdentifier('gslib.flashmessages.' . $extensionKey);
71
	}
72
73
	/**
74
	 * Adds a Flash Message to the Flash Message Queue
75
	 *
76
	 * @param \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage
77
	 * @param string $extensionKey
78
	 * @return void
79
	 */
80
	public static function addFlashMessageToQueue(FlashMessage $flashMessage, $extensionKey)
81
	{
82
		if ($flashMessage) {
83
			self::getFlashMessageQueue($extensionKey)->enqueue($flashMessage);
84
		}
85
	}
86
87
	/**
88
	 * Create a Flash Message and add it to the Queue
89
	 *
90
	 * @param string $extensionKey
91
	 * @param string $message The message.
92
	 * @param string $title Optional message title.
93
	 * @param int $severity Optional severity, must be either of one of \TYPO3\CMS\Core\Messaging\FlashMessage constants
94
	 * @param bool $storeInSession Optional, defines whether the message should be stored in the session or only for one request (default)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
95
	 * @return void
96
	 */
97
	public static function showFlashMessage($extensionKey, $message, $title = '', $severity = FlashMessage::OK, $storeInSession = true)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
98
	{
99
		if (is_string($message) || !empty($message)) {
100
			self::addFlashMessageToQueue(
101
				GeneralUtility::makeInstance(
102
					FlashMessage::class,
103
					$message,
104
					$title,
105
					$severity,
106
					$storeInSession
107
				), 
108
				$extensionKey,
109
				''
0 ignored issues
show
Unused Code introduced by
The call to FlashMessageUtility::addFlashMessageToQueue() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
110
			);
111
		}
112
	}
113
}