ldc-legaulois/app/Jobs/SendBoutiqueMails.php
2021-03-04 09:38:39 +01:00

61 lines
1.6 KiB
PHP

<?php
namespace App\Jobs;
use App\Mail\Order as MailOrder;
use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
/**
* On utilise pas unique jobs à cause d'une potentielle limitation
*
* https://laravel.com/docs/8.x/queues#unique-jobs
* Unique jobs require a cache driver that supports locks.
* Currently, the memcached, redis, dynamodb, database, file, and array
* cache drivers support atomic locks. In addition, unique job constraints
* do not apply to jobs within batches.
*/
class SendBoutiqueMails implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
/** @var \App\Models\Order */
protected $order;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Order $order)
{
$this->order = $order;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if (!$this->order->confirmMailSent) {
Mail::to($this->order->email)
->send(new MailOrder($this->order));
$this->order->confirmMailSent = true;
$this->order->saveOrFail();
Log::debug('Boutique mail is sent', $this->order->toArray());
} else {
Log::debug('Boutique mail is already sent', $this->order->toArray());
}
}
}