src/EventSubscriber/User/UserCreatedSubscriber.php line 39

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber\User;
  4. use App\Entity\Ledger\Account;
  5. use App\Entity\User;
  6. use App\Events\User\UserCreatedEvent;
  7. use App\Services\Transaction\CreateService;
  8. use App\Services\Transaction\Types\TypeCreatorInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class UserCreatedSubscriber implements EventSubscriberInterface
  11. {
  12.     protected const GIFT_COMMENT 'Gift for registration: #%s';
  13.     protected const COMMENT 'Reward for registration: #%s';
  14.     protected const GIFT_AMOUNT 5;
  15.     protected const AMOUNT 10;
  16.     public function __construct(
  17.         private readonly CreateService $transactionService,
  18.         private readonly TypeCreatorInterface $rewardTypeCreator,
  19.         private readonly TypeCreatorInterface $transferTypeCreator,
  20.         private readonly Account $bankAccount,
  21.     ) {
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             UserCreatedEvent::NAME => 'sendGift',
  27.         ];
  28.     }
  29.     /**
  30.      * @throws \Throwable
  31.      */
  32.     public function sendGift(UserCreatedEvent $event): void
  33.     {
  34.         $user $event->getUser();
  35.         $rewardType $this->transferTypeCreator->createType(
  36.             $this->bankAccount,
  37.             $user->getGiftAccount(),
  38.             self::GIFT_AMOUNT,
  39.             $this->createComment(self::GIFT_COMMENT$user),
  40.         );
  41.         $this->transactionService->perform($rewardType);
  42.         $transferType $this->rewardTypeCreator->createType(
  43.             $this->bankAccount,
  44.             $user->getMainAccount(),
  45.             self::AMOUNT,
  46.             $this->createComment(self::COMMENT$user),
  47.         );
  48.         $this->transactionService->perform($transferType);
  49.     }
  50.     private function createComment(string $tplUser $user): string
  51.     {
  52.         return sprintf($tpl$user->getId());
  53.     }
  54. }