-
app/Jobs/ProcessLinkStat.php
Open in GitHubuse Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Queue\Queueable; use App\Services\UserAgentService; use App\Enums\LinkStat\Event; use App\Models\Link; use App\Models\LinkStat; class ProcessLinkStat implements ShouldQueue { use Queueable; public function __construct( public Link $link, public string $userAgent, public array $languages, public string $ip, public bool $qr, public ?string $referer ) {} public function handle(): void { $service = new UserAgentService(); $geo = geoip($this->ip); $linkStat = LinkStat::create([ 'workspace_id' => $this->link->workspace_id, 'link_id' => $this->link->id, 'event' => $this->qr ? Event::QR_SCAN : Event::CLICK, 'country' => $geo->iso_code, 'region' => $geo->state_name, 'city' => $geo->city, 'browser' => $service->getBrowser($this->userAgent), 'os' => $service->getOS($this->userAgent), 'device' => $service->getDevice($this->userAgent), 'language' => $service->getLanguage($this->languages), 'referer' => $service->getReferer($this->referer), 'ip' => $this->ip, ]); $this->link->update([ 'clicks' => $this->link->clicks + 1, 'last_click' => now(), ]); } }
-
app/Http/Controllers/LinkController.php
Open in GitHubuse Illuminate\Http\Request; use App\Jobs\ProcessLinkStat; use App\Models\Link; class LinkController extends Controller { // ... public function show($key, Request $request) { $link = Link::where('link', $request->url())->firstOrFail(); ProcessLinkStat::dispatch( $link, $request->userAgent(), $request->getLanguages(), $request->ip(), $request->input('qr') ? true : false, $request->header('Referer') ); return redirect($link->url, 302); } }
-
app/Http/Controllers/RedirectController.php
Open in GitHubuse Illuminate\Support\Facades\Gate; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use App\Jobs\ProcessLinkStat; use App\Models\Link; class RedirectController extends Controller { public function __invoke($key, Request $request): RedirectResponse { $link = Link::where('link', $request->url()) ->with('workspace') ->firstOrFail(); $reachEventLimit = Gate::inspect('reached-event-limit', $link->workspace); ProcessLinkStat::dispatchIf( $reachEventLimit->allowed(), $link, $request->userAgent(), $request->getLanguages(), $request->ip(), $request->input('qr') ? true : false, $request->header('Referer') ); return redirect($link->url, 302); } }