src/Contact/EventSubscriber/GrantInitialContactAccessSubscriber.php line 48

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2019 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2019 TECLA Consulting Group oü
  16.  */
  17. namespace App\Contact\EventSubscriber;
  18. use App\Contact\Event\ContactCreatedEvent;
  19. use App\Contact\Model\ContactInterface;
  20. use App\Contact\Service\ContactAccessServiceInterface;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. class GrantInitialContactAccessSubscriber implements EventSubscriberInterface
  24. {
  25.     private EntityManagerInterface $entityManager;
  26.     private ContactAccessServiceInterface $contactAccessService;
  27.     public function __construct(
  28.         EntityManagerInterface $entityManager,
  29.         ContactAccessServiceInterface $contactAccessService
  30.     ) {
  31.         $this->entityManager $entityManager;
  32.         $this->contactAccessService $contactAccessService;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             ContactCreatedEvent::class => 'onContactCreated',
  38.         ];
  39.     }
  40.     public function onContactCreated(ContactCreatedEvent $event): void
  41.     {
  42.         $contact $event->getContact();
  43.         if (ContactInterface::TYPE_PERSON === $contact->getType()) {
  44.             return;
  45.         }
  46.         if (null === $contact->getContactEmail()->getEmail()) {
  47.             return;
  48.         }
  49.         $repository $this->entityManager->getRepository(ContactInterface::class);
  50.         $initialAccessor $repository->findOnePersonByUniqueInformation($contact->getContactEmail()->getEmail());
  51.         if (null === $initialAccessor) {
  52.             return;
  53.         }
  54.         $this->contactAccessService->grantInitialAccess($contact$initialAccessor);
  55.     }
  56. }