Skip to content

Commit

Permalink
Merge pull request #780 from daveroverts/native-enums
Browse files Browse the repository at this point in the history
feat: convert enums to native enums
  • Loading branch information
daveroverts authored Feb 6, 2024
2 parents 7ba1ab5 + 0b322d5 commit 2b8684c
Show file tree
Hide file tree
Showing 39 changed files with 225 additions and 403 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ _ide_helper*\.php
\.phpstorm\.meta\.php

.phpunit.result.cache
.phpunit.cache/test-results
15 changes: 4 additions & 11 deletions app/Enums/AirportView.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@

namespace App\Enums;

use BenSampo\Enum\Enum;

/**
* @method static static NAME()
* @method static static ICAO()
* @method static static IATA()
*/
final class AirportView extends Enum
enum AirportView: int
{
public const NAME = 0;
public const ICAO = 1;
public const IATA = 2;
case NAME = 0;
case ICAO = 1;
case IATA = 2;
}
15 changes: 4 additions & 11 deletions app/Enums/BookingStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@

namespace App\Enums;

use BenSampo\Enum\Enum;

/**
* @method static static UNASSIGNED()
* @method static static RESERVED()
* @method static static BOOKED()
*/
final class BookingStatus extends Enum
enum BookingStatus: int
{
public const UNASSIGNED = 0;
public const RESERVED = 1;
public const BOOKED = 2;
case UNASSIGNED = 0;
case RESERVED = 1;
case BOOKED = 2;
}
21 changes: 6 additions & 15 deletions app/Enums/EventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@

namespace App\Enums;

use BenSampo\Enum\Enum;

