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
This commit is contained in:
Nabeel S 2021-03-29 14:53:35 -04:00 committed by GitHub
parent ed146b2c70
commit 9c4aced837
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 78 additions and 3 deletions

View File

@ -0,0 +1,44 @@
<?php
namespace App\Exceptions;
/**
* Prefile Error
*
* If listening to the prefile event message, use `throw new PrefileError("message message");`
* to abort the prefile process and send the message up to ACARS
*/
class PrefileError extends AbstractHttpException
{
private $error;
public function __construct(string $error)
{
$this->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 [];
}
}

View File

@ -3,7 +3,6 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Contracts\Controller; use App\Contracts\Controller;
use App\Events\PirepPrefiled;
use App\Events\PirepUpdated; use App\Events\PirepUpdated;
use App\Exceptions\AircraftPermissionDenied; use App\Exceptions\AircraftPermissionDenied;
use App\Exceptions\PirepCancelled; use App\Exceptions\PirepCancelled;
@ -219,8 +218,6 @@ class PirepController extends Controller
$this->updateFields($pirep, $request); $this->updateFields($pirep, $request);
$this->updateFares($pirep, $request); $this->updateFares($pirep, $request);
event(new PirepPrefiled($pirep));
return $this->get($pirep->id); return $this->get($pirep->id);
} }

View File

@ -0,0 +1,29 @@
<?php
namespace App\Listeners;
use App\Contracts\Listener;
use App\Events\PirepPrefiled;
/**
* Look for and run any of the award classes. Don't modify this.
* See the documentation on creating awards:
*
* @url http://docs.phpvms.net/customizing/awards
*/
class PirepEventsHandler extends Listener
{
/** The events and the callback */
public static $callbacks = [
PirepPrefiled::class => 'onPirepPrefile',
];
/**
* Called when a PIREP is prefiled
*
* @param PirepPrefiled $event
*/
public function onPirepPrefile(PirepPrefiled $event)
{
}
}

View File

@ -9,6 +9,7 @@ use App\Listeners\AwardHandler;
use App\Listeners\BidEventHandler; use App\Listeners\BidEventHandler;
use App\Listeners\ExpenseListener; use App\Listeners\ExpenseListener;
use App\Listeners\FinanceEventHandler; use App\Listeners\FinanceEventHandler;
use App\Listeners\PirepEventsHandler;
use App\Listeners\UserStateListener; use App\Listeners\UserStateListener;
use App\Notifications\EventHandler; use App\Notifications\EventHandler;
use Codedge\Updater\Events\UpdateAvailable; use Codedge\Updater\Events\UpdateAvailable;
@ -45,5 +46,6 @@ class EventServiceProvider extends ServiceProvider
FinanceEventHandler::class, FinanceEventHandler::class,
EventHandler::class, EventHandler::class,
AwardHandler::class, AwardHandler::class,
PirepEventsHandler::class,
]; ];
} }

View File

@ -6,6 +6,7 @@ use App\Contracts\Service;
use App\Events\PirepAccepted; use App\Events\PirepAccepted;
use App\Events\PirepCancelled; use App\Events\PirepCancelled;
use App\Events\PirepFiled; use App\Events\PirepFiled;
use App\Events\PirepPrefiled;
use App\Events\PirepRejected; use App\Events\PirepRejected;
use App\Events\UserStatsChanged; use App\Events\UserStatsChanged;
use App\Exceptions\AircraftInvalid; use App\Exceptions\AircraftInvalid;
@ -151,6 +152,8 @@ class PirepService extends Service
} }
} }
event(new PirepPrefiled($pirep));
$pirep->save(); $pirep->save();
return $pirep; return $pirep;