<?php
namespace App\Console\Commands;
use App\Modules\Catalog\Models\Product;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
/**
* Scheduled: every 5 minutes (see Console/Kernel.php)
*
* Publishes products whose publish_at timestamp has passed
* and whose status is still 'scheduled'.
*/
class PublishScheduledProductsCommand extends Command
{
protected $signature = 'products:publish-scheduled';
protected $description = 'Publish products whose scheduled publish time has arrived';
public function handle(): int
{
$count = Product::where('status', 'scheduled')
->where('published_at', '<=', now())
->update(['status' => 'active']);
if ($count > 0) {
Log::info("PublishScheduledProducts: activated {$count} product(s)");
// Re-index in search engine
Product::where('status', 'active')
->where('published_at', '<=', now())
->where('published_at', '>=', now()->subMinutes(6))
->searchable();
}
return Command::SUCCESS;
}
}