<?php
declare(strict_types=1);
namespace CioCustomerDeletionCleanup\Subscriber;
use CioDataRetention\Event\BeforeCustomerDeletionEvent;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
// CIO-AI-Driven
final class CustomerDeletionCleanupSubscriber implements EventSubscriberInterface
{
private const AVAILABILITY_NOTIFICATION_TABLE = 'cio_availability_notification';
public static function getSubscribedEvents(): array
{
return [
BeforeCustomerDeletionEvent::class => 'onBeforeCustomerDeletion',
];
}
public function onBeforeCustomerDeletion(BeforeCustomerDeletionEvent $event): void
{
$connection = $event->getConnection();
// T55792: email-based cleanup
if ($this->tableExists($connection, self::AVAILABILITY_NOTIFICATION_TABLE)) {
try {
$connection->executeStatement(
'DELETE FROM `cio_availability_notification` WHERE `email` = :email',
['email' => $event->getCustomerEmail()]
);
} catch (Exception $exception) {
// Cleanup must never block customer deletion.
}
}
}
// CIO-AI-Driven
private function tableExists(Connection $connection, string $tableName): bool
{
try {
// Doctrine DBAL 3: createSchemaManager(); DBAL 2: getSchemaManager();
try {
$schemaManager = $connection->createSchemaManager();
} catch (\Throwable $throwable) {
$schemaManager = $connection->getSchemaManager();
}
return $schemaManager->tablesExist([$tableName]);
} catch (\Throwable $throwable) {
return false;
}
}
}