51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
final class BrowserService
|
|
{
|
|
public static function userAgent()
|
|
{
|
|
return app('browser-detect')->userAgent();
|
|
}
|
|
|
|
public static function findUA(?string $ua = null): ?int
|
|
{
|
|
$ua ??= app('browser-detect')->userAgent();
|
|
$row = DB::table('user_agents')->where('user_agent', $ua)->first(['id']);
|
|
if ($row) {
|
|
return $row->id;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static function upsertBrowser(?string $ua = null): int
|
|
{
|
|
$browser = blank($ua)
|
|
? app('browser-detect')->detect()
|
|
: app('browser-detect')->parse($ua);
|
|
|
|
$id = self::findUA($browser->userAgent());
|
|
if ($id) {
|
|
return $id;
|
|
}
|
|
$params = [
|
|
'user_agent' => $browser->userAgent(),
|
|
'device_type' => $browser->deviceType(),
|
|
'device_family' => $browser->deviceFamily(),
|
|
'device_model' => $browser->deviceModel(),
|
|
'browser_name' => $browser->browserName(),
|
|
'browser_version' => $browser->browserVersion(),
|
|
'platform' => $browser->platformFamily(),
|
|
'platform_version' => $browser->platformVersion(),
|
|
'created_at' => now(),
|
|
];
|
|
DB::table('user_agents')->insert($params);
|
|
|
|
return self::findUA($browser->userAgent());
|
|
}
|
|
}
|