WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.076 --> 00:00:02.706 align:middle
Our app sends two emails:

00:00:03.086 --> 00:00:06.766 align:middle
in SendBookingRemindersCommand,
and TripController::show().

00:00:07.516 --> 00:00:07.956 align:middle
There is...

00:00:07.956 --> 00:00:09.396 align:middle
a lot of duplication here.

00:00:09.716 --> 00:00:10.806 align:middle
It hurts my eyes!

00:00:11.086 --> 00:00:11.856 align:middle
But no worries!

00:00:12.116 --> 00:00:15.406 align:middle
We can reorganize this into
an email factory service.

00:00:15.666 --> 00:00:19.166 align:middle
And because we have tests covering
both emails, we can refactor

00:00:19.166 --> 00:00:21.466 align:middle
and be confident that we
haven't broken anything.

00:00:21.736 --> 00:00:24.576 align:middle
I can't say it enough: I love tests!

00:00:24.576 --> 00:00:30.946 align:middle
Start by creating a new class:
BookingEmailFactory in the App\Email namespace.

00:00:31.356 --> 00:00:38.206 align:middle
Add a constructor, copy the $termsPath argument
from TripController::show(), paste it here,

00:00:39.786 --> 00:00:44.636 align:middle
and make it a private property:
Now, stub out two factory methods:

00:00:44.826 --> 00:00:47.436 align:middle
public function createBookingConfirmation(),

00:00:48.166 --> 00:00:52.116 align:middle
which will accept Booking
$booking, and return TemplatedEmail.

00:00:54.846 --> 00:00:59.906 align:middle
Then, public function
createBookingReminder(Booking $booking) also

00:00:59.906 --> 00:01:04.306 align:middle
returning a TemplatedEmail: Create a
method to house that darn duplication:

00:01:05.036 --> 00:01:11.066 align:middle
private function createEmail(), with
arguments Booking $booking and string $tag

00:01:11.286 --> 00:01:15.546 align:middle
that returns a TemplatedEmail:
Jump to TripController::show(),

00:01:18.046 --> 00:01:20.986 align:middle
copy all the email creation
code, and paste it here.

00:01:25.846 --> 00:01:31.336 align:middle
Up top, we need two variables:
$customer = $booking-&gt;getCustomer()

00:01:32.406 --> 00:01:35.386 align:middle
and $trip = $booking-&gt;getTrip().

00:01:37.166 --> 00:01:41.836 align:middle
Remove attachFromPath(),
subject(), and htmlTemplate().

00:01:42.836 --> 00:01:45.966 align:middle
In this TagHeader, use the passed $tag variable.

00:01:46.636 --> 00:01:48.116 align:middle
We can leave the metadata the same.

00:01:48.116 --> 00:01:55.296 align:middle
Finally, return the $email:
With our shared logic in place,

00:01:55.416 --> 00:01:57.526 align:middle
use it in createBookingConfirmation().

00:01:58.166 --> 00:02:04.506 align:middle
Write return $this-&gt;createEmail(), passing
the $booking variable and booking for the tag.

00:02:05.526 --> 00:02:09.466 align:middle
Now, -&gt;subject(), copy this
from TripController::show(),

00:02:15.096 --> 00:02:18.056 align:middle
changing the $trip variable
to $booking-&gt;getTrip().

00:02:19.786 --> 00:02:25.756 align:middle
Finally, -&gt;htmlTemplate('email
/booking_confirmation.html.twig'):

00:02:27.416 --> 00:02:32.466 align:middle
For createBookingReminder(), copy the insides
of createBookingConfirmation() and paste here.

00:02:33.236 --> 00:02:37.696 align:middle
Change the tag to booking_reminder,
the subject to Booking Reminder,

00:02:38.176 --> 00:02:38.946 align:middle
and the template

00:02:38.996 --> 00:02:43.506 align:middle
to email/booking_reminder.html.twig:
Now the fun part!

00:02:43.746 --> 00:02:46.906 align:middle
Using our factory and removing
a whole wack of code!

00:02:47.676 --> 00:02:52.026 align:middle
In TripController::show(),
instead of injecting $termsPath,

00:02:52.026 --> 00:02:58.516 align:middle
inject BookingEmailFactory $emailFactory:
Delete all the email creation code

00:03:00.636 --> 00:03:07.296 align:middle
and inside $mailer-&gt;send(), write $emailFactory
-&gt;createBookingConfirmation($booking):

00:03:08.526 --> 00:03:13.446 align:middle
Over in SendBookingRemindersCommand,
again, remove all the email creation code.

00:03:15.306 --> 00:03:22.136 align:middle
Up in the constructor, autowire private
BookingEmailFactory $emailFactory: Down here,

00:03:22.136 --> 00:03:24.986 align:middle
inside $this-&gt;mailer-&gt;send(),

00:03:25.366 --> 00:03:30.566 align:middle
write $this-&gt;emailFactory
-&gt;createBookingReminder($booking):

00:03:31.156 --> 00:03:32.706 align:middle
Oh yeah, that felt good!

00:03:33.426 --> 00:03:34.696 align:middle
But did we break anything?

00:03:35.026 --> 00:03:37.176 align:middle
We Canadians are known for being a bit wild.

00:03:37.676 --> 00:03:43.096 align:middle
Check by running the tests:
bin/phpunit Uh oh, a failure!

00:03:43.546 --> 00:03:44.856 align:middle
Good thing we have these tests, eh?

00:03:45.576 --> 00:03:49.346 align:middle
The failure comes from BookingTest:
Message does not include file

00:03:49.346 --> 00:03:52.636 align:middle
with filename [Terms of Service.pdf].

00:03:52.636 --> 00:03:58.056 align:middle
Easy fix! During our refactor, I forgot to
attach the thrilling terms of service PDF

00:03:58.056 --> 00:03:59.666 align:middle
to the booking confirmation email.

00:03:59.996 --> 00:04:01.676 align:middle
And our customers depend on that.

00:04:01.676 --> 00:04:06.596 align:middle
Find BookingEmailFactory::createBooking
Confirmation(),

00:04:07.296 --> 00:04:12.156 align:middle
and add -&gt;attachFromPath($this-&gt;termsPath,

00:04:12.436 --> 00:04:18.496 align:middle
'Terms of Service.pdf'):
Re-run the tests: Passing!

00:04:18.666 --> 00:04:20.096 align:middle
Successful refactor?

00:04:20.166 --> 00:04:25.236 align:middle
Check! Next, let's switch gears a bit
and dive into two new Symfony components

00:04:25.236 --> 00:04:28.296 align:middle
to consume the email webhook
events from Mailtrap.