/**
* @method static static ONEWAY()
* @method static static CITYPAIR()
* @method static static FLYIN()
* @method static static GROUPFLIGHT()
* @method static static MULTIFLIGHTS()
*/
final class EventType extends Enum
enum EventType: int
{
public const ONEWAY = 1;
public const CITYPAIR = 2;
public const FLYIN = 3;
public const GROUPFLIGHT = 4;
public const MULTIFLIGHTS = 5;
case ONEWAY = 1;
case CITYPAIR = 2;
case FLYIN = 3;
case GROUPFLIGHT = 4;
case MULTIFLIGHTS = 5;
}
2 changes: 1 addition & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Handler extends ExceptionHandler
*
* @return void
*/
public function register()
public function register(): void
{
$this->reportable(function (Throwable $e) {
//
Expand Down
6 changes: 3 additions & 3 deletions app/Exports/BookingsExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public function __construct(public Event $event, public ?bool $vacc)
*/
public function collection()
{
return $this->event->bookings()->with('flights')->whereStatus(BookingStatus::BOOKED)->get();
return $this->event->bookings()->with('flights')->whereStatus(BookingStatus::BOOKED->value)->get();
}

public function map($booking): array
{
if ($this->event->event_type_id == EventType::MULTIFLIGHTS) {
if ($this->event->event_type_id == EventType::MULTIFLIGHTS->value) {
$flight1 = $booking->flights()->first();
$flight2 = $booking->flights()->whereKeyNot($flight1->id)->first();
if ($this->vacc) {
Expand Down Expand Up @@ -74,7 +74,7 @@ public function map($booking): array

public function columnFormats(): array
{
if ($this->event->event_type_id == EventType::MULTIFLIGHTS && !$this->vacc) {
if ($this->event->event_type_id == EventType::MULTIFLIGHTS->value && !$this->vacc) {
return [
'E' => NumberFormat::FORMAT_DATE_TIME4,
'G' => NumberFormat::FORMAT_DATE_TIME4,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected function verifyLogin(Request $request)
$booking = Booking::whereUuid(session('booking'))->first();
session()->forget('booking');
if (!empty($booking)) {
if ($booking->status !== BookingStatus::BOOKED) {
if ($booking->status !== BookingStatus::BOOKED->value) {
return to_route('bookings.edit', $booking);
}
return to_route('bookings.show', $booking);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Booking/BookingAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function adminAutoAssign(AutoAssign $request, Event $event): RedirectResp
}]);

if (!$request->checkAssignAllFlights) {
$bookings = $bookings->where('status', BookingStatus::BOOKED);
$bookings = $bookings->where('status', BookingStatus::BOOKED->value);
}
$bookings = $bookings->get();
$count = 0;
Expand Down
26 changes: 13 additions & 13 deletions app/Http/Controllers/Booking/BookingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function index(Request $request, Event $event): View|RedirectResponse

public function show(Booking $booking): View
{
if ($booking->event->event_type_id == EventType::MULTIFLIGHTS) {
if ($booking->event->event_type_id == EventType::MULTIFLIGHTS->value) {
return view('booking.show_multiflights', compact('booking'));
}
$flight = $booking->flights->first();
Expand All @@ -35,21 +35,21 @@ public function show(Booking $booking): View
public function edit(Booking $booking): View|RedirectResponse
{
// Check if the booking has already been booked or reserved
if ($booking->status !== BookingStatus::UNASSIGNED) {
if ($booking->status !== BookingStatus::UNASSIGNED->value) {
// Check if current user has booked/reserved
if ($booking->user_id == auth()->id()) {
if ($booking->status == BookingStatus::BOOKED && !$booking->is_editable) {
if ($booking->status == BookingStatus::BOOKED->value && !$booking->is_editable) {
flashMessage('info', __('Danger'), __('You cannot edit the booking!'));
return to_route('bookings.event.index', $booking->event);
}
if ($booking->event->event_type_id == EventType::MULTIFLIGHTS) {
if ($booking->event->event_type_id == EventType::MULTIFLIGHTS->value) {
return view('booking.edit_multiflights', compact('booking'));
}
$flight = $booking->flights->first();
return view('booking.edit', compact('booking', 'flight'));
} else {
// Check if the booking has already been reserved
if ($booking->status === BookingStatus::RESERVED) {
if ($booking->status === BookingStatus::RESERVED->value) {
flashMessage(
'danger',
__('Warning'),
Expand All @@ -74,15 +74,15 @@ public function edit(Booking $booking): View|RedirectResponse
'event_id',
$booking->event_id
)
->where('status', BookingStatus::BOOKED)
->where('status', BookingStatus::BOOKED->value)
->first()
) {
flashMessage('danger!', __('Warning'), __('You already have a booking!'));
return to_route('bookings.event.index', $booking->event);
}
// If user already has another reservation open
if (auth()->user()->bookings->where('event_id', $booking->event_id)
->where('status', BookingStatus::RESERVED)
->where('status', BookingStatus::RESERVED->value)
->first()
) {
flashMessage('danger', __('Warning'), __('You already have a reservation! Please cancel or book that flight first.'));
Expand All @@ -96,14 +96,14 @@ public function edit(Booking $booking): View|RedirectResponse
->by(auth()->user())
->on($booking)
->log('Flight reserved');
$booking->status = BookingStatus::RESERVED;
$booking->status = BookingStatus::RESERVED->value;
$booking->user()->associate(auth()->user())->save();
flashMessage(
'info',
__('Slot reserved'),
__('Slot remains reserved until :time', ['time' => $booking->updated_at->addMinutes(10)->format('Hi') . 'z'])
);
if ($booking->event->event_type_id == EventType::MULTIFLIGHTS) {
if ($booking->event->event_type_id == EventType::MULTIFLIGHTS->value) {
return view('booking.edit_multiflights', compact('booking'));
}
$flight = $booking->flights->first();
Expand Down Expand Up @@ -146,8 +146,8 @@ public function update(UpdateBooking $request, Booking $booking): RedirectRespon
);
}

$booking->status = BookingStatus::BOOKED;
if ($booking->getOriginal('status') === BookingStatus::RESERVED) {
$booking->status = BookingStatus::BOOKED->value;
if ($booking->getOriginal('status') === BookingStatus::RESERVED->value) {
$booking->save();
event(new BookingConfirmed($booking));
if (!Str::contains(config('mail.default'), ['log', 'array'])) {
Expand Down Expand Up @@ -219,7 +219,7 @@ public function cancel(Booking $booking): RedirectResponse
]);
}

if ($booking->status === BookingStatus::BOOKED) {
if ($booking->status === BookingStatus::BOOKED->value) {
event(new BookingCancelled($booking, auth()->user()));
$title = __('Booking cancelled!');
$message = __('Booking has been cancelled!');
Expand All @@ -228,7 +228,7 @@ public function cancel(Booking $booking): RedirectResponse
$message = __('Slot is now free to use again');
}

$booking->status = BookingStatus::UNASSIGNED;
$booking->status = BookingStatus::UNASSIGNED->value;
flashMessage('info', $title, $message);
$booking->user()->dissociate()->save();
return to_route('bookings.event.index', $booking->event);
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Event/EventAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function sendEmail(SendEmail $request, Event $event): JsonResponse|Redire
/* @var User $users */
$users = User::whereHas('bookings', function (Builder $query) use ($event) {
$query->where('event_id', $event->id);
$query->where('status', BookingStatus::BOOKED);
$query->where('status', BookingStatus::BOOKED->value);
})->get();
event(new EventBulkEmail($event, $request->all(), $users));
flashMessage('success', __('Done'), __('Bulk E-mail has been sent to :count people!', ['count' => $users->count()]));
Expand All @@ -155,7 +155,7 @@ public function sendFinalInformationMail(Request $request, Event $event): Redire
{
$bookings = $event->bookings()
->with(['user', 'flights'])
->where('status', BookingStatus::BOOKED)
->where('status', BookingStatus::BOOKED->value)
->get();

if ($request->testmode) {
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Livewire/Bookings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class Bookings extends Component
public int $total = 0;
public int $booked = 0;

public function filter($filter)
public function filter($filter): void
{
$this->filter = strtolower($filter);
}

public function mount()
public function mount(): void
{
// Only enable polling if event is 'active'
if (now()->between($this->event->startBooking, $this->event->endEvent)) {
Expand Down Expand Up @@ -61,7 +61,7 @@ public function render()
abort_unless(auth()->check() && auth()->user()->isAdmin, 404);
}

$this->booked = $this->bookings->where('status', BookingStatus::BOOKED)->count();
$this->booked = $this->bookings->where('status', BookingStatus::BOOKED->value)->count();

$this->total = $this->bookings->count();

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/EventResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EventResource extends JsonResource
public function toArray($request)
{
$total = $this->bookings->count();
$booked = $total - $this->bookings->where('status', BookingStatus::BOOKED)->count();
$booked = $total - $this->bookings->where('status', BookingStatus::BOOKED->value)->count();
return [
'id' => $this->id,
'event_type' => $this->type->name,
Expand Down
6 changes: 3 additions & 3 deletions app/Imports/BookingsImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(public Event $event)
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
public function model(array $row): void
{
$editable = true;
if (!empty($row['call_sign']) && !empty($row['aircraft_type'])) {
Expand All @@ -41,7 +41,7 @@ public function model(array $row)
'acType' => $row['aircraft_type'] ?? null,
]);

if ($this->event->event_type_id == EventType::MULTIFLIGHTS) {
if ($this->event->event_type_id == EventType::MULTIFLIGHTS->value) {
$airport1 = $this->getAirport($row['airport_1']);
$airport2 = $this->getAirport($row['airport_2']);
$airport3 = $this->getAirport($row['airport_3']);
Expand Down Expand Up @@ -89,7 +89,7 @@ public function chunkSize(): int

public function rules(): array
{
if ($this->event->event_type_id == EventType::MULTIFLIGHTS) {
if ($this->event->event_type_id == EventType::MULTIFLIGHTS->value) {
return [
'airport_1' => 'exists:airports,icao',
'airport_2' => 'exists:airports,icao',
Expand Down
2 changes: 1 addition & 1 deletion app/Imports/FlightRouteAssign.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FlightRouteAssign implements ToCollection, WithHeadingRow, WithValidation
{
use Importable;

public function collection(Collection $rows)
public function collection(Collection $rows): void
{
foreach ($rows as $row) {
$from = $this->getAirport($row['from']);
Expand Down
6 changes: 3 additions & 3 deletions app/Jobs/EventCleanupReservationsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public function __construct(public Event $event)
*
* @return void
*/
public function handle()
public function handle(): void
{
$this->event->bookings()
->whereStatus(BookingStatus::RESERVED)
->whereStatus(BookingStatus::RESERVED->value)
->each(function (Booking $booking) {
if (now()->greaterThanOrEqualTo($booking->updated_at->addMinutes(10))) {
$booking->status = BookingStatus::UNASSIGNED;
$booking->status = BookingStatus::UNASSIGNED->value;
$booking->user_id = null;
$booking->save();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/ImportAirportsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct()
*
* @return void
*/
public function handle()
public function handle(): void
{
$file = 'import_' . time() . '.csv';
Storage::disk('local')->put(
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/SendBookingCancelledNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct()
*
* @return void
*/
public function handle(BookingCancelled $event)
public function handle(BookingCancelled $event): void
{
activity()
->by($event->user)
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/SendBookingChangedNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct()
* @param BookingChanged $event
* @return void
*/
public function handle(BookingChanged $event)
public function handle(BookingChanged $event): void
{
$event->booking->user->notify(new \App\Notifications\BookingChanged($event->booking, $event->changes));
}
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/SendBookingConfirmedNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct()
* @param BookingConfirmed $event
* @return void
*/
public function handle(BookingConfirmed $event)
public function handle(BookingConfirmed $event): void
{
activity()
->by(auth()->user())
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/SendBookingDeletedNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct()
* @param BookingDeleted $event
* @return void
*/
public function handle(BookingDeleted $event)
public function handle(BookingDeleted $event): void
{
$event->user->notify(new \App\Notifications\BookingDeleted($event->event));
}
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/SendEventBulkEmailNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct()
* @param EventBulkEmail $event
* @return void
*/
public function handle(EventBulkEmail $event)
public function handle(EventBulkEmail $event): void
{
if (isset($event->request['testmode'])) {
$log = 'Bulk E-mail test performed';
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/SendEventFinalInformationNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct()
* @param EventFinalInformation $event
* @return void
*/
public function handle(EventFinalInformation $event)
public function handle(EventFinalInformation $event): void
{
if ($event->testUser) {
activity()
Expand Down
Loading

0 comments on commit 2b8684c

Please sign in to comment.