Completed
Pull Request — master (#278)
by Pascale
03:06
created

AbstractCommandController::enqueueMessage()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.9617
c 0
b 0
f 0
cc 6
nc 6
nop 3
1
<?php
2
3
/**
4
 * Command controller abstraction.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\Command;
9
10
use TYPO3\CMS\Core\Messaging\FlashMessage;
11
use TYPO3\CMS\Core\Messaging\FlashMessageService;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
14
15
/**
16
 * Command controller abstraction.
17
 */
18
abstract class AbstractCommandController extends CommandController
0 ignored issues
show
Deprecated Code introduced by
The class TYPO3\CMS\Extbase\Mvc\Controller\CommandController has been deprecated with message: since TYPO3 v9, will be removed in TYPO3 v10.0. Use symfony/console commands instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
19
{
20
    /**
21
     * Flash message service.
22
     *
23
     * @var \TYPO3\CMS\Core\Messaging\FlashMessageService
24
     */
25
    protected $flashMessageService;
26
27
    public function injectFlashMessageService(FlashMessageService $flashMessageService)
28
    {
29
        $this->flashMessageService = $flashMessageService;
30
    }
31
32
    /**
33
     * Adds a message to the FlashMessageQueue or prints it to the CLI.
34
     *
35
     * @param mixed  $message
36
     * @param string $title
37
     * @param int    $severity
38
     */
39
    public function enqueueMessage($message, $title = '', $severity = FlashMessage::INFO)
40
    {
41
        if ($message instanceof \Exception) {
42
            if ('' === $title) {
43
                $title = 'Exception: ' . $message->getCode();
44
            }
45
            $message = '"' . $message->getMessage() . '"' . LF . 'In ' . $message->getFile() . ' at line ' . $message->getLine() . '!';
46
            $this->enqueueMessage($message, $title, FlashMessage::ERROR);
47
48
            return;
49
        }
50
        if (!\is_scalar($message)) {
51
            $message = \var_export($message, true);
52
        }
53
        if (\defined('TYPO3_cliMode') && TYPO3_cliMode) {
54
            echo '==' . $title . ' == ' . LF;
55
            echo $message . LF;
56
        } else {
57
            $this->enqueueMessageGui($message, $title, $severity);
58
        }
59
    }
60
61
    /**
62
     * Adds a message to the FlashMessageQueue.
63
     *
64
     * @param mixed  $message
65
     * @param string $title
66
     * @param int    $severity
67
     */
68
    private function enqueueMessageGui($message, $title = '', $severity = FlashMessage::INFO)
69
    {
70
        $message = GeneralUtility::makeInstance(FlashMessage::class, \nl2br($message), $title, $severity);
71
        $this->flashMessageService->getMessageQueueByIdentifier()
72
            ->enqueue($message);
73
    }
74
}
75