49 lines
1.6 KiB
PHP
49 lines
1.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Jobs;
|
||
|
|
|
||
|
|
use App\Enums\News\NewsStatusEnum;
|
||
|
|
use App\Models\News;
|
||
|
|
use Illuminate\Support\Carbon;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update status attribute reading its
|
||
|
|
* published_at and unpublished_at attributes.
|
||
|
|
*/
|
||
|
|
class NewsStatusUpdate
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Called when the job will be executed.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function handle(): void
|
||
|
|
{
|
||
|
|
// Publish all unpublish models if the published_at date has passed.
|
||
|
|
/** @var \Illuminate\Database\Eloquent\Collection<array-key, \App\Models\News> */
|
||
|
|
$newsListToPublish = News::query()
|
||
|
|
->where('published_at', '<=', Carbon::now())
|
||
|
|
->whereNotNull('published_at')
|
||
|
|
->where('status', NewsStatusEnum::unpublished)
|
||
|
|
->get();
|
||
|
|
Log::debug(__CLASS__ . " news to publish " . $newsListToPublish->count());
|
||
|
|
foreach ($newsListToPublish as $news) {
|
||
|
|
$news->status = NewsStatusEnum::published;
|
||
|
|
$news->saveQuietly();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Unpublish all publish models if the unpublished_at date has passed.
|
||
|
|
/** @var \Illuminate\Database\Eloquent\Collection<array-key, \App\Models\News> */
|
||
|
|
$newsListToUnPublish = News::query()
|
||
|
|
->where('unpublished_at', '<=', Carbon::now())
|
||
|
|
->whereNotNull('unpublished_at')
|
||
|
|
->where('status', NewsStatusEnum::published)->get();
|
||
|
|
Log::debug(__CLASS__ . " news to unpublish " . $newsListToPublish->count());
|
||
|
|
foreach ($newsListToUnPublish as $news) {
|
||
|
|
$news->status = NewsStatusEnum::unpublished;
|
||
|
|
$news->saveQuietly();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|