src/User/DeleteUser.php line 121

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Harmonizely\Core\Validation\Contract\IDTOValidator;
  6. use Harmonizely\Integration\Simplypay\Contract\SimplypayGetIntegrationConfigInterface;
  7. use Harmonizely\Model\UserInterface;
  8. use Harmonizely\Repository\Contract\ICalendarServiceRepository;
  9. use Harmonizely\Service\Simplypay\Contract\CommonApiClientInterface;
  10. use Harmonizely\Service\Simplypay\DTO\Common\Request\BlockMerchantRequestDTO;
  11. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  12. use Harmonizely\Core\BackgroundWork\Contract\IClient;
  13. use Harmonizely\Background\Data\DirectoryDeleteCompanyData;
  14. final class DeleteUser
  15. {
  16.     private IDTOValidator $validator;
  17.     private EntityManagerInterface $entityManager;
  18.     private EventDispatcherInterface $eventDispatcher;
  19.     /**
  20.      * @var IClient
  21.      */
  22.     private IClient $backgroundClient;
  23.     /**
  24.      * @var ICalendarServiceRepository
  25.      */
  26.     private ICalendarServiceRepository $calendarServiceRepository;
  27.     /**
  28.      * @var CommonApiClientInterface
  29.      */
  30.     private CommonApiClientInterface $simplepayCommonApiClient;
  31.     /**
  32.      * @var SimplypayGetIntegrationConfigInterface
  33.      */
  34.     private SimplypayGetIntegrationConfigInterface $getIntegrationConfig;
  35.     /**
  36.      * @param IDTOValidator $validator
  37.      * @param EntityManagerInterface $entityManager
  38.      * @param EventDispatcherInterface $eventDispatcher
  39.      * @param IClient $backgroundClient
  40.      * @param ICalendarServiceRepository $calendarServiceRepository
  41.      */
  42.     public function __construct(
  43.         IDTOValidator $validator,
  44.         EntityManagerInterface $entityManager,
  45.         EventDispatcherInterface $eventDispatcher,
  46.         IClient $backgroundClient,
  47.         ICalendarServiceRepository $calendarServiceRepository,
  48.         CommonApiClientInterface $simplepayCommonApiClient,
  49.         SimplypayGetIntegrationConfigInterface $getIntegrationConfig
  50.     )
  51.     {
  52.         $this->validator $validator;
  53.         $this->entityManager $entityManager;
  54.         $this->eventDispatcher $eventDispatcher;
  55.         $this->backgroundClient $backgroundClient;
  56.         $this->calendarServiceRepository $calendarServiceRepository;
  57.         $this->simplepayCommonApiClient $simplepayCommonApiClient;
  58.         $this->getIntegrationConfig $getIntegrationConfig;
  59.     }
  60.     public function __invoke(
  61.         UserInterface $user,
  62.         DeleteUserRequest $deleteUserRequest
  63.     ): void
  64.     {
  65.         $this->validator->validate($deleteUserRequest);
  66.         $userId $user->getId();
  67.         $userSimplypayConfig $this->getIntegrationConfig->perform($usertrue);
  68.         $organization $user->getDefaultOrganization();
  69.         $organizationSimplypayConfig null;
  70.         if ($organization && $organization->isOrganizationOwner($user) && count($organization->getEnabledOwners()) === 1) {
  71.             $organizationSimplypayConfig $this->getIntegrationConfig->perform($userfalsetrue);
  72.         }
  73.         if ($deleteUserRequest->isDelete()) {
  74.             $user->clearOrganizationTag();
  75.             $user->clearUserCategories();
  76.             $this->calendarServiceRepository->deleteByUser($user);
  77.             $this->entityManager->remove($user);
  78.         } else {
  79.             $user->setEnabled(false);
  80.         }
  81.         $this->entityManager->flush();
  82.         if ($userSimplypayConfig) {
  83.             $this->simplepayCommonApiClient->blockMerchant(new BlockMerchantRequestDTO(
  84.                 $userSimplypayConfig->getMerchant()
  85.             ));
  86.         }
  87.         if ($organizationSimplypayConfig) {
  88.             $this->simplepayCommonApiClient->blockMerchant(new BlockMerchantRequestDTO(
  89.                 $organizationSimplypayConfig->getMerchant()
  90.             ));
  91.         }
  92.         $this->backgroundClient->add(new DirectoryDeleteCompanyData($user->getDirectoryCompanyLogin()));
  93.         $this->backgroundClient->runJobs();
  94.         $userDeletedEvent = new UserDeletedEvent($user$userId$deleteUserRequest->getReason(), $deleteUserRequest->isDelete());
  95.         $this->eventDispatcher->dispatch($userDeletedEventUserDeletedEvent::NAME);
  96.     }
  97.     public function perform(
  98.         UserInterface $user,
  99.         DeleteUserRequest $deleteUserRequest
  100.     ): void
  101.     {
  102.         $this($user$deleteUserRequest);
  103.     }
  104. }