2018-04-13 05:12:32 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Cron\Nightly;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Listener;
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Events\CronNightly;
|
2018-04-13 05:12:32 +08:00
|
|
|
use App\Models\Enums\Days;
|
|
|
|
use App\Models\Flight;
|
|
|
|
use Carbon\Carbon;
|
2018-07-13 11:23:45 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-04-13 05:12:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Figure out what flights need to be active for today
|
|
|
|
*/
|
|
|
|
class SetActiveFlights extends Listener
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param CronNightly $event
|
|
|
|
*/
|
|
|
|
public function handle(CronNightly $event): void
|
|
|
|
{
|
|
|
|
$this->checkFlights();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look through every single flight, check the start/end dates,
|
|
|
|
* as well of the days of week if this flight is active on this day
|
|
|
|
*
|
|
|
|
* TODO: Option to check the flight active/inactive against departure TZ
|
|
|
|
* TODO: Move to FlightService
|
|
|
|
*/
|
|
|
|
public function checkFlights(): void
|
|
|
|
{
|
|
|
|
$today = Carbon::now('UTC');
|
|
|
|
$flights = Flight::all();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Flight $flight
|
|
|
|
*/
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($flights as $flight) {
|
2018-07-13 11:23:45 +08:00
|
|
|
if (!$flight->active) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-13 05:12:32 +08:00
|
|
|
// dates aren't set, so just save if there were any changes above
|
|
|
|
// and move onto the next one
|
|
|
|
if ($flight->start_date === null || $flight->end_date === null) {
|
2018-07-25 03:58:12 +08:00
|
|
|
if ($flight->days !== null && $flight->days > 0) {
|
2018-07-13 11:23:45 +08:00
|
|
|
$visible = Days::isToday($flight->days);
|
2018-07-25 03:58:12 +08:00
|
|
|
if ($flight->visible !== $visible) {
|
|
|
|
Log::info('Flight '.$flight->ident.' to '.($visible ? 'shown' : 'hidden'));
|
|
|
|
|
2018-07-13 11:23:45 +08:00
|
|
|
$flight->visible = $visible;
|
2018-07-25 03:58:12 +08:00
|
|
|
if ($visible === false) {
|
|
|
|
Log::info('Today='.date('N').', start=no, mask='.$flight->days.', in='
|
|
|
|
.Days::in($flight->days, Days::$isoDayMap[(int) date('N')]));
|
|
|
|
}
|
2018-07-13 11:23:45 +08:00
|
|
|
}
|
2018-07-13 11:29:50 +08:00
|
|
|
} else {
|
|
|
|
Log::info('Toggling flight '.$flight->ident.' to visible');
|
|
|
|
$flight->visible = true;
|
2018-04-13 05:12:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$flight->save();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the day of week now first
|
|
|
|
|
|
|
|
// Start/end date is set, so make sure today is valid for it to be alive
|
|
|
|
// and then make sure if days of the week are specified, check that too
|
|
|
|
if ($today->gte($flight->start_date) && $today->lte($flight->end_date)) {
|
2018-07-25 03:58:12 +08:00
|
|
|
if ($flight->days !== null && $flight->days > 0) {
|
2018-07-13 11:23:45 +08:00
|
|
|
$visible = Days::isToday($flight->days);
|
2018-08-27 00:40:04 +08:00
|
|
|
if ($flight->visible !== $visible) {
|
|
|
|
Log::info('Toggling flight '.$flight->ident.' to '.($visible ? 'shown' : 'hidden').'');
|
2018-07-25 03:58:12 +08:00
|
|
|
|
2018-07-13 20:43:27 +08:00
|
|
|
$flight->visible = $visible;
|
2018-07-25 03:58:12 +08:00
|
|
|
if ($visible === false) {
|
|
|
|
Log::info('Today='.date('N').', start='.$flight->start_date
|
|
|
|
.', end='.$flight->end_date.', mask='.$flight->days.', in='
|
|
|
|
.Days::in($flight->days, Days::$isoDayMap[(int) date('N')]));
|
|
|
|
}
|
2018-07-13 11:23:45 +08:00
|
|
|
}
|
2018-04-13 05:12:32 +08:00
|
|
|
} else {
|
2018-07-25 03:58:12 +08:00
|
|
|
Log::info('Toggling flight '.$flight->ident.' to visible');
|
|
|
|
$flight->visible = true;
|
2018-04-13 05:12:32 +08:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-13 11:23:45 +08:00
|
|
|
$flight->visible = false;
|
2018-07-25 03:58:12 +08:00
|
|
|
Log::info('Toggling flight '.$flight->ident.' to hidden, outside of start/end boundary');
|
2018-04-13 05:12:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$flight->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|