Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Manager.php « Notification « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2fd53a3c5022bf3c81685697526a46f02e83d0e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik
 * @package Piwik
 */
namespace Piwik\Notification;

use Piwik\Notification;
use Piwik\Session\SessionNamespace;

/**
 * @package Piwik
 * @subpackage Notification
 * @api
 */
class Manager
{
    private static $session = null;

    /**
     * Post a notification to be shown in the status bar. If a notification with the same id has already been posted
     * by your application and has not yet been canceled, it will be replaced by the updated information.
     *
     * @param string       $id   A unique identifier for this notification. Id must be a string and may contain only
     *                           word characters (AlNum + underscore)
     * @param Notification $notification
     */
    public static function notify($id, Notification $notification)
    {
        self::checkId($id);

        $session = static::getSession();
        $session->$id = $notification;

        if (Notification::TYPE_PERSISTENT != $notification->type) {
            $session->setExpirationHops(1, $id);
        }
    }

    public static function getAllNotificationsToDisplay()
    {
        $session = static::getSession();
        $notifications = $session->getIterator();

        $notifications->uasort(function ($n1, $n2) {
            if ($n1->priority == $n2->priority) {
                return 0;
            }

            return $n1->priority > $n2->priority;
        });

        return $notifications;
    }

    /**
     * Cancel a previously registered (or persistent) notification.
     * @param $id
     */
    public static function cancel($id)
    {
        self::checkId($id);

        $session = static::getSession();
        unset($session->$id);
    }

    /**
     * @return SessionNamespace
     */
    private static function getSession()
    {
        if (!isset(static::$session)) {
            static::$session = new SessionNamespace('notification');
        }

        return static::$session;
    }

    /**
     * @param $id
     * @throws \Exception
     */
    private static function checkId($id)
    {
        if (empty($id)) {
            throw new \Exception('Notification ID is empty.');
        }

        if (!is_string($id) || !preg_match('/^(\w)*$/', $id)) {
            throw new \Exception('Invalid Notification ID given. Only word characters (AlNum + underscore) allowed.');
        }
    }
}