src/Contact/EventSubscriber/AddCompanyContactPersonSubscriber.php line 55
<?phpdeclare(strict_types=1);/*** Copyright (c) 2023 TECLA Consulting Group oü.* All rights reserved.** This unpublished material is proprietary to TECLA Consulting Group oü.* All rights reserved. The methods and* techniques described herein are considered trade secrets* and/or confidential. Reproduction or distribution, in whole* or in part, is forbidden except by express written permission* of TECLA Consulting Group oü.** @author Matúš Sýkorjak <matus@tecla.no>* @copyright 2023 TECLA Consulting Group oü*/namespace App\Contact\EventSubscriber;use App\Contact\Event\ContactAccess;use App\Contact\Event\Relationship\Ownership;use App\Contact\Form\Dto\Factory\ContactRelationshipCompanyContactPersonCreateDtoFactoryInterface;use App\Contact\Model\ContactInterface;use App\Contact\Query\ContactRelationshipCompanyContactPerson\FindOneByCompanyAndContactPersonQueryInterface;use App\Contact\Service\ContactRelationshipCompanyContactPersonServiceInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;final class AddCompanyContactPersonSubscriber implements EventSubscriberInterface{private FindOneByCompanyAndContactPersonQueryInterface $findByUniqueKeyQuery;private ContactRelationshipCompanyContactPersonCreateDtoFactoryInterface $contactPersonDtoFactory;private ContactRelationshipCompanyContactPersonServiceInterface $contactPersonService;public function __construct(FindOneByCompanyAndContactPersonQueryInterface $findByUniqueKeyQuery,ContactRelationshipCompanyContactPersonCreateDtoFactoryInterface $contactPersonDtoFactory,ContactRelationshipCompanyContactPersonServiceInterface $contactPersonService) {$this->findByUniqueKeyQuery = $findByUniqueKeyQuery;$this->contactPersonDtoFactory = $contactPersonDtoFactory;$this->contactPersonService = $contactPersonService;}public static function getSubscribedEvents(): array{return [ContactAccess\CreatedEvent::class => 'onContactAccessCreated',Ownership\CreatedEvent::class => 'onOwnershipCreated',];}public function onContactAccessCreated(ContactAccess\CreatedEvent $event): void{$contactAccess = $event->getContactAccess();$this->addContactPersonIfNotExists($contactAccess->getContact(),$contactAccess->getAccessor());}public function onOwnershipCreated(Ownership\CreatedEvent $event): void{$ownership = $event->getRelationship();$this->addContactPersonIfNotExists($ownership->getCompany(),$ownership->getOwner());}private function addContactPersonIfNotExists(ContactInterface $company, ContactInterface $person): void{if (ContactInterface::TYPE_COMPANY !== $company->getType()) {return;}if (ContactInterface::TYPE_PERSON !== $person->getType()) {return;}if ($this->findByUniqueKeyQuery->execute($company->getId(), $person->getId())) {return;}$contactPersonDto = $this->contactPersonDtoFactory->create($company,$person,$person->getContactEmail()?->getEmail(),$person->getPhone());$this->contactPersonService->createFromRelationshipContactPersonCreateDto($contactPersonDto);}}