phpvms/app/Cron/Nightly/PilotLeave.php

51 lines
1.2 KiB
PHP
Raw Normal View History

<?php
2018-04-07 06:14:01 +08:00
namespace App\Cron\Nightly;
use App\Events\CronNightly;
use App\Contracts\Listener;
use App\Models\Enums\UserState;
use App\Models\User;
use App\Services\UserService;
use Carbon\Carbon;
/**
* Determine if any pilots should be set to ON LEAVE status
*/
class PilotLeave extends Listener
{
private $userSvc;
/**
* PilotLeave constructor.
*/
public function __construct(UserService $userSvc)
{
$this->userSvc = $userSvc;
}
/**
* Set any users to being on leave after X days
2018-08-27 00:40:04 +08:00
*
* @param CronNightly $event
2018-08-27 00:40:04 +08:00
*
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
*/
public function handle(CronNightly $event): void
{
2018-08-27 00:40:04 +08:00
if (setting('pilots.auto_leave_days') === 0) {
return;
}
$date = Carbon::now()->subDay(setting('pilots.auto_leave_days'));
$users = User::where('status', UserState::ACTIVE)
->whereDate('updated_at', '<', $date);
2018-08-27 00:40:04 +08:00
foreach ($users as $user) {
Log::info('Setting user '.$user->ident.' to ON LEAVE status');
2018-03-31 11:36:01 +08:00
$this->userSvc->setStatusOnLeave($user);
}
}
}