57 lines
1.3 KiB
PHP
57 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Service;
|
|
use App\Models\ServiceStat;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class SetServiceStats implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
/** Used to prevent overlaping */
|
|
private const JOBKEY = 95563222;
|
|
|
|
/** @var \App\Models\Service */
|
|
protected $service;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(Service $service)
|
|
{
|
|
$this->service = $service;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
$serviceStat = ServiceStat::firstOrNew(['date' => date('Y-m-d')]);
|
|
if (!$serviceStat->exists) {
|
|
$serviceStat->service()->associate($this->service);
|
|
$serviceStat->brand()->associate($this->service->brand);
|
|
}
|
|
$serviceStat->nb_calls += 1;
|
|
$serviceStat->saveOrFail();
|
|
}
|
|
|
|
public function middleware()
|
|
{
|
|
return [(new WithoutOverlapping(self::JOBKEY))->releaseAfter(30)];
|
|
}
|
|
}
|