src/Note/EventSubscriber/GrantPermissionSubscriber.php line 70
<?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\Note\EventSubscriber;use App\Contact\Dto\Factory\ContactDtoFactoryInterface;use App\Contact\Model\ContactInterface;use App\Contact\Model\Identity\ContactId;use App\Contact\Repository\ContactRepositoryInterface;use App\Contact\Security\ContactContextInterface;use App\Note\Dto\Factory\NoteDtoFactoryInterface;use App\Note\Dto\Factory\NotePermissionDtoFactoryInterface;use App\Note\Event\Note\CreatedEvent;use App\Note\Model\NoteInterface;use App\Note\Service\NotePermissionServiceInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;final class GrantPermissionSubscriber implements EventSubscriberInterface{private ContactContextInterface $contactContext;private NotePermissionDtoFactoryInterface $notePermissionDtoFactory;private NoteDtoFactoryInterface $noteDtoFactory;private ContactDtoFactoryInterface $contactDtoFactory;private NotePermissionServiceInterface $notePermissionService;private ContactRepositoryInterface $contactRepository;public function __construct(ContactContextInterface $contactContext,NotePermissionDtoFactoryInterface $notePermissionDtoFactory,NoteDtoFactoryInterface $noteDtoFactory,ContactDtoFactoryInterface $contactDtoFactory,NotePermissionServiceInterface $notePermissionService,ContactRepositoryInterface $contactRepository) {$this->contactContext = $contactContext;$this->notePermissionDtoFactory = $notePermissionDtoFactory;$this->noteDtoFactory = $noteDtoFactory;$this->contactDtoFactory = $contactDtoFactory;$this->notePermissionService = $notePermissionService;$this->contactRepository = $contactRepository;}public static function getSubscribedEvents(): array{return [CreatedEvent::class => 'onCreatedEvent',];}public function onCreatedEvent(CreatedEvent $event): void{if (!$this->contactContext->getCurrent()) {return;}if ($this->isTeclaContext()) {$this->grantTeclaPermission($event->getNote());} else {$this->grantPermission($event->getNote(), $this->contactContext->getCurrent());}}private function isTeclaContext(): bool{$allowedIds = ['a1e7e2ae-e4be-11ea-848c-3448ed126851','a1e7e4fc-e4be-11ea-9d2b-3448ed126851','a1e9240c-e4be-11ea-8071-3448ed126851','a1eaf35e-e4be-11ea-8f03-3448ed126851','a1f0badc-e4be-11ea-9318-3448ed126851',];if (false === \in_array($this->contactContext->getCurrent()?->getId()?->toRfc4122(), $allowedIds)) {return false;}return true;}private function grantPermission(NoteInterface $note, ContactInterface $contact): void{$dto = $this->notePermissionDtoFactory->create()->setCreatedAt($note->getCreatedAt())->setNote($this->noteDtoFactory->createReferenceFromModel($note))->setContact($this->contactDtoFactory->createReferenceFromModel($contact));$this->notePermissionService->createFromDto($dto);}private function grantTeclaPermission(NoteInterface $note): void{$allowedIds = ['a1e7e2ae-e4be-11ea-848c-3448ed126851','a1e7e4fc-e4be-11ea-9d2b-3448ed126851','a1e9240c-e4be-11ea-8071-3448ed126851','a1eaf35e-e4be-11ea-8f03-3448ed126851','a1f0badc-e4be-11ea-9318-3448ed126851',];foreach ($allowedIds as $allowedId) {$contact = $this->contactRepository->find(ContactId::createFromString($allowedId));$this->grantPermission($note, $contact);}}}