From 9c4aced837ee96f17d337ba4b08fec5b83c06da2 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Mon, 29 Mar 2021 14:53:35 -0400 Subject: [PATCH] Move the prefile event and add a new error exception (#1111) * Move the prefile event and add a new error exception * Remove extra import * Add empty handler for testing * Fix styling * Fix styleci --- app/Exceptions/PrefileError.php | 44 ++++++++++++++++++++ app/Http/Controllers/Api/PirepController.php | 3 -- app/Listeners/PirepEventsHandler.php | 29 +++++++++++++ app/Providers/EventServiceProvider.php | 2 + app/Services/PirepService.php | 3 ++ 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 app/Exceptions/PrefileError.php create mode 100644 app/Listeners/PirepEventsHandler.php diff --git a/app/Exceptions/PrefileError.php b/app/Exceptions/PrefileError.php new file mode 100644 index 00000000..d7b8ab80 --- /dev/null +++ b/app/Exceptions/PrefileError.php @@ -0,0 +1,44 @@ +error = $error; + parent::__construct(400, $error); + } + + /** + * Return the RFC 7807 error type (without the URL root) + */ + public function getErrorType(): string + { + return 'prefile-error'; + } + + /** + * Get the detailed error string + */ + public function getErrorDetails(): string + { + return $this->getMessage(); + } + + /** + * Return an array with the error details, merged with the RFC7807 response + */ + public function getErrorMetadata(): array + { + return []; + } +} diff --git a/app/Http/Controllers/Api/PirepController.php b/app/Http/Controllers/Api/PirepController.php index 1277a5b3..2ff64175 100644 --- a/app/Http/Controllers/Api/PirepController.php +++ b/app/Http/Controllers/Api/PirepController.php @@ -3,7 +3,6 @@ namespace App\Http\Controllers\Api; use App\Contracts\Controller; -use App\Events\PirepPrefiled; use App\Events\PirepUpdated; use App\Exceptions\AircraftPermissionDenied; use App\Exceptions\PirepCancelled; @@ -219,8 +218,6 @@ class PirepController extends Controller $this->updateFields($pirep, $request); $this->updateFares($pirep, $request); - event(new PirepPrefiled($pirep)); - return $this->get($pirep->id); } diff --git a/app/Listeners/PirepEventsHandler.php b/app/Listeners/PirepEventsHandler.php new file mode 100644 index 00000000..84d0d63a --- /dev/null +++ b/app/Listeners/PirepEventsHandler.php @@ -0,0 +1,29 @@ + 'onPirepPrefile', + ]; + + /** + * Called when a PIREP is prefiled + * + * @param PirepPrefiled $event + */ + public function onPirepPrefile(PirepPrefiled $event) + { + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 2fdd304a..d7cdc389 100755 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -9,6 +9,7 @@ use App\Listeners\AwardHandler; use App\Listeners\BidEventHandler; use App\Listeners\ExpenseListener; use App\Listeners\FinanceEventHandler; +use App\Listeners\PirepEventsHandler; use App\Listeners\UserStateListener; use App\Notifications\EventHandler; use Codedge\Updater\Events\UpdateAvailable; @@ -45,5 +46,6 @@ class EventServiceProvider extends ServiceProvider FinanceEventHandler::class, EventHandler::class, AwardHandler::class, + PirepEventsHandler::class, ]; } diff --git a/app/Services/PirepService.php b/app/Services/PirepService.php index 86043c85..666c2810 100644 --- a/app/Services/PirepService.php +++ b/app/Services/PirepService.php @@ -6,6 +6,7 @@ use App\Contracts\Service; use App\Events\PirepAccepted; use App\Events\PirepCancelled; use App\Events\PirepFiled; +use App\Events\PirepPrefiled; use App\Events\PirepRejected; use App\Events\UserStatsChanged; use App\Exceptions\AircraftInvalid; @@ -151,6 +152,8 @@ class PirepService extends Service } } + event(new PirepPrefiled($pirep)); + $pirep->save(); return $pirep;