ldc-boutique-cnc-sable/app/Jobs/UpdateSlidesStatus.php

55 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Models\Slide;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Log;
class UpdateSlidesStatus implements ShouldQueue
{
use Queueable;
/**
* Execute the job.
*/
public function handle(): void
{
$now = Date::now();
$unpublishQuery = Slide::query()
->where('date_end', '<=', $now)
->whereNotNull('date_end')
->where('status', true);
$unpublishCount = $unpublishQuery->count();
Log::debug(self::class . ' slides to unpublish ' . $unpublishCount);
if ($unpublishCount > 0) {
$unpublishQuery->update([
'date_start' => null,
'date_end' => null,
'status' => false,
]);
}
$publishQuery = Slide::query()
->where('date_start', '<=', $now)
->whereNotNull('date_start')
->where('status', false);
$publishCount = $publishQuery->count();
Log::debug(self::class . ' slides to publish ' . $publishCount);
if ($publishCount > 0) {
$publishQuery->update([
'date_start' => null,
'status' => true,
]);
}
}
}