FlashMessageUtility::getFlashMessageService()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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