2026-03-19 22:09:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MintyPHP\Module\Notifications\Providers;
|
|
|
|
|
|
|
|
|
|
use MintyPHP\App\AppContainer;
|
|
|
|
|
use MintyPHP\App\Module\Contracts\SessionProvider;
|
|
|
|
|
use MintyPHP\Http\SessionStoreInterface;
|
|
|
|
|
use MintyPHP\Module\Notifications\Service\NotificationService;
|
|
|
|
|
|
|
|
|
|
final class NotificationsSessionProvider implements SessionProvider
|
|
|
|
|
{
|
|
|
|
|
private const SESSION_KEY = 'module.notifications.unread_count';
|
|
|
|
|
|
|
|
|
|
public function populate(array $user, AppContainer $container): void
|
|
|
|
|
{
|
|
|
|
|
$userId = (int) ($user['id'] ?? 0);
|
|
|
|
|
if ($userId <= 0) {
|
|
|
|
|
unset($_SESSION[self::SESSION_KEY]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sessionStore = $container->get(SessionStoreInterface::class);
|
|
|
|
|
$service = $container->get(NotificationService::class);
|
|
|
|
|
if (!$sessionStore instanceof SessionStoreInterface || !$service instanceof NotificationService) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
$currentTenant = $sessionStore->get('current_tenant', []);
|
|
|
|
|
$tenantId = is_array($currentTenant) ? (int) ($currentTenant['id'] ?? 0) : 0;
|
|
|
|
|
$sessionStore->set(self::SESSION_KEY, $service->unreadCount($userId, $tenantId > 0 ? $tenantId : null));
|
2026-03-19 22:09:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function clear(): void
|
|
|
|
|
{
|
|
|
|
|
unset($_SESSION[self::SESSION_KEY]);
|
|
|
|
|
}
|
|
|
|
|
}
|