Migrate from mobiledetectlib to phpuseragentparser

This commit is contained in:
Alejandro Celaya 2024-11-28 12:05:10 +01:00
parent d121d4d496
commit 6331fa3ed3
2 changed files with 9 additions and 12 deletions

View File

@ -23,6 +23,7 @@
"doctrine/dbal": "^4.2", "doctrine/dbal": "^4.2",
"doctrine/migrations": "^3.8", "doctrine/migrations": "^3.8",
"doctrine/orm": "^3.3", "doctrine/orm": "^3.3",
"donatj/phpuseragentparser": "^1.10",
"endroid/qr-code": "^6.0", "endroid/qr-code": "^6.0",
"friendsofphp/proxy-manager-lts": "^1.0", "friendsofphp/proxy-manager-lts": "^1.0",
"geoip2/geoip2": "^3.0", "geoip2/geoip2": "^3.0",
@ -39,7 +40,6 @@
"mezzio/mezzio-fastroute": "^3.12", "mezzio/mezzio-fastroute": "^3.12",
"mezzio/mezzio-problem-details": "^1.15", "mezzio/mezzio-problem-details": "^1.15",
"mlocati/ip-lib": "^1.18.1", "mlocati/ip-lib": "^1.18.1",
"mobiledetect/mobiledetectlib": "4.8.x-dev#920c549 as 4.9",
"pagerfanta/core": "^3.8", "pagerfanta/core": "^3.8",
"ramsey/uuid": "^4.7", "ramsey/uuid": "^4.7",
"shlinkio/doctrine-specification": "^2.1.1", "shlinkio/doctrine-specification": "^2.1.1",

View File

@ -2,7 +2,8 @@
namespace Shlinkio\Shlink\Core\Model; namespace Shlinkio\Shlink\Core\Model;
use Detection\MobileDetect; use donatj\UserAgent\Platforms;
use donatj\UserAgent\UserAgentParser;
enum DeviceType: string enum DeviceType: string
{ {
@ -12,17 +13,13 @@ enum DeviceType: string
public static function matchFromUserAgent(string $userAgent): self|null public static function matchFromUserAgent(string $userAgent): self|null
{ {
$detect = new MobileDetect(); static $uaParser = new UserAgentParser();
$detect->setUserAgent($userAgent); $ua = $uaParser->parse($userAgent);
return match (true) { return match ($ua->platform()) {
// $detect->is('iOS') && $detect->isTablet() => self::IOS, // TODO To detect iPad only Platforms::IPHONE, Platforms::IPAD => self::IOS, // Detects both iPhone and iPad (except iPadOS 13+)
// $detect->is('iOS') && ! $detect->isTablet() => self::IOS, // TODO To detect iPhone only Platforms::ANDROID => self::ANDROID, // Detects both android phones and android tablets
// $detect->is('androidOS') && $detect->isTablet() => self::ANDROID, // TODO To detect Android tablets Platforms::LINUX, Platforms::WINDOWS, Platforms::MACINTOSH, Platforms::CHROME_OS => self::DESKTOP,
// $detect->is('androidOS') && ! $detect->isTablet() => self::ANDROID, // TODO To detect Android phones
$detect->is('iOS') => self::IOS, // Detects both iPhone and iPad
$detect->is('androidOS') => self::ANDROID, // Detects both android phones and android tablets
! $detect->isMobile() && ! $detect->isTablet() => self::DESKTOP,
default => null, default => null,
}; };
} }