47 lines
1.1 KiB
PHP
47 lines
1.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\AiMascotCampaign\Model;
|
|
|
|
use Magento\Customer\Model\Session as CustomerSession;
|
|
|
|
class CampaignContext
|
|
{
|
|
public const CAMPAIGN_CODE = 'ai_mascot_2026';
|
|
|
|
private const SESSION_KEY = 'esun_ai_mascot_campaign';
|
|
private const MAX_AGE_SECONDS = 7200;
|
|
|
|
public function __construct(private readonly CustomerSession $customerSession)
|
|
{
|
|
}
|
|
|
|
public function start(): void
|
|
{
|
|
$this->customerSession->setData(self::SESSION_KEY, [
|
|
'code' => self::CAMPAIGN_CODE,
|
|
'started_at' => time(),
|
|
]);
|
|
}
|
|
|
|
public function consume(): ?string
|
|
{
|
|
$data = $this->customerSession->getData(self::SESSION_KEY);
|
|
$this->customerSession->unsetData(self::SESSION_KEY);
|
|
|
|
if (!is_array($data)
|
|
|| ($data['code'] ?? null) !== self::CAMPAIGN_CODE
|
|
|| !isset($data['started_at'])
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
$age = time() - (int)$data['started_at'];
|
|
if ($age < 0 || $age > self::MAX_AGE_SECONDS) {
|
|
return null;
|
|
}
|
|
|
|
return self::CAMPAIGN_CODE;
|
|
}
|
|
}
|