57 lines
2.2 KiB
PHP
57 lines
2.2 KiB
PHP
<?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
|
|
{
|
|
// Set all models to apply status modifications.
|
|
collect([News::class, Strip::class])
|
|
->each(function (string $model) {
|
|
// Get all published model with an unpublish_at date passed.
|
|
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\News|\App\Models\Strip> $modelListToUnPublish */
|
|
$modelListToUnPublish = $model::query()
|
|
->whereNotNull('unpublish_at')
|
|
->where('unpublish_at', '<=', Carbon::now())
|
|
->where('published', true)
|
|
->get();
|
|
Log::debug(__CLASS__ . " model to unpublish " . $modelListToUnPublish->count());
|
|
|
|
// Unpublish all models with an unpublish_at date passed.
|
|
$modelListToUnPublish->each(function (News|Strip $modelToUnpublish) {
|
|
$modelToUnpublish->published = false;
|
|
$modelToUnpublish->saveOrFail();
|
|
});
|
|
|
|
// Get all unpublished model with a publish_at date passed.
|
|
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\News|\App\Models\Strip> $modelListToPublish */
|
|
$modelListToPublish = $model::query()
|
|
->where('publish_at', '<=', Carbon::now())
|
|
->whereNotNull('publish_at')
|
|
->where('published', false)
|
|
->get();
|
|
Log::debug(__CLASS__ . " model to publish " . $modelListToPublish->count());
|
|
|
|
// Publish all models with a publish_at date passed.
|
|
$modelListToPublish->each(function (News|Strip $modelToPublish) {
|
|
$modelToPublish->published = true;
|
|
$modelToPublish->saveOrFail();
|
|
});
|
|
});
|
|
}
|
|
}
|