<?php
declare(strict_types=1);
namespace Harmonizely\User;
use Doctrine\ORM\EntityManagerInterface;
use Harmonizely\Core\Validation\Contract\IDTOValidator;
use Harmonizely\Integration\Simplypay\Contract\SimplypayGetIntegrationConfigInterface;
use Harmonizely\Model\UserInterface;
use Harmonizely\Repository\Contract\ICalendarServiceRepository;
use Harmonizely\Service\Simplypay\Contract\CommonApiClientInterface;
use Harmonizely\Service\Simplypay\DTO\Common\Request\BlockMerchantRequestDTO;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Harmonizely\Core\BackgroundWork\Contract\IClient;
use Harmonizely\Background\Data\DirectoryDeleteCompanyData;
final class DeleteUser
{
private IDTOValidator $validator;
private EntityManagerInterface $entityManager;
private EventDispatcherInterface $eventDispatcher;
/**
* @var IClient
*/
private IClient $backgroundClient;
/**
* @var ICalendarServiceRepository
*/
private ICalendarServiceRepository $calendarServiceRepository;
/**
* @var CommonApiClientInterface
*/
private CommonApiClientInterface $simplepayCommonApiClient;
/**
* @var SimplypayGetIntegrationConfigInterface
*/
private SimplypayGetIntegrationConfigInterface $getIntegrationConfig;
/**
* @param IDTOValidator $validator
* @param EntityManagerInterface $entityManager
* @param EventDispatcherInterface $eventDispatcher
* @param IClient $backgroundClient
* @param ICalendarServiceRepository $calendarServiceRepository
*/
public function __construct(
IDTOValidator $validator,
EntityManagerInterface $entityManager,
EventDispatcherInterface $eventDispatcher,
IClient $backgroundClient,
ICalendarServiceRepository $calendarServiceRepository,
CommonApiClientInterface $simplepayCommonApiClient,
SimplypayGetIntegrationConfigInterface $getIntegrationConfig
)
{
$this->validator = $validator;
$this->entityManager = $entityManager;
$this->eventDispatcher = $eventDispatcher;
$this->backgroundClient = $backgroundClient;
$this->calendarServiceRepository = $calendarServiceRepository;
$this->simplepayCommonApiClient = $simplepayCommonApiClient;
$this->getIntegrationConfig = $getIntegrationConfig;
}
public function __invoke(
UserInterface $user,
DeleteUserRequest $deleteUserRequest
): void
{
$this->validator->validate($deleteUserRequest);
$userId = $user->getId();
$userSimplypayConfig = $this->getIntegrationConfig->perform($user, true);
$organization = $user->getDefaultOrganization();
$organizationSimplypayConfig = null;
if ($organization && $organization->isOrganizationOwner($user) && count($organization->getEnabledOwners()) === 1) {
$organizationSimplypayConfig = $this->getIntegrationConfig->perform($user, false, true);
}
if ($deleteUserRequest->isDelete()) {
$user->clearOrganizationTag();
$user->clearUserCategories();
$this->calendarServiceRepository->deleteByUser($user);
$this->entityManager->remove($user);
} else {
$user->setEnabled(false);
}
$this->entityManager->flush();
if ($userSimplypayConfig) {
$this->simplepayCommonApiClient->blockMerchant(new BlockMerchantRequestDTO(
$userSimplypayConfig->getMerchant()
));
}
if ($organizationSimplypayConfig) {
$this->simplepayCommonApiClient->blockMerchant(new BlockMerchantRequestDTO(
$organizationSimplypayConfig->getMerchant()
));
}
$this->backgroundClient->add(new DirectoryDeleteCompanyData($user->getDirectoryCompanyLogin()));
$this->backgroundClient->runJobs();
$userDeletedEvent = new UserDeletedEvent($user, $userId, $deleteUserRequest->getReason(), $deleteUserRequest->isDelete());
$this->eventDispatcher->dispatch($userDeletedEvent, UserDeletedEvent::NAME);
}
public function perform(
UserInterface $user,
DeleteUserRequest $deleteUserRequest
): void
{
$this($user, $deleteUserRequest);
}
}