src/Contact/EventSubscriber/AddCompanyContactPersonSubscriber.php line 55

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2023 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 2023 TECLA Consulting Group oü
  16.  */
  17. namespace App\Contact\EventSubscriber;
  18. use App\Contact\Event\ContactAccess;
  19. use App\Contact\Event\Relationship\Ownership;
  20. use App\Contact\Form\Dto\Factory\ContactRelationshipCompanyContactPersonCreateDtoFactoryInterface;
  21. use App\Contact\Model\ContactInterface;
  22. use App\Contact\Query\ContactRelationshipCompanyContactPerson\FindOneByCompanyAndContactPersonQueryInterface;
  23. use App\Contact\Service\ContactRelationshipCompanyContactPersonServiceInterface;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. final class AddCompanyContactPersonSubscriber implements EventSubscriberInterface
  26. {
  27.     private FindOneByCompanyAndContactPersonQueryInterface $findByUniqueKeyQuery;
  28.     private ContactRelationshipCompanyContactPersonCreateDtoFactoryInterface $contactPersonDtoFactory;
  29.     private ContactRelationshipCompanyContactPersonServiceInterface $contactPersonService;
  30.     public function __construct(
  31.         FindOneByCompanyAndContactPersonQueryInterface $findByUniqueKeyQuery,
  32.         ContactRelationshipCompanyContactPersonCreateDtoFactoryInterface $contactPersonDtoFactory,
  33.         ContactRelationshipCompanyContactPersonServiceInterface $contactPersonService
  34.     ) {
  35.         $this->findByUniqueKeyQuery $findByUniqueKeyQuery;
  36.         $this->contactPersonDtoFactory $contactPersonDtoFactory;
  37.         $this->contactPersonService $contactPersonService;
  38.     }
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             ContactAccess\CreatedEvent::class => 'onContactAccessCreated',
  43.             Ownership\CreatedEvent::class => 'onOwnershipCreated',
  44.         ];
  45.     }
  46.     public function onContactAccessCreated(ContactAccess\CreatedEvent $event): void
  47.     {
  48.         $contactAccess $event->getContactAccess();
  49.         $this->addContactPersonIfNotExists(
  50.             $contactAccess->getContact(),
  51.             $contactAccess->getAccessor()
  52.         );
  53.     }
  54.     public function onOwnershipCreated(Ownership\CreatedEvent $event): void
  55.     {
  56.         $ownership $event->getRelationship();
  57.         $this->addContactPersonIfNotExists(
  58.             $ownership->getCompany(),
  59.             $ownership->getOwner()
  60.         );
  61.     }
  62.     private function addContactPersonIfNotExists(ContactInterface $companyContactInterface $person): void
  63.     {
  64.         if (ContactInterface::TYPE_COMPANY !== $company->getType()) {
  65.             return;
  66.         }
  67.         if (ContactInterface::TYPE_PERSON !== $person->getType()) {
  68.             return;
  69.         }
  70.         if ($this->findByUniqueKeyQuery->execute($company->getId(), $person->getId())) {
  71.             return;
  72.         }
  73.         $contactPersonDto $this->contactPersonDtoFactory->create(
  74.             $company,
  75.             $person,
  76.             $person->getContactEmail()?->getEmail(),
  77.             $person->getPhone()
  78.         );
  79.         $this->contactPersonService->createFromRelationshipContactPersonCreateDto($contactPersonDto);
  80.     }
  81. }