custom/plugins/CioCustomerDeletionCleanup/src/Subscriber/CustomerDeletionCleanupSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CioCustomerDeletionCleanup\Subscriber;
  4. use CioDataRetention\Event\BeforeCustomerDeletionEvent;
  5. use Doctrine\DBAL\Connection;
  6. use Doctrine\DBAL\Exception;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. // CIO-AI-Driven
  9. final class CustomerDeletionCleanupSubscriber implements EventSubscriberInterface
  10. {
  11.     private const AVAILABILITY_NOTIFICATION_TABLE = 'cio_availability_notification';
  12.     public static function getSubscribedEvents(): array
  13.     {
  14.         return [
  15.             BeforeCustomerDeletionEvent::class => 'onBeforeCustomerDeletion',
  16.         ];
  17.     }
  18.     public function onBeforeCustomerDeletion(BeforeCustomerDeletionEvent $event): void
  19.     {
  20.         $connection = $event->getConnection();
  21.         // T55792: email-based cleanup
  22.         if ($this->tableExists($connection, self::AVAILABILITY_NOTIFICATION_TABLE)) {
  23.             try {
  24.                 $connection->executeStatement(
  25.                     'DELETE FROM `cio_availability_notification` WHERE `email` = :email',
  26.                     ['email' => $event->getCustomerEmail()]
  27.                 );
  28.             } catch (Exception $exception) {
  29.                 // Cleanup must never block customer deletion.
  30.             }
  31.         }
  32.     }
  33.     // CIO-AI-Driven
  34.     private function tableExists(Connection $connection, string $tableName): bool
  35.     {
  36.         try {
  37.             // Doctrine DBAL 3: createSchemaManager(); DBAL 2: getSchemaManager();
  38.             try {
  39.                 $schemaManager = $connection->createSchemaManager();
  40.             } catch (\Throwable $throwable) {
  41.                 $schemaManager = $connection->getSchemaManager();
  42.             }
  43.             return $schemaManager->tablesExist([$tableName]);
  44.         } catch (\Throwable $throwable) {
  45.             return false;
  46.         }
  47.     }
  48. }