WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.086 --> 00:00:04.076 align:middle
Inside the loop, making things
dynamic is nothing new...

00:00:04.186 --> 00:00:05.386 align:middle
which is great!

00:00:05.766 --> 00:00:09.236 align:middle
For in progress, say {{ ship.status }}.

00:00:10.496 --> 00:00:12.696 align:middle
When we refresh, it prints!

00:00:12.986 --> 00:00:14.196 align:middle
Though, yikes!

00:00:14.326 --> 00:00:17.116 align:middle
The statuses are running way out of their space.

00:00:17.416 --> 00:00:19.226 align:middle
Our data doesn't match the design!

00:00:19.826 --> 00:00:20.586 align:middle
Plot twist!

00:00:20.866 --> 00:00:23.356 align:middle
Someone changed the project's requirements...

00:00:23.526 --> 00:00:24.356 align:middle
right in the middle!

00:00:24.736 --> 00:00:26.116 align:middle
That "never" happens!

00:00:26.606 --> 00:00:31.996 align:middle
The new plan is this: each ship should have a
status of in progress, waiting, or completed.

00:00:32.566 --> 00:00:37.466 align:middle
Over in src/Repository/StarshipRepository.php,
our ships do have a status -

00:00:37.566 --> 00:00:41.266 align:middle
it's this argument - but it's a
string that can be set to anything.

00:00:41.756 --> 00:00:44.296 align:middle
So we need to do some refactoring
to fit the new plan.

00:00:44.776 --> 00:00:48.096 align:middle
Let's think: there are exactly
three valid statuses.

00:00:48.486 --> 00:00:50.736 align:middle
This a perfect use case for a PHP enum.

00:00:50.736 --> 00:00:57.216 align:middle
If you're not familiar with enums, they're
lovely and a great way to organize a set

00:00:57.216 --> 00:01:02.196 align:middle
of statuses - like published,
unpublished &amp; draft - or sizes - small,

00:01:02.196 --> 00:01:04.336 align:middle
medium or large - or anything similar.

00:01:05.186 --> 00:01:07.956 align:middle
In the Model/ directory - though
this could live anywhere...

00:01:08.236 --> 00:01:11.026 align:middle
we're creating the enum for
our own organization -

00:01:11.396 --> 00:01:14.286 align:middle
create a new class and call
it StarshipStatusEnum.

00:01:14.836 --> 00:01:20.446 align:middle
As soon as I typed the word enum, PhpStorm
changed the template from class to an enum.

00:01:21.136 --> 00:01:27.906 align:middle
So we're not creating a class, as you can see,
we created an enum Add a : string to the enum

00:01:27.996 --> 00:01:30.486 align:middle
to make what's called a "string-backed enum".

00:01:30.966 --> 00:01:36.386 align:middle
We won't go too deep, but this allows us to
define each status - like WAITING and assign

00:01:36.386 --> 00:01:38.626 align:middle
that to a string, which will
be handy in a minute.

00:01:39.396 --> 00:01:44.186 align:middle
Add a status for IN_PROGRESS
and finally one for COMPLETED.

00:01:46.816 --> 00:01:47.246 align:middle
That's it!

00:01:47.396 --> 00:01:52.386 align:middle
That's all an enum is: a set of "states"
that get centralized in one place.

00:01:53.146 --> 00:01:55.076 align:middle
Next: open up the Starship class.

00:01:55.556 --> 00:01:58.096 align:middle
The last argument is currently a string status.

00:01:58.396 --> 00:02:01.126 align:middle
Change it to be a StarshipStatusEnum.

00:02:01.666 --> 00:02:05.866 align:middle
And at the bottom, the getStatus method
will now return a StarshipStatusEnum.

00:02:06.526 --> 00:02:11.896 align:middle
Finally, in StarshipRepository where we
create each Starship, my editor is angry.

00:02:12.166 --> 00:02:13.456 align:middle
It says: Hey!

00:02:13.456 --> 00:02:17.506 align:middle
This argument accepts a StarshipStatusEnum,
but you're passing a string!

00:02:18.086 --> 00:02:19.666 align:middle
Let's calm it down.

00:02:20.066 --> 00:02:22.976 align:middle
Change this to StarshipStatusEnum::...

00:02:22.976 --> 00:02:25.616 align:middle
and it autocomplete the choices!

00:02:26.126 --> 00:02:28.236 align:middle
Let's make the first one IN_PROGRESS.

00:02:28.736 --> 00:02:32.286 align:middle
And that did add the use statement
for the enum to the top of the class.

00:02:33.026 --> 00:02:34.626 align:middle
For the next one, make it COMPLETED...

00:02:35.886 --> 00:02:37.416 align:middle
and for the last, WAITING.

00:02:39.216 --> 00:02:40.736 align:middle
Refactoring done!

00:02:41.096 --> 00:02:42.126 align:middle
Well... maybe.

00:02:42.516 --> 00:02:44.856 align:middle
When we refresh, busted!

00:02:45.186 --> 00:02:49.826 align:middle
It says: object of class StarshipStatusEnum
could not be converted to string

00:02:50.416 --> 00:02:53.156 align:middle
And it's coming from the ship.status Twig call.

00:02:53.556 --> 00:02:57.196 align:middle
That makes sense: ship.status is now an enum...

00:02:57.386 --> 00:02:59.996 align:middle
which can't be directly printed as a string.

00:03:00.626 --> 00:03:05.446 align:middle
The easiest fix, in homepage.html.twig,
is to add .value.

00:03:06.176 --> 00:03:11.366 align:middle
Because we made our enum string-backed, it
has a value property, which will be the string

00:03:11.366 --> 00:03:13.476 align:middle
that we assigned to the current status.

00:03:14.486 --> 00:03:15.126 align:middle
Try it now.

00:03:15.816 --> 00:03:17.136 align:middle
It looks great!

00:03:17.766 --> 00:03:19.796 align:middle
In progress, completed, waiting.

00:03:20.546 --> 00:03:24.256 align:middle
Next: let's learn how we can make
this last change a bit more elegant

00:03:24.256 --> 00:03:27.456 align:middle
by creating smarter methods
on our Starship class.

00:03:27.906 --> 00:03:29.966 align:middle
Then we'll put the finishing
touches on our design.

