src/EventSubscriber/User/UserArchivedListener.php line 27

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber\User;
  4. use App\Entity\User;
  5. use App\Events\User\UserArchivedEvent;
  6. use App\Services\Transaction;
  7. use App\Services\Transaction\Exceptions\CheckerException;
  8. use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
  9. #[AsEventListener(eventUserArchivedEvent::NAMEmethod'resetAccounts')]
  10. class UserArchivedListener
  11. {
  12.     protected const COMMENT 'User archived: #%s';
  13.     public function __construct(
  14.         private readonly Transaction\ResetService $resetService,
  15.     ) {
  16.     }
  17.     /**
  18.      * @throws \Throwable
  19.      * @throws CheckerException
  20.      */
  21.     public function resetAccounts(UserArchivedEvent $event): void
  22.     {
  23.         $user $event->getUser();
  24.         $comment $this->createComment($user);
  25.         $this->resetService->performAccount($user->getMainAccount(), $comment);
  26.         $this->resetService->performAccount($user->getGiftAccount(), $comment);
  27.     }
  28.     private function createComment(User $user): string
  29.     {
  30.         return sprintf(self::COMMENT, (string) $user->getId());
  31.     }
  32. }