#355 Calculate distance button (#366)

* #355 Calculate distance button in add/edit Flight page

* Styling

* Move add/edit flight logic out of controller and into service layer

* Styling

* Formatting

* Run styleci against modules dir

* Styleci config

* Style fixes in /modules
This commit is contained in:
Nabeel S
2019-08-26 12:32:46 -04:00
committed by GitHub
parent 25999d55a3
commit bbec276da8
57 changed files with 9819 additions and 7522 deletions

View File

@@ -5,8 +5,15 @@ namespace App\Services;
use App\Contracts\AirportLookup as AirportLookupProvider;
use App\Contracts\Metar as MetarProvider;
use App\Contracts\Service;
use App\Exceptions\AirportNotFound;
use App\Repositories\AirportRepository;
use App\Support\Metar;
use App\Support\Units\Distance;
use Illuminate\Support\Facades\Cache;
use League\Geotools\Coordinate\Coordinate;
use League\Geotools\Geotools;
use PhpUnitsOfMeasure\Exception\NonNumericValue;
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
use VaCentral\Airport;
/**
@@ -14,14 +21,17 @@ use VaCentral\Airport;
*/
class AirportService extends Service
{
private $airportRepo;
private $lookupProvider;
private $metarProvider;
public function __construct(
AirportLookupProvider $lookupProvider,
AirportRepository $airportRepo,
MetarProvider $metarProvider
) {
$this->airportRepo = $airportRepo;
$this->lookupProvider = $lookupProvider;
$this->metarProvider = $metarProvider;
}
@@ -76,4 +86,42 @@ class AirportService extends Service
return $airport;
}
/**
* Calculate the distance from one airport to another
*
* @param string $fromIcao
* @param string $toIcao
*
* @return Distance
*/
public function calculateDistance($fromIcao, $toIcao)
{
$from = $this->airportRepo->find($fromIcao, ['lat', 'lon']);
$to = $this->airportRepo->find($toIcao, ['lat', 'lon']);
if (!$from) {
throw new AirportNotFound($fromIcao);
}
if (!$to) {
throw new AirportNotFound($toIcao);
}
// Calculate the distance
$geotools = new Geotools();
$start = new Coordinate([$from->lat, $from->lon]);
$end = new Coordinate([$to->lat, $to->lon]);
$dist = $geotools->distance()->setFrom($start)->setTo($end);
// Convert into a Distance object
try {
$distance = new Distance($dist->in('mi')->greatCircle(), 'mi');
return $distance;
} catch (NonNumericValue $e) {
return;
} catch (NonStringUnitName $e) {
return;
}
}
}

View File

@@ -4,19 +4,23 @@ namespace App\Services;
use App\Contracts\Service;
use App\Exceptions\BidExistsForFlight;
use App\Exceptions\DuplicateFlight;
use App\Models\Bid;
use App\Models\Enums\Days;
use App\Models\Flight;
use App\Models\FlightFieldValue;
use App\Models\User;
use App\Repositories\FlightRepository;
use App\Repositories\NavdataRepository;
use Log;
use App\Support\Units\Time;
use Illuminate\Support\Facades\Log;
/**
* Class FlightService
*/
class FlightService extends Service
{
private $airportSvc;
private $flightRepo;
private $navDataRepo;
private $userSvc;
@@ -24,20 +28,98 @@ class FlightService extends Service
/**
* FlightService constructor.
*
* @param AirportService $airportSvc
* @param FlightRepository $flightRepo
* @param NavdataRepository $navdataRepo
* @param UserService $userSvc
*/
public function __construct(
AirportService $airportSvc,
FlightRepository $flightRepo,
NavdataRepository $navdataRepo,
UserService $userSvc
) {
$this->airportSvc = $airportSvc;
$this->flightRepo = $flightRepo;
$this->navDataRepo = $navdataRepo;
$this->userSvc = $userSvc;
}
/**
* Create a new flight
*
* @param array $fields
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
*
* @return \Illuminate\Http\RedirectResponse
*/
public function createFlight($fields)
{
$flightTmp = new Flight($fields);
if ($this->isFlightDuplicate($flightTmp)) {
throw new DuplicateFlight($flightTmp);
}
$fields = $this->transformFlightFields($fields);
$flight = $this->flightRepo->create($fields);
return $flight;
}
/**
* Update a flight with values from the given fields
*
* @param Flight $flight
* @param array $fields
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
*
* @return \App\Models\Flight|mixed
*/
public function updateFlight($flight, $fields)
{
// apply the updates here temporarily, don't save
// the repo->update() call will actually do it
$flight->fill($fields);
if ($this->isFlightDuplicate($flight)) {
throw new DuplicateFlight($flight);
}
$fields = $this->transformFlightFields($fields);
$flight = $this->flightRepo->update($fields, $flight->id);
return $flight;
}
/**
* Check the fields for a flight and transform them
*
* @param array $fields
*
* @return array
*/
protected function transformFlightFields($fields)
{
if (array_key_exists('days', $fields) && filled($fields['days'])) {
$fields['days'] = Days::getDaysMask($fields['days']);
}
$fields['flight_time'] = Time::init($fields['minutes'], $fields['hours'])->getMinutes();
$fields['active'] = get_truth_state($fields['active']);
// Figure out a distance if not found
if (empty($fields['distance'])) {
$fields['distance'] = $this->airportSvc->calculateDistance(
$fields['dpt_airport_id'],
$fields['arr_airport_id']
);
}
return $fields;
}
/**
* Filter out any flights according to different settings
*