Answer the question
In order to leave comments, you need to log in
What am I doing wrong with Symfony Messenger?
So, I solve the problem of sending email messages. In a good way, this should be done asynchronously, which is what I'm trying to set up. But first, I want to test the functionality of the code, so I just write the text to a file.
I did everything according to the documentation: https://symfony.com/doc/current/messenger.html
I
tried both Redis and DB as a transport
Message class:
namespace App\Message;
/**
* Class EmailNotification
* @package App\Message
*/
final class EmailNotification
{
private $email;
private $flightId;
public function __construct(string $email, int $flightId)
{
$this->email = $email;
$this->flightId = $flightId;
}
public function getEmail(): string
{
return $this->email;
}
public function getFlightId(): int
{
return $this->flightId;
}
}
namespace App\MessageHandler;
use App\Message\EmailNotification;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
final class EmailNotificationHandler implements MessageHandlerInterface
{
/**
* @param EmailNotification $message
*/
public function __invoke(EmailNotification $message)
{
$email = $message->getEmail();
$flightId = $message->getFlightId();
$mes = "Flight $flightId was cancelled - $email";
$testFile = __DIR__ .'/../../var/log/test.txt';
file_put_contents($testFile, $mes);
}
}
$emails = $this->getEmails($flightId);
foreach ($emails as $email) {
$bus->dispatch(new EmailNotification($email, $flightId));
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question