// Контроллер
public function __construct(
private readonly MessageBusInterface $bus,
private LoggerInterface $logger,
private readonly SendTemplateMailer $sendTemplateMailer,
private readonly TemplateRepository $templateRepository
) {
}
#[Route('/test-rabbit/{id}', name: 'create_image', methods: ['GET'])]
public function testRabbit(Image $image): array
{
$this->logger->info('Test 1'); // тут сообщение логируется
$template = $this->templateRepository->findOneBy(['uuid' => $image->getUuid()]);
$this->sendTemplateMailer->sendTemplate($template); // тут сообщение приходит на почту
try {
$this->bus->dispatch(new SendOneMessage($image->getId()));
} catch (Exception $e) {
return ['status' => false, 'message' => $e->getMessage()];
}
return ['status' => true];
}
// Обработчики
#[AsMessageHandler]
class OneJobHandler implements MessageHandlerInterface
{
public function __construct(
private readonly ImageRepository $imageRepository,
private LoggerInterface $logger,
private readonly MessageBusInterface $bus
) {
}
public function __invoke(SendOneMessage $message)
{
$this->logger->info('Test 2'); // в обработчике уже не логируется тут и дальше
$image = $this->imageRepository->find($message->imageId);
$image
->setDescription('Test')
->setDateUpdate(new DateTime())
;
$this->imageRepository->save($image, true);
$this->bus->dispatch(new SendTwoMessage($image->getUuid()));
$this->logger->info('Test 3');
}
}
#[AsMessageHandler]
class TwoJobHandler implements MessageHandlerInterface
{
public function __construct(
private readonly TemplateRepository $templateRepository,
private LoggerInterface $logger,
private readonly SendTemplateMailer $sendTemplateMailer
) {
}
public function __invoke(SendTwoMessage $message)
{
$this->logger->info('Test 4');
$template = $this->templateRepository->findOneBy(['uuid' => $message->templateUuid]);
$this->sendTemplateMailer->sendTemplate($template); // тут уже сообщение не отправляется, хотя тот же шаблон
$this->logger->info('Test 5');
}
}
// Mailer
class SendTemplateMailer
{
public function __construct(
private readonly Environment $twig,
private readonly MailerInterface $mailer,
private readonly string $emailFrom,
private readonly LoggerInterface $logger
) {
}
public function sendTemplate(Template $template): void
{
$this->logger->info('Test 6'); // это сообщение появляется в логе, только когда метод вызывается из контроллера
$email = (new Email())
->from($this->emailFrom)
->to($this->emailFrom)
->subject('Тестовое сообщение')
->html(
$this
->twig
->load('/Email/test.html.twig')
->render(
[
'uuid' => $template->getUuid(),
'title' => $template->getTitle(),
'description' => $template->getDescription()
]
)
)
;
$this->mailer->send($email);
}
}
// Контроллер
public function __construct(private readonly MessageBusInterface $bus)
{
}
#[Route('/test-rabbit/{id}', name: 'create_image', methods: ['GET'])]
public function testRabbit(Image $image): array
{
try {
$this->bus->dispatch(new SendOneMessage($image->getId()));
} catch (Exception $e) {
return ['status' => false, 'message' => $e->getMessage()];
}
return ['status' => true];
}
// Обработчики
#[AsMessageHandler]
class OneJobHandler implements MessageHandlerInterface
{
public function __construct(
private readonly ImageRepository $imageRepository,
private readonly MessageBusInterface $bus
) {
}
public function __invoke(SendOneMessage $message)
{
$image = $this->imageRepository->find($message->imageId);
$image
->setDescription('Test')
->setDateUpdate(new DateTime())
;
$this->imageRepository->save($image, true);
$this->bus->dispatch(new SendTwoMessage($image->getUuid()));
}
}
#[AsMessageHandler]
class TwoJobHandler implements MessageHandlerInterface
{
public function __construct(
private readonly TemplateRepository $templateRepository,
private readonly SendTemplateMailer $sendTemplateMailer
) {
}
public function __invoke(SendTwoMessage $message)
{
$template = $this->templateRepository->findOneBy(['uuid' => $message->templateUuid]);
$this->sendTemplateMailer->sendTemplate($template);
}
}
// Messages
class SendOneMessage
{
public int $imageId;
public function __construct(int $imageId)
{
$this->imageId = $imageId;
}
}
class SendTwoMessage
{
public int $templateUuid;
public function __construct(int $templateUuid)
{
$this->templateUuid = $templateUuid;
}
}