| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Notification; |
|
|
| use Piwik\Notification; |
| use Piwik\Session; |
| use Piwik\Session\SessionNamespace; |
|
|
| |
| |
| |
| |
| class Manager |
| { |
| public const MAX_NOTIFICATIONS_IN_SESSION = 30; |
|
|
| |
| |
| |
| private static $session = null; |
|
|
| |
| |
| |
| private static $notifications = []; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function notify($id, Notification $notification): bool |
| { |
| self::checkId($id); |
|
|
| self::removeOldestNotificationsIfThereAreTooMany(); |
| return self::addNotification($id, $notification); |
| } |
|
|
| |
| |
| |
| |
| |
| public static function cancel($id): void |
| { |
| self::checkId($id); |
|
|
| self::removeNotification($id); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static function cancelAllNonPersistent(): void |
| { |
| foreach (self::getAllNotifications() as $id => $notification) { |
| if (Notification::TYPE_PERSISTENT != $notification->type) { |
| self::removeNotification($id); |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| public static function getAllNotificationsToDisplay(): array |
| { |
| $notifications = self::getAllNotifications(); |
|
|
| uasort($notifications, function ($n1, $n2) { |
| |
| if ($n1->getPriority() == $n2->getPriority()) { |
| return 0; |
| } |
|
|
| return $n1->getPriority() > $n2->getPriority() ? -1 : 1; |
| }); |
|
|
| return $notifications; |
| } |
|
|
| |
| |
| |
| |
| private static function checkId($id): void |
| { |
| if (empty($id)) { |
| throw new \Exception('Notification ID is empty.'); |
| } |
|
|
| if (!preg_match('/^\w*$/', $id)) { |
| throw new \Exception('Invalid Notification ID given. Only word characters (AlNum + underscore) allowed.'); |
| } |
| } |
|
|
| private static function addNotification($id, Notification $notification): bool |
| { |
| self::saveNotificationAcrossUiRequestsIfNeeded($id, $notification); |
|
|
| if (count(self::$notifications) >= self::MAX_NOTIFICATIONS_IN_SESSION) { |
| return false; |
| } |
|
|
| |
| |
| self::$notifications[$id] = $notification; |
|
|
| return true; |
| } |
|
|
| private static function saveNotificationAcrossUiRequestsIfNeeded($id, Notification $notification): void |
| { |
| if (self::isSessionEnabled()) { |
| |
| |
| $session = self::getSession(); |
| $session->notifications[$id] = $notification; |
| } |
| } |
|
|
| private static function removeOldestNotificationsIfThereAreTooMany(): void |
| { |
| if (!self::isSessionEnabled()) { |
| return; |
| } |
|
|
| $maxNotificationsInSession = self::MAX_NOTIFICATIONS_IN_SESSION; |
|
|
| $session = self::getSession(); |
|
|
| while (count($session->notifications) >= $maxNotificationsInSession) { |
| array_shift($session->notifications); |
| } |
| } |
|
|
| |
| |
| |
| private static function getAllNotifications(): array |
| { |
| if (!self::isSessionEnabled()) { |
| return []; |
| } |
|
|
| $notifications = self::$notifications; |
|
|
| foreach ($notifications as $id => $notification) { |
| |
| |
| self::saveNotificationAcrossUiRequestsIfNeeded($id, $notification); |
| } |
|
|
| $session = self::getSession(); |
| foreach ($session->notifications as $id => $notification) { |
| $notifications[$id] = $notification; |
| } |
|
|
| return $notifications; |
| } |
|
|
| private static function removeNotification($id): void |
| { |
| if (array_key_exists($id, self::$notifications)) { |
| unset(self::$notifications[$id]); |
| } |
|
|
| if (self::isSessionEnabled()) { |
| $session = self::getSession(); |
| if (array_key_exists($id, $session->notifications)) { |
| unset($session->notifications[$id]); |
| } |
| } |
| } |
|
|
| private static function isSessionEnabled(): bool |
| { |
| return Session::isWritable() && Session::isReadable(); |
| } |
|
|
| private static function getSession(): SessionNamespace |
| { |
| if (!isset(self::$session)) { |
| self::$session = new SessionNamespace('notification'); |
| } |
|
|
| if (empty(self::$session->notifications) && self::isSessionEnabled()) { |
| self::$session->notifications = []; |
| } |
|
|
| return self::$session; |
| } |
|
|
| public static function cancelAllNotifications(): void |
| { |
| self::$notifications = []; |
| } |
|
|
| |
| |
| |
| |
| public static function getPendingInMemoryNotifications(): array |
| { |
| return self::$notifications; |
| } |
| } |
|
|