src/EventSubscriber/UnsubscribesSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\PhoneNumberUnsubscribes;
  4. use App\Entity\Unsubscribes;
  5. use App\Service\GmailAddressService;
  6. use DateTime;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class UnsubscribesSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var GmailAddressService
  14.      */
  15.     private $gmailAddressService;
  16.     public function __construct(GmailAddressService $gmailAddressService)
  17.     {
  18.         $this->gmailAddressService $gmailAddressService;
  19.     }
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             BeforeEntityPersistedEvent::class => 'beforeEntityPersisted',
  24.             BeforeEntityUpdatedEvent::class => 'beforeEntityUpdated'
  25.         ];
  26.     }
  27.     public function beforeEntityPersisted(BeforeEntityPersistedEvent $event)
  28.     {
  29.         $entity $event->getEntityInstance();
  30.         if ($entity instanceof Unsubscribes) {
  31.             $sanitized $this->gmailAddressService->sanitize(array($entity->getRecipientemail()));
  32.             $entity->setRecipientemail($sanitized[0]);
  33.             if (!$entity->getTimestamp()) {
  34.                 $entity->setTimestamp(new DateTime());
  35.             }
  36.         }
  37.         if ($entity instanceof PhoneNumberUnsubscribes) {
  38.             if (!$entity->getTimestamp()) {
  39.                 $entity->setTimestamp(new DateTime());
  40.             }
  41.         }
  42.     }
  43.     public function beforeEntityUpdated(BeforeEntityUpdatedEvent $event)
  44.     {
  45.         $entity $event->getEntityInstance();
  46.         if (!$entity instanceof Unsubscribes) {
  47.             return;
  48.         }
  49.         $sanitized $this->gmailAddressService->sanitize(array($entity->getRecipientemail()));
  50.         $entity->setRecipientemail($sanitized[0]);
  51.     }
  52. }