src/Service/SuperAdmin/Panel/Api/Customer/CustomerDeleteService.php line 80

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Service\SuperAdmin\Panel\Api\Customer;
  3. use Harmonizely\Core\Exception\AccessDeniedException;
  4. use Harmonizely\Core\Exception\NotFoundException;
  5. use Harmonizely\Core\Security\Contract\ISuperAdminSecurityHelper;
  6. use Harmonizely\Model\UserInterface;
  7. use Harmonizely\Repository\Contract\IUserRepository;
  8. use Harmonizely\Service\SuperAdmin\Event\UserActionEvent;
  9. use Harmonizely\Service\SuperAdmin\Panel\AccessVoters\CustomerVoter;
  10. use Harmonizely\Service\SuperAdmin\Panel\Api\Customer\Contract\ICustomerDeleteService;
  11. use Harmonizely\Types\SuperAdmin\UserActionsActionType;
  12. use Harmonizely\Types\SuperAdmin\UserActionsObjectType;
  13. use Harmonizely\User\DeleteUser;
  14. use Harmonizely\User\DeleteUserRequest;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. class CustomerDeleteService implements ICustomerDeleteService
  17. {
  18.     /**
  19.      * @var ISuperAdminSecurityHelper
  20.      */
  21.     private ISuperAdminSecurityHelper $security;
  22.     /**
  23.      * @var EventDispatcherInterface
  24.      */
  25.     private EventDispatcherInterface $eventDispatcher;
  26.     /**
  27.      * @var IUserRepository
  28.      */
  29.     private IUserRepository $userRepository;
  30.     /**
  31.      * @var DeleteUser
  32.      */
  33.     private DeleteUser $deleteUser;
  34.     /**
  35.      * @param ISuperAdminSecurityHelper $security
  36.      * @param EventDispatcherInterface $eventDispatcher
  37.      * @param IUserRepository $userRepository
  38.      * @param DeleteUser $deleteUser
  39.      */
  40.     public function __construct(
  41.         ISuperAdminSecurityHelper $security,
  42.         EventDispatcherInterface $eventDispatcher,
  43.         IUserRepository $userRepository,
  44.         DeleteUser $deleteUser
  45.     )
  46.     {
  47.         $this->security $security;
  48.         $this->eventDispatcher $eventDispatcher;
  49.         $this->userRepository $userRepository;
  50.         $this->deleteUser $deleteUser;
  51.     }
  52.     /**
  53.      * @param int $id
  54.      * @return void
  55.      * @throws NotFoundException
  56.      * @throws AccessDeniedException
  57.      */
  58.     public function perform(int $id): void
  59.     {
  60.         /** @var UserInterface $user */
  61.         $user $this->userRepository->find($id);
  62.         if (!($user instanceof UserInterface)) {
  63.             throw new NotFoundException('User not found');
  64.         }
  65.         $this->security->denyUnlessGranted(CustomerVoter::DELETE$user);
  66.         $request = new DeleteUserRequest();
  67.         $request->setDelete(true);
  68.         $request->setReason('support_delete_user');
  69.         $this->deleteUser->perform($user$request);
  70.         $this->eventDispatcher->dispatch(new UserActionEvent(
  71.             UserActionsActionType::TYPE_DELETE,
  72.             UserActionsObjectType::TYPE_CUSTOMER,
  73.             $user->getId()
  74.         ), UserActionEvent::NAME);
  75.     }
  76. }