- correction on table : PK is now Date instead of Timestamp - Contact::incrementStat now dispatches a job to handle concurent incrementations - Transactions are used on contact actions - date is no correctly casted - isoFormat is use with Carbon
59 lines
1.4 KiB
PHP
59 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Enums\ContactFormType;
|
|
use App\Models\Contact;
|
|
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\Carbon;
|
|
|
|
class IncrementContactStatsJob implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
/**
|
|
* @var \App\Enums\ContactFormType
|
|
*/
|
|
private $type;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @param \App\Enums\ContactFormType $type
|
|
* @return void
|
|
*/
|
|
public function __construct(ContactFormType $type)
|
|
{
|
|
$this->type = $type;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
$insert = function (ContactFormType $type) {
|
|
$contact = Contact::where('date', '=', Carbon::now()->isoFormat('YYYY-MM-DD'))->firstOrNew();
|
|
// * Can't do $contact->counter[$type->value]++ => ErrorException
|
|
$d = $contact->values;
|
|
$d[$type->value]++;
|
|
$contact->values = $d;
|
|
$contact->saveOrFail();
|
|
};
|
|
try {
|
|
$insert($this->type);
|
|
} catch (\Exception $e) {
|
|
// * Handle user requests colisions
|
|
$insert($this->type);
|
|
}
|
|
}
|
|
}
|