<?php
namespace Harmonizely\Service\SuperAdmin\Panel\Api\Customer;
use Harmonizely\Core\Exception\AccessDeniedException;
use Harmonizely\Core\Exception\NotFoundException;
use Harmonizely\Core\Security\Contract\ISuperAdminSecurityHelper;
use Harmonizely\Model\UserInterface;
use Harmonizely\Repository\Contract\IUserRepository;
use Harmonizely\Service\SuperAdmin\Event\UserActionEvent;
use Harmonizely\Service\SuperAdmin\Panel\AccessVoters\CustomerVoter;
use Harmonizely\Service\SuperAdmin\Panel\Api\Customer\Contract\ICustomerDeleteService;
use Harmonizely\Types\SuperAdmin\UserActionsActionType;
use Harmonizely\Types\SuperAdmin\UserActionsObjectType;
use Harmonizely\User\DeleteUser;
use Harmonizely\User\DeleteUserRequest;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class CustomerDeleteService implements ICustomerDeleteService
{
/**
* @var ISuperAdminSecurityHelper
*/
private ISuperAdminSecurityHelper $security;
/**
* @var EventDispatcherInterface
*/
private EventDispatcherInterface $eventDispatcher;
/**
* @var IUserRepository
*/
private IUserRepository $userRepository;
/**
* @var DeleteUser
*/
private DeleteUser $deleteUser;
/**
* @param ISuperAdminSecurityHelper $security
* @param EventDispatcherInterface $eventDispatcher
* @param IUserRepository $userRepository
* @param DeleteUser $deleteUser
*/
public function __construct(
ISuperAdminSecurityHelper $security,
EventDispatcherInterface $eventDispatcher,
IUserRepository $userRepository,
DeleteUser $deleteUser
)
{
$this->security = $security;
$this->eventDispatcher = $eventDispatcher;
$this->userRepository = $userRepository;
$this->deleteUser = $deleteUser;
}
/**
* @param int $id
* @return void
* @throws NotFoundException
* @throws AccessDeniedException
*/
public function perform(int $id): void
{
/** @var UserInterface $user */
$user = $this->userRepository->find($id);
if (!($user instanceof UserInterface)) {
throw new NotFoundException('User not found');
}
$this->security->denyUnlessGranted(CustomerVoter::DELETE, $user);
$request = new DeleteUserRequest();
$request->setDelete(true);
$request->setReason('support_delete_user');
$this->deleteUser->perform($user, $request);
$this->eventDispatcher->dispatch(new UserActionEvent(
UserActionsActionType::TYPE_DELETE,
UserActionsObjectType::TYPE_CUSTOMER,
$user->getId()
), UserActionEvent::NAME);
}
}