45 lines
1.2 KiB
PHP
45 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\MultiLocaleTranslation\Model\Queue;
|
|
|
|
use Magento\Backend\Model\Session;
|
|
|
|
class SelectionRegistry
|
|
{
|
|
private const SESSION_PREFIX = 'esun_mlt_queue_selection_';
|
|
|
|
public function __construct(private readonly Session $session)
|
|
{
|
|
}
|
|
|
|
/** @param int[] $entityIds */
|
|
public function save(string $entityType, array $entityIds): string
|
|
{
|
|
$token = bin2hex(random_bytes(16));
|
|
$this->session->setData(self::SESSION_PREFIX . $token, [
|
|
'entity_type' => $entityType,
|
|
'entity_ids' => array_values(array_unique(array_map('intval', $entityIds))),
|
|
]);
|
|
return $token;
|
|
}
|
|
|
|
/** @return array{entity_type:string,entity_ids:int[]}|null */
|
|
public function get(string $token): ?array
|
|
{
|
|
if (preg_match('/^[a-f0-9]{32}$/', $token) !== 1) {
|
|
return null;
|
|
}
|
|
$selection = $this->session->getData(self::SESSION_PREFIX . $token);
|
|
return is_array($selection) ? $selection : null;
|
|
}
|
|
|
|
public function clear(string $token): void
|
|
{
|
|
if (preg_match('/^[a-f0-9]{32}$/', $token) === 1) {
|
|
$this->session->unsetData(self::SESSION_PREFIX . $token);
|
|
}
|
|
}
|
|
}
|