38 lines
1,001 B
PHP
38 lines
1,001 B
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Bo;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Models\Url;
|
||
|
|
use App\Models\UrlStats;
|
||
|
|
use Exception;
|
||
|
|
|
||
|
|
class UrlStatsController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Count every qrcode use and redirect to intended route
|
||
|
|
* @param string $key
|
||
|
|
* @return Response
|
||
|
|
* @throws Exception Throw exception if the key doesn't exist.
|
||
|
|
*/
|
||
|
|
public function count(string $key)
|
||
|
|
{
|
||
|
|
$url = Url::where('key', $key)->first();
|
||
|
|
if (!$url) {
|
||
|
|
throw new Exception('Lien incorrect');
|
||
|
|
}
|
||
|
|
$stats = $url->stats()->where('date', now()->toDateString())->first();
|
||
|
|
if ($stats) {
|
||
|
|
$stats->calls += 1;
|
||
|
|
$stats->saveOrFail();
|
||
|
|
} else {
|
||
|
|
$stats = new UrlStats();
|
||
|
|
$stats->calls = 1;
|
||
|
|
$stats->date = now()->toDateString();
|
||
|
|
$stats->url()->associate($url);
|
||
|
|
$stats->saveOrFail();
|
||
|
|
}
|
||
|
|
return redirect($url->value);
|
||
|
|
}
|
||
|
|
}
|