Refactor ship status to be enum instead of string.

This commit is contained in:
Krzysztof Płaczek
2024-10-18 16:21:44 +02:00
parent 08d6ed936b
commit 231745d315
4 changed files with 37 additions and 22 deletions

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Enum;
enum StarshipStatusEnum: string
{
case WAITING = 'waiting';
case IN_PROGRESS = 'in_progress';
case COMPLETED = 'completed';
}

View File

@@ -2,6 +2,8 @@
namespace App\Model; namespace App\Model;
use App\Enum\StarshipStatusEnum;
class Starship class Starship
{ {
public function __construct( public function __construct(
@@ -9,7 +11,7 @@ class Starship
private string $name, private string $name,
private string $class, private string $class,
private string $captain, private string $captain,
private string $status, private StarshipStatusEnum $status,
) { ) {
} }
@@ -33,7 +35,7 @@ class Starship
return $this->name; return $this->name;
} }
public function getStatus(): string public function getStatus(): StarshipStatusEnum
{ {
return $this->status; return $this->status;
} }

View File

@@ -2,6 +2,7 @@
namespace App\Repository; namespace App\Repository;
use App\Enum\StarshipStatusEnum;
use App\Model\Starship; use App\Model\Starship;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@@ -16,18 +17,18 @@ class StarshipRepository
$this->logger->info('Starship Repository: Finding Starships'); $this->logger->info('Starship Repository: Finding Starships');
return [ return [
new Starship(1, 'Starship 01', 'Heavy Starship', 'Human Captain', 'damaged'), new Starship(1, 'Starship 01', 'Heavy Starship', 'Human Captain', StarshipStatusEnum::COMPLETED),
new Starship(2, 'Starship 02', 'Light Starship', 'Robot Captain', 'new'), new Starship(2, 'Starship 02', 'Light Starship', 'Robot Captain', StarshipStatusEnum::IN_PROGRESS),
new Starship(3, 'USS Voyager', 'Intrepid-class', 'Kathryn Janeway', 'Active'), new Starship(3, 'USS Voyager', 'Intrepid-class', 'Kathryn Janeway', StarshipStatusEnum::WAITING),
new Starship(4, 'Starblade', 'Interceptor-class', 'Lance Stellar', 'In repair'), new Starship(4, 'Starblade', 'Interceptor-class', 'Lance Stellar', StarshipStatusEnum::COMPLETED),
new Starship(5, 'Black Star', 'Battlecruiser-class', 'Zara Nox', 'Active'), new Starship(5, 'Black Star', 'Battlecruiser-class', 'Zara Nox', StarshipStatusEnum::WAITING),
new Starship(6, 'SS Horizon', 'Explorer-class', 'James Marshall', 'Decommissioned'), new Starship(6, 'SS Horizon', 'Explorer-class', 'James Marshall', StarshipStatusEnum::COMPLETED),
new Starship(7, 'Nebula Queen', 'Transport-class', 'Elara Trent', 'Under inspection'), new Starship(7, 'Nebula Queen', 'Transport-class', 'Elara Trent', StarshipStatusEnum::IN_PROGRESS),
new Starship(8, 'Celestial Wave', 'Frigate-class', 'Talon Skye', 'On mission'), new Starship(8, 'Celestial Wave', 'Frigate-class', 'Talon Skye', StarshipStatusEnum::IN_PROGRESS),
new Starship(9, 'Galactic Serpent', 'Destroyer-class', 'Viktor Helios', 'Active'), new Starship(9, 'Galactic Serpent', 'Destroyer-class', 'Viktor Helios', StarshipStatusEnum::COMPLETED),
new Starship(10, 'Phoenix Wing', 'Carrier-class', 'Aurora Drake', 'Awaiting orders'), new Starship(10, 'Phoenix Wing', 'Carrier-class', 'Aurora Drake', StarshipStatusEnum::COMPLETED),
new Starship(11, 'Silver Comet', 'Scout-class', 'Finn O\'Neil', 'In dock'), new Starship(11, 'Silver Comet', 'Scout-class', 'Finn O\'Neil', StarshipStatusEnum::WAITING),
new Starship(12, 'Crimson Spear', 'Corvette-class', 'Raven Callen', 'Destroyed'), new Starship(12, 'Crimson Spear', 'Corvette-class', 'Raven Callen', StarshipStatusEnum::IN_PROGRESS),
]; ];
} }

View File

@@ -12,36 +12,37 @@
</h1> </h1>
<div class="space-y-5"> <div class="space-y-5">
<!-- start ship item -->
{% for ship in ships %}
<div class="bg-[#16202A] rounded-2xl pl-5 py-5 pr-11 flex flex-col min-[1174px]:flex-row min-[1174px]:justify-between"> <div class="bg-[#16202A] rounded-2xl pl-5 py-5 pr-11 flex flex-col min-[1174px]:flex-row min-[1174px]:justify-between">
<div class="flex justify-center min-[1174px]:justify-start"> <div class="flex justify-center min-[1174px]:justify-start">
<img class="h-[83px] w-[84px]" src="/images/status-in-progress.png" alt="Status: in progress"> <img class="h-[83px] w-[84px]" src="/images/status-in-progress.png" alt="Status: {{ ship.status.value }}">
<div class="ml-5"> <div class="ml-5">
<div class="rounded-2xl py-1 px-3 flex justify-center w-32 items-center bg-amber-400/10"> <div class="rounded-2xl py-1 px-3 flex justify-center w-32 items-center bg-amber-400/10">
<div class="rounded-full h-2 w-2 bg-amber-400 blur-[1px] mr-2"></div> <div class="rounded-full h-2 w-2 bg-amber-400 blur-[1px] mr-2"></div>
<p class="uppercase text-xs text-nowrap">in progress</p> <p class="uppercase text-xs text-nowrap">{{ ship.status.value }}</p>
</div> </div>
<h4 class="text-[22px] pt-1 font-semibold"> <h4 class="text-[22px] pt-1 font-semibold">
<a <a
class="hover:text-slate-200" class="hover:text-slate-200"
href="#" href="{{ path('app_starship_show', {id: ship.id}) }}"
>USS LeafyCruiser</a> >{{ ship.name }}</a>
</h4> </h4>
</div> </div>
</div> </div>
<div class="flex justify-center min-[1174px]:justify-start mt-2 min-[1174px]:mt-0 shrink-0"> <div class="flex justify-center min-[1174px]:justify-start mt-2 min-[1174px]:mt-0 shrink-0">
<div class="border-r border-white/20 pr-8"> <div class="border-r border-white/20 pr-8">
<p class="text-slate-400 text-xs">Captain</p> <p class="text-slate-400 text-xs">Captain</p>
<p class="text-xl">Jean-Luc Pickles</p> <p class="text-xl">{{ ship.captain }}</p>
</div> </div>
<div class="pl-8 w-[100px]"> <div class="pl-8 w-[100px]">
<p class="text-slate-400 text-xs">Class</p> <p class="text-slate-400 text-xs">Class</p>
<p class="text-xl">Garden</p> <p class="text-xl">{{ ship.class }}</p>
</div> </div>
</div> </div>
</div> </div>
<!-- end ship item --> {% endfor %}
</div> </div>
<p class="text-lg mt-5 text-center md:text-left"> <p class="text-lg mt-5 text-center md:text-left">