phpvms/app/Services/PIREPService.php

204 lines
5.4 KiB
PHP
Raw Normal View History

2017-06-10 14:50:00 +08:00
<?php
namespace App\Services;
2017-07-02 10:06:55 +08:00
use App\Models\Pirep;
use App\Models\PirepFieldValues;
2017-12-03 00:55:17 +08:00
use App\Events\PirepAccepted;
use App\Events\PirepFiled;
use App\Events\PirepRejected;
use App\Events\UserStateChanged;
2017-06-10 14:50:00 +08:00
use App\Repositories\PirepRepository;
use Log;
class PIREPService extends BaseService
{
protected $pilotSvc, $pirepRepo;
2017-06-10 14:50:00 +08:00
/**
* PIREPService constructor.
2017-12-13 04:48:03 +08:00
* @param UserService $pilotSvc
* @param PirepRepository $pirepRepo
2017-06-10 14:50:00 +08:00
*/
public function __construct(
2017-12-13 04:48:03 +08:00
UserService $pilotSvc,
PirepRepository $pirepRepo
) {
$this->pilotSvc = $pilotSvc;
$this->pirepRepo = $pirepRepo;
2017-06-10 14:50:00 +08:00
}
/**
* Create a new PIREP with some given fields
*
* @param Pirep $pirep
* @param array [PirepFieldValues] $field_values
*
* @return Pirep
*/
public function create(Pirep &$pirep, array $field_values): Pirep
{
if($field_values === null) {
$field_values = [];
}
2017-07-02 10:06:55 +08:00
# Figure out what default state should be. Look at the default
# behavior from the rank that the pilot is assigned to
if($pirep->source === config('enums.sources.ACARS')) {
$default_status = $pirep->pilot->rank->auto_approve_acars;
} else {
$default_status = $pirep->pilot->rank->auto_approve_manual;
}
2017-07-02 10:06:55 +08:00
$pirep->save();
$pirep->refresh();
foreach ($field_values as $fv) {
2017-07-02 10:06:55 +08:00
$v = new PirepFieldValues();
$v->pirep_id = $pirep->id;
2017-07-02 10:06:55 +08:00
$v->name = $fv['name'];
$v->value = $fv['value'];
$v->source = $fv['source'];
$v->save();
}
2017-06-10 14:50:00 +08:00
Log::info('New PIREP filed', [$pirep]);
2017-12-03 00:55:17 +08:00
event(new PirepFiled($pirep));
if ($default_status === config('enums.pirep_status.ACCEPTED')) {
2017-12-03 00:55:17 +08:00
$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);
}
return $pirep;
}
/**
* @param Pirep $pirep
* @param int $new_status
* @return Pirep
*/
public function changeStatus(Pirep &$pirep, int $new_status): Pirep
{
Log::info('PIREP ' . $pirep->id . ' status change from '.$pirep->status.' to ' . $new_status);
if ($pirep->status === $new_status) {
return $pirep;
}
/**
* Move from a PENDING status into either ACCEPTED or REJECTED
*/
if ($pirep->status === config('enums.pirep_status.PENDING')) {
if ($new_status === config('enums.pirep_status.ACCEPTED')) {
return $this->accept($pirep);
} elseif ($new_status === config('enums.pirep_status.REJECTED')) {
return $this->reject($pirep);
} else {
return $pirep;
}
}
/*
* Move from a ACCEPTED to REJECTED status
*/
elseif ($pirep->status === config('enums.pirep_status.ACCEPTED')) {
$pirep = $this->reject($pirep);
return $pirep;
}
/**
* Move from REJECTED to ACCEPTED
*/
elseif ($pirep->status === config('enums.pirep_status.REJECTED')) {
$pirep = $this->accept($pirep);
return $pirep;
}
}
/**
* @param Pirep $pirep
* @return Pirep
*/
public function accept(Pirep &$pirep): Pirep
{
# moving from a REJECTED state to ACCEPTED, reconcile statuses
if ($pirep->status === config('enums.pirep_status.ACCEPTED')) {
return $pirep;
}
$ft = $pirep->flight_time;
2017-07-05 02:57:08 +08:00
$pilot = $pirep->pilot;
$this->pilotSvc->adjustFlightHours($pilot, $ft);
$this->pilotSvc->adjustFlightCount($pilot, +1);
$this->pilotSvc->calculatePilotRank($pilot);
$pirep->pilot->refresh();
# Change the status
2017-07-05 04:43:47 +08:00
$pirep->status = config('enums.pirep_status.ACCEPTED');
$pirep->save();
$pirep->refresh();
$this->setPilotState($pirep);
Log::info('PIREP '.$pirep->id.' status change to ACCEPTED');
2017-12-03 00:55:17 +08:00
event(new PirepAccepted($pirep));
return $pirep;
}
/**
* @param Pirep $pirep
* @return Pirep
*/
public function reject(Pirep &$pirep): Pirep
{
# If this was previously ACCEPTED, then reconcile the flight hours
# that have already been counted, etc
if ($pirep->status === config('enums.pirep_status.ACCEPTED')) {
$pilot = $pirep->pilot;
$ft = $pirep->flight_time * -1;
$this->pilotSvc->adjustFlightHours($pilot, $ft);
$this->pilotSvc->adjustFlightCount($pilot, -1);
$this->pilotSvc->calculatePilotRank($pilot);
$pirep->pilot->refresh();
}
# Change the status
2017-07-05 04:43:47 +08:00
$pirep->status = config('enums.pirep_status.REJECTED');
$pirep->save();
$pirep->refresh();
Log::info('PIREP ' . $pirep->id . ' status change to REJECTED');
2017-12-03 00:55:17 +08:00
event(new PirepRejected($pirep));
return $pirep;
2017-06-10 14:50:00 +08:00
}
/**
* @param Pirep $pirep
*/
public function setPilotState(Pirep &$pirep)
{
$pilot = $pirep->pilot;
$pilot->refresh();
$pilot->curr_airport_id = $pirep->arr_airport_id;
$pilot->last_pirep_id = $pirep->id;
$pilot->save();
2017-12-03 00:55:17 +08:00
event(new UserStateChanged($pilot));
}
2017-06-10 14:50:00 +08:00
}