ldc-tregalette/app/Jobs/GamesStatusUpdate.php

55 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2023-12-15 15:53:55 +00:00
<?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()
{
2023-12-15 16:59:41 +01:00
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\News> */
2023-12-15 15:53:55 +00:00
$gamesListToUnPublish = Game::query()
2023-12-15 16:59:41 +01:00
->where('unpublish_at', '<=', Carbon::now())
2023-12-15 15:53:55 +00:00
->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();
}
2023-12-15 16:59:41 +01:00
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\Game> */
2023-12-15 15:53:55 +00:00
$gamesListToPublish = Game::query()
2023-12-15 16:59:41 +01:00
->where('publish_at', '<=', Carbon::now())
2023-12-15 15:53:55 +00:00
->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()) {
2023-12-15 16:59:41 +01:00
Game::query()->where('published', true)
->each(function (Game $game) {
$game->published = false;
$game->saveOrFail();
});
2023-12-15 15:53:55 +00:00
}
foreach ($gamesListToPublish as $game) {
$game->publish_at = null;
$game->published = true;
$game->saveOrFail();
}
}
}