src/Contact/EventSubscriber/GrantContactPermissionsSubscriber.php line 89

  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\ContactAccess;
  19. use App\Contact\Event\ContactCreatedEvent;
  20. use App\Contact\Event\ContactRelationshipCompanyContactPerson;
  21. use App\Contact\Event\Relationship\Employment;
  22. use App\Contact\Event\Relationship\Ownership;
  23. use App\Contact\Security\ContactContextInterface;
  24. use App\Contact\Service\ContactPermissionService;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. class GrantContactPermissionsSubscriber implements EventSubscriberInterface
  27. {
  28.     private ContactContextInterface $contactContext;
  29.     private ContactPermissionService $contactPermissionService;
  30.     public function __construct(
  31.         ContactContextInterface $contactContext,
  32.         ContactPermissionService $contactPermissionService
  33.     ) {
  34.         $this->contactContext $contactContext;
  35.         $this->contactPermissionService $contactPermissionService;
  36.     }
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             ContactCreatedEvent::class => 'onContactCreated',
  41.             ContactAccess\CreatedEvent::class => 'onAccessCreated',
  42.             ContactRelationshipCompanyContactPerson\CreatedEvent::class => 'onContactPersonCreated',
  43.             Employment\CreatedEvent::class => 'onEmploymentCreated',
  44.             Ownership\CreatedEvent::class => 'onOwnershipCreated',
  45.             Ownership\UpdatedEvent::class => 'onOwnershipUpdated',
  46.         ];
  47.     }
  48.     public function onContactCreated(ContactCreatedEvent $event): void
  49.     {
  50.         $this->contactPermissionService->grantPermission(
  51.             $event->getContact(),
  52.             $this->contactContext->getCurrent()
  53.         );
  54.     }
  55.     public function onAccessCreated(ContactAccess\CreatedEvent $event): void
  56.     {
  57.         $access $event->getContactAccess();
  58.         $this->contactPermissionService->grantPermission(
  59.             $access->getContact(),
  60.             $access->getAccessor()
  61.         );
  62.         $this->contactPermissionService->grantPermission(
  63.             $access->getAccessor(),
  64.             $access->getContact()
  65.         );
  66.     }
  67.     public function onContactPersonCreated(ContactRelationshipCompanyContactPerson\CreatedEvent $event): void
  68.     {
  69.         $relationship $event->getRelationship();
  70.         $this->contactPermissionService->grantPermission(
  71.             $relationship->getContactPerson(),
  72.             $relationship->getCompany()
  73.         );
  74.     }
  75.     public function onEmploymentCreated(Employment\CreatedEvent $event): void
  76.     {
  77.         $relationship $event->getRelationship();
  78.         $this->contactPermissionService->grantPermission(
  79.             $relationship->getEmployee(),
  80.             $relationship->getEmployer()
  81.         );
  82.     }
  83.     public function onOwnershipCreated(Ownership\CreatedEvent $event): void
  84.     {
  85.         $relationship $event->getRelationship();
  86.         $this->contactPermissionService->grantPermission(
  87.             $relationship->getOwner(),
  88.             $relationship->getCompany()
  89.         );
  90.     }
  91.     public function onOwnershipUpdated(Ownership\UpdatedEvent $event): void
  92.     {
  93.         $relationship $event->getRelationship();
  94.         $this->contactPermissionService->grantPermission(
  95.             $relationship->getOwner(),
  96.             $relationship->getCompany()
  97.         );
  98.     }
  99. }