WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.096 --> 00:00:02.036 align:middle
New feature time!

00:00:02.386 --> 00:00:06.306 align:middle
I want to send a reminder email to
customers 1 week before their booked trip.

00:00:06.656 --> 00:00:08.746 align:middle
T minus 1 week to lift off people!

00:00:08.746 --> 00:00:12.676 align:middle
First though, we have a little
problem with our Symfony CLI worker.

00:00:13.186 --> 00:00:15.506 align:middle
Open .symfony.local.yaml.

00:00:16.026 --> 00:00:19.436 align:middle
Our messenger worker is watching
the vendor directory for changes.

00:00:19.886 --> 00:00:22.956 align:middle
At least on some systems, there's
just too many files in here

00:00:22.956 --> 00:00:24.876 align:middle
to monitor and some weird things happen.

00:00:25.256 --> 00:00:30.286 align:middle
No big deal: remove vendor/: And since we
changed the config, jump to your terminal

00:00:30.286 --> 00:00:34.466 align:middle
and restart the webserver:
symfony server:stop And:

00:00:34.466 --> 00:00:40.146 align:middle
symfony serve -d Our new booking reminder
email will have a template very similar

00:00:40.146 --> 00:00:41.666 align:middle
to the booking confirmation one.

00:00:42.056 --> 00:00:47.666 align:middle
To reduce duplication, and keep our snazzy
emails consistent, in templates/email/,

00:00:47.926 --> 00:00:53.186 align:middle
create a new layout.html.twig template
that all our emails will extend.

00:00:53.836 --> 00:00:58.376 align:middle
Copy the contents of
booking_confirmation.html.twig and paste here.

00:00:59.196 --> 00:01:04.806 align:middle
Now, remove the booking-confirmation-specific
content and create an empty content block.

00:01:05.946 --> 00:01:07.766 align:middle
I think it's fine to keep our signature here.

00:01:08.656 --> 00:01:12.266 align:middle
In booking_confirmation.html.twig, up top here,

00:01:12.266 --> 00:01:17.186 align:middle
extend this new layout and
add the content block.

00:01:20.856 --> 00:01:25.376 align:middle
Down below, copy the email-specific
content and paste it inside that block.

00:01:26.656 --> 00:01:27.816 align:middle
Remove everything else.

00:01:29.786 --> 00:01:34.256 align:middle
Let's make sure the booking confirmation email
still works - and we have tests for that!

00:01:34.716 --> 00:01:39.196 align:middle
Back in the terminal, run
them with: bin/phpunit Green!

00:01:39.326 --> 00:01:40.206 align:middle
That's a good sign.

00:01:40.566 --> 00:01:42.846 align:middle
Let's be doubly sure by checking it in Mailtrap.

00:01:43.456 --> 00:01:44.826 align:middle
In the app, book a trip...

00:01:48.596 --> 00:01:49.556 align:middle
and check Mailtrap.

00:01:51.666 --> 00:01:53.106 align:middle
I still looks fantastic!

00:01:53.616 --> 00:01:55.306 align:middle
Time to bang out the reminder email!

00:01:55.306 --> 00:01:59.256 align:middle
After an email reminder is sent,
we need to mark the booking

00:01:59.256 --> 00:02:02.246 align:middle
so that we don't annoy the
customer with multiple reminders.

00:02:02.696 --> 00:02:05.146 align:middle
Let's add a new flag for
this to the Booking entity.

00:02:05.806 --> 00:02:07.206 align:middle
In your terminal, run: Oops!

00:02:07.206 --> 00:02:16.276 align:middle
symfony console make:entity Booking
Add a new field called reminderSentAt,

00:02:16.776 --> 00:02:19.396 align:middle
type datetime_immutable, nullable?

00:02:19.586 --> 00:02:24.766 align:middle
Yes. This is a common pattern I use for these
type of flag fields instead of a simple boolean.

00:02:25.306 --> 00:02:27.536 align:middle
null means false and a date means true.

00:02:28.046 --> 00:02:30.396 align:middle
It works the same, but gives us a bit more info.

00:02:31.026 --> 00:02:32.546 align:middle
Hit enter to exit the command.

00:02:33.126 --> 00:02:34.226 align:middle
In the Booking entity...

00:02:34.506 --> 00:02:37.876 align:middle
here's our new property, and
down here, the getter and setter.

00:02:38.686 --> 00:02:42.176 align:middle
Next, we need a way to find all
bookings that need a reminder sent.

00:02:42.576 --> 00:02:44.586 align:middle
Perfect job for BookingRepository!

00:02:46.516 --> 00:02:51.066 align:middle
Add a new method called findBookingsToRemind(),
return type: array.

00:02:52.036 --> 00:02:56.616 align:middle
Add a docblock to show it returns
an array of Booking objects: Inside,

00:02:56.976 --> 00:03:06.266 align:middle
return $this-&gt;createQueryBuilder(), alias b.
Chain -&gt;andWhere('b.reminderSentAt IS NULL'),

00:03:07.236 --> 00:03:18.086 align:middle
-&gt;andWhere('b.date &lt;= :future'),
-&gt;andWhere('b.date &gt; :now') filling

00:03:18.086 --> 00:03:21.566 align:middle
in the placeholders with
-&gt;setParameter('future',

00:03:22.246 --> 00:03:24.726 align:middle
new \DateTimeImmutable('+7 days'))

00:03:25.206 --> 00:03:30.346 align:middle
and -&gt;setParameter('now', new
\DateTimeImmutable('now')).

00:03:30.346 --> 00:03:37.006 align:middle
Finish with -&gt;getQuery()-&gt;getResult():
In AppFixtures,

00:03:37.876 --> 00:03:40.246 align:middle
down here, we create some fake bookings.

00:03:40.786 --> 00:03:47.106 align:middle
Add one that will for sure trigger a reminder
email to be sent: BookingFactory::createOne(),

00:03:47.586 --> 00:03:55.416 align:middle
inside, 'trip' =&gt; $arrakis, 'customer' =&gt;
$clark and, this is the important part,

00:03:55.816 --> 00:04:02.336 align:middle
'date' =&gt; new \DateTimeImmutable('+6 days'):
Clearly between now and 7 days from now.

00:04:02.996 --> 00:04:05.206 align:middle
We made changes to the structure
of our database.

00:04:05.556 --> 00:04:07.466 align:middle
Normally, we should be creating a migration...

00:04:07.686 --> 00:04:09.376 align:middle
but, we aren't using migrations.

00:04:09.376 --> 00:04:11.706 align:middle
So, we'll just force update the schema.

00:04:12.266 --> 00:04:17.266 align:middle
In your terminal, run: symfony
console doctrine:schema:update --

00:04:17.376 --> 00:04:26.246 align:middle
force Then, reload the fixtures: symfony console
doctrine:fixture:load That all worked, great!

00:04:26.246 --> 00:04:31.166 align:middle
Next, we'll create a new reminder
email and a CLI command to send them!

