2018-08-20 22:42:54 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Cron\Nightly;
|
|
|
|
|
|
|
|
use App\Events\CronNightly;
|
|
|
|
use App\Interfaces\Listener;
|
|
|
|
use App\Models\Enums\UserState;
|
|
|
|
use App\Repositories\UserRepository;
|
|
|
|
use App\Services\UserService;
|
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This recalculates the balances on all of the journals
|
|
|
|
*/
|
|
|
|
class RecalculateStats extends Listener
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
private $userRepo;
|
|
|
|
private $userSvc;
|
2018-08-20 22:42:54 +08:00
|
|
|
|
|
|
|
public function __construct(UserRepository $userRepo, UserService $userService)
|
|
|
|
{
|
|
|
|
$this->userRepo = $userRepo;
|
|
|
|
$this->userSvc = $userService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recalculate the stats for active users
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-08-20 22:42:54 +08:00
|
|
|
* @param CronNightly $event
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-08-20 22:42:54 +08:00
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function handle(CronNightly $event): void
|
|
|
|
{
|
|
|
|
Log::info('Recalculating balances');
|
|
|
|
|
|
|
|
$w = [
|
2018-08-27 00:40:04 +08:00
|
|
|
['state', '!=', UserState::REJECTED],
|
2018-08-20 22:42:54 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
$users = $this->userRepo->findWhere($w, ['id', 'name', 'airline_id']);
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$this->userSvc->recalculateStats($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::info('Done recalculating stats');
|
|
|
|
}
|
|
|
|
}
|