ldc-qrcollector-back/app/Jobs/IncrementUrlStat.php

60 lines
1.2 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Jobs;
use App\Models\Url;
use App\Models\UrlStats;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
class IncrementUrlStat implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
/**
* To be serialized by jobs system
*
* @var Url
* */
private $url;
/**
* Create a new job instance.
*
* @param \App\Models\Url $url
* @return void
*/
public function __construct(Url $url)
{
$this->url = $url;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$urlStat = UrlStats::query()
->whereDate('date', now())
->firstOrNew(
[
'url_key' => $this->url->key,
'url' => $this->url->value,
],
);
$urlStat->calls += 1;
$urlStat->saveOrFail();
Log::debug(sprintf('Stats incremented for URL : %s', $this->url->key));
}
}