ldc-loue/app/Jobs/ProductStatusUpdate.php
Jérémy Munsch aadb5c0acc Sprint2
2022-07-18 08:24:42 +00:00

41 lines
1.3 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Product;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
/**
* Update published and visibility attributes reading their
* publish_at and unpublish_at attributes.
*/
class ProductStatusUpdate
{
/**
* Called when the job will be executed.
*
* @return void
*/
public function handle()
{
$productsToPublish = Product::where('publish_at', '<=', Carbon::now()->toDateTime())
->whereNotNull('publish_at')
->whereStatus(false)->get();
Log::debug(__CLASS__ . " products to publish " . $productsToPublish->count());
foreach ($productsToPublish as $product) {
$product->publish_at = null;
$product->status = true;
$product->saveOrFail();
}
$productsToUnPublish = Product::where('unpublish_at', '<=', Carbon::now()->toDateTime())
->whereNotNull('unpublish_at')
->whereStatus(true)->get();
Log::debug(__CLASS__ . " products to unpublish " . $productsToPublish->count());
foreach ($productsToUnPublish as $product) {
$product->unpublish_at = null;
$product->status = false;
$product->saveOrFail();
}
}
}