123movies-seo/app/Services/DeviceDetect.php

88 lines
No EOL
2.9 KiB
PHP

<?php
namespace App\Services;
use DeviceDetector\ClientHints;
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\AbstractDeviceParser;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use DeviceDetector\Parser\OperatingSystem;
use DeviceDetector\Parser\Client\Browser;
use Illuminate\Support\Str;
use stdClass;
class DeviceDetect {
public static function getDeviceDetector($userAgent = null):stdClass
{
AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE);
$clientHints = ClientHints::factory($_SERVER);
$dd = new DeviceDetector($userAgent, $clientHints);
$dd->discardBotInformation();
$dd->skipBotDetection();
if (is_null($userAgent)) {
return (Object)[
'os' => null,
'browser' => null,
'device' => null,
'is_bot' => null,
'is_safari' => null,
'is_chrome' => null,
'is_mobile' => null,
'is_mac' =>null,
'is_windows' =>null,
];
}
AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE);
$clientHints = ClientHints::factory($_SERVER);
$dd = new DeviceDetector($userAgent, $clientHints);
$dd->discardBotInformation();
$dd->skipBotDetection();
$redisConnection = RedisAdapter::createConnection('redis:///var/lib/redis/redis.sock/14');
$cache = new RedisAdapter($redisConnection, 43200);
// $cache = new ApcuAdapter();
$data = $cache->get(Str::slug($userAgent).'_', function() use ($cache, $dd) {
$dd->setCache(
new \DeviceDetector\Cache\PSR6Bridge($cache)
);
$dd->parse();
$osFamily = OperatingSystem::getOsFamily($dd->getOs('name'));
$browserFamily = Browser::getBrowserFamily($dd->getClient('name'));
if ($dd->isTablet() || $dd->isPhablet()) {
$viewport = 'tablet';
} else if ($dd->isSmartphone() || $dd->isFeaturePhone()) {
$viewport = 'phone';
} else {
$viewport = 'desktop';
}
$data = (Object)[
'os' => $osFamily,
'browser' => $browserFamily,
'device' => $dd->getDeviceName(),
'is_bot' => $dd->isBot(),
'is_safari' => 'Safari' === $browserFamily,
'is_chrome' => 'Chrome' === $browserFamily,
'is_mobile' => 'phone' === $viewport,
'is_mac' => 'Mac' === $osFamily,
'is_windows' => 'Windows' === $osFamily,
];
if ($data->is_bot) {
$data->bot = $dd->getBot()['name'] ?? null;
}
return $data;
});
return $data;
}
}