This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,46 @@
<?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;
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Esun\AiMascotCampaign\Model;
use Magento\Framework\App\ResourceConnection;
class RegistrationRecorder
{
private const TABLE_NAME = 'esun_campaign_registration';
public function __construct(private readonly ResourceConnection $resourceConnection)
{
}
public function record(
string $campaignCode,
int $customerId,
string $email,
int $websiteId,
int $storeId
): void {
$normalizedEmail = strtolower(trim($email));
if ($customerId <= 0 || $normalizedEmail === '') {
return;
}
$connection = $this->resourceConnection->getConnection();
$connection->insertOnDuplicate(
$this->resourceConnection->getTableName(self::TABLE_NAME),
[
'campaign_code' => $campaignCode,
'customer_id' => $customerId,
'email' => $normalizedEmail,
'website_id' => $websiteId,
'store_id' => $storeId,
],
[]
);
}
}
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Esun\AiMascotCampaign\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Registration extends AbstractDb
{
protected function _construct(): void
{
$this->_init('esun_campaign_registration', 'entity_id');
}
}