src/EventSubscriber/User/UserCreatedSubscriber.php line 39
<?php
declare(strict_types=1);
namespace App\EventSubscriber\User;
use App\Entity\Ledger\Account;
use App\Entity\User;
use App\Events\User\UserCreatedEvent;
use App\Services\Transaction\CreateService;
use App\Services\Transaction\Types\TypeCreatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserCreatedSubscriber implements EventSubscriberInterface
{
protected const GIFT_COMMENT = 'Gift for registration: #%s';
protected const COMMENT = 'Reward for registration: #%s';
protected const GIFT_AMOUNT = 5;
protected const AMOUNT = 10;
public function __construct(
private readonly CreateService $transactionService,
private readonly TypeCreatorInterface $rewardTypeCreator,
private readonly TypeCreatorInterface $transferTypeCreator,
private readonly Account $bankAccount,
) {
}
public static function getSubscribedEvents(): array
{
return [
UserCreatedEvent::NAME => 'sendGift',
];
}
/**
* @throws \Throwable
*/
public function sendGift(UserCreatedEvent $event): void
{
$user = $event->getUser();
$rewardType = $this->transferTypeCreator->createType(
$this->bankAccount,
$user->getGiftAccount(),
self::GIFT_AMOUNT,
$this->createComment(self::GIFT_COMMENT, $user),
);
$this->transactionService->perform($rewardType);
$transferType = $this->rewardTypeCreator->createType(
$this->bankAccount,
$user->getMainAccount(),
self::AMOUNT,
$this->createComment(self::COMMENT, $user),
);
$this->transactionService->perform($transferType);
}
private function createComment(string $tpl, User $user): string
{
return sprintf($tpl, $user->getId());
}
}