Albertobeiz

Albertobeiz

Symfony Tips #16 - Send emails asynchronously

Symfony Tips #16 - Send emails asynchronously

Subscribe to my newsletter and never miss my upcoming articles

🖥 Symfony Tips: Quick and practical tricks to develop solid backend systems.

We'll use our new Domain Events to remove the EmailService from our use case.

This is OK

    public function __invoke(Request $request): User
    {
        .
        .
        .
        $this->emailService->onUserCreated($user);
        $this->analyticsService->onUserCreated();

        return $user;
    }

This is better

    public function __invoke(Request $request): User
    {
        .
        .
        .
        $this->analyticsService->onUserCreated();

        return $user;
    }
class OnUserCreated_SendWelcomeEmail implements MessageSubscriberInterface
{
    public function __construct(
        private EmailService $emailService
    )
    {
    }

    public function __invoke(UserCreated $domainEvent)
    {
        $this->emailService->send($domainEvent->email, 'Bienvenido a Twitfony');
    }

    public static function getHandledMessages(): iterable
    {
        yield UserCreated::class => [
            'from_transport' => 'async',
        ];
    }
}

That's it, magic 🧙‍♀️ Now each time a UserCreated event is dispatched this Subscriber will be called. And you can make it sync or async just by changing the from_transport config.

Let's run test.sh to see it in action.

[Test.sh] Sending new user requests

[Event Bus - PrintSync] App\Modules\User\Domain\UserCreated
[Analytics Service] Added User
{
    "uuid": {
        "uid": "f5e2b737-81dc-4fd2-a08a-984c0993937b"
    },
    "username": "aa",
    "email": "a@a.a"
}

[Error] Email Already Exists

[Error] Username is too short


[Test.sh] Waiting before processing Async events...

[Email Service] Send Bienvenido a Twitfony to a@a.a
[Event Bus - PrintAsync] App\Modules\User\Domain\UserCreated

And don't forget to remove all email related code from the unit test.

Symfony tip completed 👍! Check the final code and leave a ⭐️!

Next Tip -> Symfony Tips #17 - Store your Domain Events

Previous Tip -> Symfony Tips #15 - Dispatch Domain Events

HEY! Follow me at @albertobeiz if you found this tip useful or have any question.

Interested in reading more such articles from Alberto Beiz?

Support the author by donating an amount of your choice.

 
Share this