FlashMessage::addFlashMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 17
cp 0
rs 9.504
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
namespace TildBJ\Seeder\Message;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2016 Dennis Römmich <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
use TildBJ\Seeder;
28
29
/**
30
 * FlashMessage
31
 *
32
 * @author Dennis Römmich<[email protected]>
33
 * @copyright Copyright belongs to the respective authors
34
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
35
 */
36
class FlashMessage implements Seeder\Message, \TYPO3\CMS\Core\SingletonInterface
37
{
38
    /**
39
     * error
40
     *
41
     * @param string $message
42
     * @return void
43
     */
44
    public function error($message)
45
    {
46
        $this->addFlashMessage($message, \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR);
47
    }
48
49
    /**
50
     * info
51
     *
52
     * @param string $message
53
     * @return void
54
     */
55
    public function info($message)
56
    {
57
        $this->addFlashMessage($message, \TYPO3\CMS\Core\Messaging\AbstractMessage::INFO);
58
    }
59
60
    /**
61
     * notice
62
     *
63
     * @param string $message
64
     * @return void
65
     */
66
    public function notice($message)
67
    {
68
        $this->addFlashMessage($message, \TYPO3\CMS\Core\Messaging\AbstractMessage::NOTICE);
69
    }
70
71
    /**
72
     * success
73
     *
74
     * @param string $message
75
     * @return void
76
     */
77
    public function success($message)
78
    {
79
        $this->addFlashMessage($message, \TYPO3\CMS\Core\Messaging\AbstractMessage::OK);
80
    }
81
82
    /**
83
     * warning
84
     *
85
     * @param string $message
86
     * @return void
87
     */
88
    public function warning($message)
89
    {
90
        $this->addFlashMessage($message, \TYPO3\CMS\Core\Messaging\AbstractMessage::WARNING);
91
    }
92
93
    /**
94
     * addFlashMessage
95
     *
96
     * @param $message
97
     * @param $severity
98
     * @return void
99
     */
100
    protected function addFlashMessage($message, $severity)
101
    {
102
        if (!is_string($message)) {
103
            throw new \InvalidArgumentException(
104
                'The message body must be of type string, "' . gettype($message) . '" given.',
105
                1243258395
106
            );
107
        }
108
        /* @var \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage */
109
        $flashMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
110
            \TYPO3\CMS\Core\Messaging\FlashMessage::class,
111
            $message,
112
            '',
113
            $severity,
114
            true
115
        );
116
117
        /** @var \TYPO3\CMS\Core\Messaging\FlashMessageService $flashMessageService */
118
        $flashMessageService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
119
            \TYPO3\CMS\Core\Messaging\FlashMessageService::class
120
        );
121
        $messageQueue = $flashMessageService->getMessageQueueByIdentifier(
122
            'extbase.flashmessages.tx_seeder_web_seederseedermod'
123
        );
124
        $messageQueue->addMessage($flashMessage);
125
    }
126
}
127