53 lines
1.6 KiB
PHP
53 lines
1.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\AiMascotCampaign\Observer;
|
|
|
|
use Esun\AiMascotCampaign\Model\CampaignContext;
|
|
use Esun\AiMascotCampaign\Model\RegistrationRecorder;
|
|
use Magento\Framework\Event\Observer;
|
|
use Magento\Framework\Event\ObserverInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class RecordCampaignRegistration implements ObserverInterface
|
|
{
|
|
public function __construct(
|
|
private readonly CampaignContext $campaignContext,
|
|
private readonly RegistrationRecorder $registrationRecorder,
|
|
private readonly LoggerInterface $logger
|
|
) {
|
|
}
|
|
|
|
public function execute(Observer $observer): void
|
|
{
|
|
$campaignCode = $this->campaignContext->consume();
|
|
if ($campaignCode === null) {
|
|
return;
|
|
}
|
|
|
|
$customer = $observer->getEvent()->getCustomer();
|
|
$customerId = (int)($customer?->getId() ?? 0);
|
|
$email = (string)($customer?->getEmail() ?? '');
|
|
if ($customerId <= 0 || trim($email) === '') {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$this->registrationRecorder->record(
|
|
$campaignCode,
|
|
$customerId,
|
|
$email,
|
|
(int)$customer->getWebsiteId(),
|
|
(int)$customer->getStoreId()
|
|
);
|
|
} catch (\Throwable $exception) {
|
|
// Campaign tracking must not turn a successful account creation into an error page.
|
|
$this->logger->error('Unable to record AI Mascot campaign registration.', [
|
|
'campaign_code' => $campaignCode,
|
|
'customer_id' => $customerId,
|
|
'exception' => $exception,
|
|
]);
|
|
}
|
|
}
|
|
}
|