54 lines
1.8 KiB
PHP
54 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Game;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/**
|
|
* Update published attribute reading its
|
|
* publish_at and unpublish_at attributes.
|
|
*/
|
|
class GamesStatusUpdate
|
|
{
|
|
/**
|
|
* Called when the job will be executed.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\News> */
|
|
$gamesListToUnPublish = Game::query()
|
|
->where('unpublish_at', '<=', Carbon::now())
|
|
->whereNotNull('unpublish_at')
|
|
->where('published', true)->get();
|
|
Log::debug(__CLASS__ . " games to unpublish " . $gamesListToUnPublish->count());
|
|
foreach ($gamesListToUnPublish as $game) {
|
|
$game->publish_at = null;
|
|
$game->unpublish_at = null;
|
|
$game->published = false;
|
|
$game->saveOrFail();
|
|
}
|
|
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\Game> */
|
|
$gamesListToPublish = Game::query()
|
|
->where('publish_at', '<=', Carbon::now())
|
|
->whereNotNull('publish_at')
|
|
->where('published', false)->get();
|
|
Log::debug(__CLASS__ . " games to publish " . $gamesListToPublish->count());
|
|
// Unpublish all game with no date because only 1 game can be published.
|
|
if ($gamesListToPublish->count()) {
|
|
Game::query()->where('published', true)
|
|
->each(function (Game $game) {
|
|
$game->published = false;
|
|
$game->saveOrFail();
|
|
});
|
|
}
|
|
foreach ($gamesListToPublish as $game) {
|
|
$game->publish_at = null;
|
|
$game->published = true;
|
|
$game->saveOrFail();
|
|
}
|
|
}
|
|
}
|