agent foundation
This commit is contained in:
@@ -2,12 +2,17 @@
|
||||
|
||||
namespace MintyPHP\Service\Import;
|
||||
|
||||
use MintyPHP\Http\SessionStoreInterface;
|
||||
|
||||
class ImportStateStoreService
|
||||
{
|
||||
private const SESSION_KEY = 'admin_imports_v1';
|
||||
private const STATE_TTL_SECONDS = 7200;
|
||||
|
||||
public function __construct(private readonly ImportTempFileService $importTempFileService)
|
||||
public function __construct(
|
||||
private readonly ImportTempFileService $importTempFileService,
|
||||
private readonly SessionStoreInterface $sessionStore
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -16,10 +21,12 @@ class ImportStateStoreService
|
||||
*/
|
||||
public function setState(string $token, array $state): void
|
||||
{
|
||||
if (!isset($_SESSION[self::SESSION_KEY]) || !is_array($_SESSION[self::SESSION_KEY])) {
|
||||
$_SESSION[self::SESSION_KEY] = [];
|
||||
$states = $this->sessionStore->get(self::SESSION_KEY, []);
|
||||
if (!is_array($states)) {
|
||||
$states = [];
|
||||
}
|
||||
$_SESSION[self::SESSION_KEY][$token] = $state;
|
||||
$states[$token] = $state;
|
||||
$this->sessionStore->set(self::SESSION_KEY, $states);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,7 +39,7 @@ class ImportStateStoreService
|
||||
return null;
|
||||
}
|
||||
|
||||
$all = $_SESSION[self::SESSION_KEY] ?? [];
|
||||
$all = $this->sessionStore->get(self::SESSION_KEY, []);
|
||||
if (!is_array($all)) {
|
||||
return null;
|
||||
}
|
||||
@@ -42,6 +49,7 @@ class ImportStateStoreService
|
||||
return null;
|
||||
}
|
||||
|
||||
// Lazy expiry: clean up the temp file and session entry when the state is accessed after TTL.
|
||||
$createdAt = (int) ($state['created_at'] ?? 0);
|
||||
if ($createdAt > 0 && (time() - $createdAt) > self::STATE_TTL_SECONDS) {
|
||||
$this->importTempFileService->delete((string) ($state['path'] ?? ''));
|
||||
@@ -54,9 +62,12 @@ class ImportStateStoreService
|
||||
|
||||
public function clearState(string $token): void
|
||||
{
|
||||
if (!isset($_SESSION[self::SESSION_KEY]) || !is_array($_SESSION[self::SESSION_KEY])) {
|
||||
$states = $this->sessionStore->get(self::SESSION_KEY, []);
|
||||
if (!is_array($states)) {
|
||||
return;
|
||||
}
|
||||
unset($_SESSION[self::SESSION_KEY][$token]);
|
||||
|
||||
unset($states[$token]);
|
||||
$this->sessionStore->set(self::SESSION_KEY, $states);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user