36 lines
1.0 KiB
PHP
36 lines
1.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace MintyPHP\Module\Bookmarks\Providers;
|
||
|
|
|
||
|
|
use MintyPHP\App\AppContainer;
|
||
|
|
use MintyPHP\App\Module\Contracts\SessionProvider;
|
||
|
|
use MintyPHP\Http\SessionStoreInterface;
|
||
|
|
use MintyPHP\Module\Bookmarks\Service\BookmarkService;
|
||
|
|
|
||
|
|
final class BookmarksSessionProvider implements SessionProvider
|
||
|
|
{
|
||
|
|
private const SESSION_KEY = 'module.bookmarks.grouped';
|
||
|
|
|
||
|
|
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);
|
||
|
|
$bookmarkService = $container->get(BookmarkService::class);
|
||
|
|
if (!$sessionStore instanceof SessionStoreInterface || !$bookmarkService instanceof BookmarkService) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$sessionStore->set(self::SESSION_KEY, $bookmarkService->listGroupedForUser($userId));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function clear(): void
|
||
|
|
{
|
||
|
|
unset($_SESSION[self::SESSION_KEY]);
|
||
|
|
}
|
||
|
|
}
|