src/Contact/EventSubscriber/DeleteContactFromGroupsSubscriber.php line 52

  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\ContactDeletedEvent;
  19. use App\Contact\Query\GroupItem\FindAllByContactQueryInterface;
  20. use App\Contact\Security\ContactContextInterface;
  21. use App\Contact\Service\GroupItemServiceInterface;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. final class DeleteContactFromGroupsSubscriber implements EventSubscriberInterface
  24. {
  25.     private ContactContextInterface $contactContext;
  26.     private FindAllByContactQueryInterface $findAllByContactQuery;
  27.     private GroupItemServiceInterface $itemService;
  28.     public function __construct(
  29.         ContactContextInterface        $contactContext,
  30.         FindAllByContactQueryInterface $findAllByContactQuery,
  31.         GroupItemServiceInterface      $itemService
  32.     ) {
  33.         $this->contactContext $contactContext;
  34.         $this->findAllByContactQuery $findAllByContactQuery;
  35.         $this->itemService $itemService;
  36.     }
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             ContactDeletedEvent::class => 'onContactDeletedEvent',
  41.         ];
  42.     }
  43.     public function onContactDeletedEvent(ContactDeletedEvent $event): void
  44.     {
  45.         $currentContext $this->contactContext->getCurrent();
  46.         if (null === $currentContext) {
  47.             return;
  48.         }
  49.         $contact $event->getContact();
  50.         $groupItems $this->findAllByContactQuery->execute($currentContext$contact);
  51.         foreach ($groupItems as $groupItem) {
  52.             $this->itemService->delete($groupItem);
  53.         }
  54.     }
  55. }