ldc-volena/app/Jobs/UpdatePlanificatedStatus.php

55 lines
2 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Jobs;
use App\Models\News;
use App\Models\Strip;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
/**
* Update published attribute reading its
* publish_at and unpublish_at attributes.
*/
class UpdatePlanificatedStatus
{
/**
* Called when the job will be executed.
*
* @return void
*/
public function handle(): void
{
collect([News::class, Strip::class])
->each(function (string $model) {
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\News|\App\Models\Strip> */
$modelListToPublish = $model::query()
->whereNotNull('publish_at')
->where('publish_at', '<=', Carbon::now())
->where(function ($query) {
$query->whereNull('unpublish_at')
->orWhere('unpublish_at', '>', Carbon::now());
})
->where('published', false)
->get();
Log::debug(__CLASS__ . " models to publish " . $modelListToPublish->count());
$modelListToPublish->each(function ($currentModel) {
$currentModel->published = true;
$currentModel->saveOrFail();
});
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\News|\App\Models\Strip> */
$modelListToUnpublish = $model::query()
->whereNotNull('unpublish_at')
->where('unpublish_at', '<=', Carbon::now())
->where('published', true)
->get();
Log::debug(__CLASS__ . " models to unpublish " . $modelListToUnpublish->count());
$modelListToUnpublish->each(function ($currentModel) {
$currentModel->published = false;
$currentModel->saveOrFail();
});
});
}
}