ldc-cocotte/app/Cqs/Actions/Slides/UpdateSlidesStatus.php
Jérémy Munsch ece1b7238d
fix(slides): update status handling for publishing and unpublishing
Refactor the logic to preserve scheduling dates when updating slide statuses.
2026-11-15 19:31:49 +01:00

68 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Cqs\Actions\Slides;
use App\Contracts\Action;
use App\Contracts\ActionInput;
use App\Contracts\ActionInputEmpty;
use App\Models\Slide;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Date;
use Psr\Log\LoggerInterface;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Action<\App\Contracts\ActionInputEmpty, void>
*/
final class UpdateSlidesStatus implements Action
{
public function __construct(
private readonly LoggerInterface $logger,
) {
}
/**
* @param \App\Contracts\ActionInputEmpty $input
*/
#[\Override]
public function __invoke(ActionInput $input): void
{
Assert::isInstanceOf($input, ActionInputEmpty::class);
$now = Date::now();
$unpublishQuery = Slide::query()
->where('date_end', '<=', $now)
->whereNotNull('date_end')
->where('status', true);
$unpublishCount = $unpublishQuery->count();
$this->logger->debug(self::class . ' slides to unpublish ' . $unpublishCount);
if ($unpublishCount > 0) {
$unpublishQuery->update([
'status' => false,
]);
}
$publishQuery = Slide::query()
->where('date_start', '<=', $now)
->whereNotNull('date_start')
->where(function (Builder $query) use ($now): void {
$query->whereNull('date_end')
->orWhere('date_end', '>', $now);
})
->where('status', false);
$publishCount = $publishQuery->count();
$this->logger->debug(self::class . ' slides to publish ' . $publishCount);
if ($publishCount > 0) {
$publishQuery->update([
'status' => true,
]);
}
}
}