Issues (3627)

app/bundles/CoreBundle/Service/FlashBag.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2018 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CoreBundle\Service;
13
14
use Mautic\CoreBundle\Model\NotificationModel;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
use Symfony\Component\HttpFoundation\Session\Session;
17
use Symfony\Component\Translation\TranslatorInterface;
18
19
/**
20
 * Provides translated flash messages.
21
 */
22
class FlashBag
23
{
24
    const LEVEL_ERROR     = 'error';
25
    const LEVEL_WARNING   = 'warning';
26
    const LEVEL_NOTICE    = 'notice';
27
28
    /**
29
     * @var Session
30
     */
31
    private $session;
32
33
    /**
34
     * @var TranslatorInterface
35
     */
36
    private $translator;
37
38
    /**
39
     * @var NotificationModel
40
     */
41
    private $notificationModel;
42
43
    /**
44
     * @var RequestStack
45
     */
46
    private $requestStack;
47
48
    public function __construct(
49
        Session $session,
50
        TranslatorInterface $translator,
51
        RequestStack $requestStack,
52
        NotificationModel $notificationModel
53
    ) {
54
        $this->session           = $session;
55
        $this->translator        = $translator;
56
        $this->requestStack      = $requestStack;
57
        $this->notificationModel = $notificationModel;
58
    }
59
60
    /**
61
     * @param string     $message
62
     * @param array|null $messageVars
63
     * @param string     $level
64
     * @param string     $domain
65
     * @param bool       $addNotification
66
     */
67
    public function add($message, $messageVars = [], $level = self::LEVEL_NOTICE, $domain = 'flashes', $addNotification = false)
68
    {
69
        if (false === $domain) {
0 ignored issues
show
The condition false === $domain is always false.
Loading history...
70
            //message is already translated
71
            $translatedMessage = $message;
72
        } else {
73
            if (isset($messageVars['pluralCount'])) {
74
                $translatedMessage = $this->translator->transChoice($message, $messageVars['pluralCount'], $messageVars, $domain);
75
            } else {
76
                $translatedMessage = $this->translator->trans($message, $messageVars, $domain);
77
            }
78
        }
79
80
        $this->session->getFlashBag()->add($level, $translatedMessage);
81
82
        if (!defined('MAUTIC_INSTALLER') && $addNotification) {
83
            switch ($level) {
84
                case self::LEVEL_WARNING:
85
                    $iconClass = 'text-warning fa-exclamation-triangle';
86
                    break;
87
                case self::LEVEL_ERROR:
88
                    $iconClass = 'text-danger fa-exclamation-circle';
89
                    break;
90
                default:
91
                    $iconClass = 'fa-info-circle';
92
                    break;
93
            }
94
95
            //If the user has not interacted with the browser for the last 30 seconds, consider the message unread
96
            $lastActive = $this->requestStack->getCurrentRequest()->get('mauticUserLastActive', 0);
97
            $isRead     = $lastActive > 30 ? 0 : 1;
98
99
            $this->notificationModel->addNotification($message, $level, $isRead, null, $iconClass);
100
        }
101
    }
102
}
103