From ddcb3c632f5c8a0aff697bce0b17f76407daaf14 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Sat, 2 Dec 2017 10:55:17 -0600 Subject: [PATCH] Add different PIREP and User events --- app/Events/PirepAccepted.php | 23 +++++++++++++++++++++++ app/Events/PirepFiled.php | 20 ++++++++++++++++++++ app/Events/PirepRejected.php | 20 ++++++++++++++++++++ app/Events/UserAccepted.php | 20 ++++++++++++++++++++ app/Events/UserRegistered.php | 20 ++++++++++++++++++++ app/Events/UserStateChanged.php | 20 ++++++++++++++++++++ app/Services/AircraftService.php | 21 --------------------- app/Services/PIREPService.php | 27 +++++++++++++++------------ app/Services/PilotService.php | 12 ++++++++++++ tests/AircraftTest.php | 4 ---- 10 files changed, 150 insertions(+), 37 deletions(-) create mode 100644 app/Events/PirepAccepted.php create mode 100644 app/Events/PirepFiled.php create mode 100644 app/Events/PirepRejected.php create mode 100644 app/Events/UserAccepted.php create mode 100644 app/Events/UserRegistered.php create mode 100644 app/Events/UserStateChanged.php diff --git a/app/Events/PirepAccepted.php b/app/Events/PirepAccepted.php new file mode 100644 index 00000000..ba367e59 --- /dev/null +++ b/app/Events/PirepAccepted.php @@ -0,0 +1,23 @@ +pirep = $pirep; + } +} diff --git a/app/Events/PirepRejected.php b/app/Events/PirepRejected.php new file mode 100644 index 00000000..b0c81ebd --- /dev/null +++ b/app/Events/PirepRejected.php @@ -0,0 +1,20 @@ +pirep = $pirep; + } +} diff --git a/app/Events/UserAccepted.php b/app/Events/UserAccepted.php new file mode 100644 index 00000000..9651d3e1 --- /dev/null +++ b/app/Events/UserAccepted.php @@ -0,0 +1,20 @@ +user = $user; + } +} diff --git a/app/Events/UserRegistered.php b/app/Events/UserRegistered.php new file mode 100644 index 00000000..069cf00d --- /dev/null +++ b/app/Events/UserRegistered.php @@ -0,0 +1,20 @@ +user = $user; + } +} diff --git a/app/Events/UserStateChanged.php b/app/Events/UserStateChanged.php new file mode 100644 index 00000000..7fa78985 --- /dev/null +++ b/app/Events/UserStateChanged.php @@ -0,0 +1,20 @@ +user = $user; + } +} diff --git a/app/Services/AircraftService.php b/app/Services/AircraftService.php index 0b70437f..4b6f94a8 100644 --- a/app/Services/AircraftService.php +++ b/app/Services/AircraftService.php @@ -2,28 +2,7 @@ namespace App\Services; -use App\Models\AircraftClass; -use Dompdf\Exception; - class AircraftService extends BaseService { - public function create( - array $attributes - ) { - - $repo = app('App\Repositories\SubfleetRepository'); - try { - $model = $repo->create($attributes); - } catch (Exception $e) { - return false; - } - - /*if ($class != null) { - $model->class()->associate($class); - $model->save(); - }*/ - - return $model; - } } diff --git a/app/Services/PIREPService.php b/app/Services/PIREPService.php index 2f5865f0..44d17109 100644 --- a/app/Services/PIREPService.php +++ b/app/Services/PIREPService.php @@ -5,6 +5,10 @@ namespace App\Services; use App\Models\Pirep; use App\Models\PirepFieldValues; +use App\Events\PirepAccepted; +use App\Events\PirepFiled; +use App\Events\PirepRejected; +use App\Events\UserStateChanged; class PIREPService extends BaseService { @@ -40,10 +44,6 @@ class PIREPService extends BaseService $default_status = $pirep->pilot->rank->auto_approve_manual; } - if ($default_status == config('enums.pirep_status.ACCEPTED')) { - $pirep = $this->accept($pirep); - } - $pirep->save(); $pirep->refresh(); @@ -56,6 +56,12 @@ class PIREPService extends BaseService $v->save(); } + event(new PirepFiled($pirep)); + + if ($default_status == config('enums.pirep_status.ACCEPTED')) { + $pirep = $this->accept($pirep); + } + # only update the pilot last state if they are accepted if ($default_status == config('enums.pirep_status.ACCEPTED')) { $this->setPilotState($pirep); @@ -128,6 +134,8 @@ class PIREPService extends BaseService $this->setPilotState($pirep); + event(new PirepAccepted($pirep)); + return $pirep; } @@ -154,6 +162,8 @@ class PIREPService extends BaseService $pirep->save(); $pirep->refresh(); + event(new PirepRejected($pirep)); + return $pirep; } @@ -163,14 +173,7 @@ class PIREPService extends BaseService $pilot->curr_airport_id = $pirep->arr_airport_id; $pilot->last_pirep_id = $pirep->id; $pilot->save(); - } - - /** - * Calculate all of the finances for a PIREP - * @param Pirep $pirep - */ - public function calculateFinances(Pirep &$pirep) - { + event(new UserStateChanged($pilot)); } } diff --git a/app/Services/PilotService.php b/app/Services/PilotService.php index 8760dac9..3f857dc3 100644 --- a/app/Services/PilotService.php +++ b/app/Services/PilotService.php @@ -2,9 +2,13 @@ namespace App\Services; +use App\Events\UserRegistered; use App\Models\User; use App\Models\Rank; use App\Models\Role; + +use App\Events\UserStateChanged; + use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Hash; @@ -17,6 +21,8 @@ class PilotService extends BaseService $pilot->flights = $pilot->flights + $count; $pilot->save(); + event(new UserStateChanged($pilot)); + return $pilot; } @@ -26,6 +32,8 @@ class PilotService extends BaseService $pilot->flight_time = $pilot->flight_time + $hours; $pilot->save(); + event(new UserStateChanged($pilot)); + return $pilot; } @@ -52,6 +60,8 @@ class PilotService extends BaseService $pilot->save(); + event(new UserStateChanged($pilot)); + return $pilot; } @@ -68,6 +78,8 @@ class PilotService extends BaseService $user->attachRole($role); # Let's check their rank $this->calculatePilotRank($user); + + event(new UserRegistered($user)); # TODO: Send out an email # Looking good, let's return their information diff --git a/tests/AircraftTest.php b/tests/AircraftTest.php index 5448075f..af75b86e 100644 --- a/tests/AircraftTest.php +++ b/tests/AircraftTest.php @@ -130,9 +130,5 @@ class AircraftTest extends TestCase $this->markTestSkipped( 'This test has not been implemented yet.' ); - return true; - # missing the name field - $svc = app('App\Services\AircraftService'); - $svc->create(['icao' => $this->ICAO]); } }