mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
41 lines
1.2 KiB
PHP
41 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Shlinkio\Shlink\Core\Visit\Geolocation;
|
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress;
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
|
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
|
|
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
|
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
|
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
|
|
|
|
class VisitToLocationHelper implements VisitToLocationHelperInterface
|
|
{
|
|
public function __construct(private readonly IpLocationResolverInterface $ipLocationResolver)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @throws IpCannotBeLocatedException
|
|
*/
|
|
public function resolveVisitLocation(Visit $visit): Location
|
|
{
|
|
if (! $visit->hasRemoteAddr()) {
|
|
throw IpCannotBeLocatedException::forEmptyAddress();
|
|
}
|
|
|
|
$ipAddr = $visit->getRemoteAddr() ?? '';
|
|
if ($ipAddr === IpAddress::LOCALHOST) {
|
|
throw IpCannotBeLocatedException::forLocalhost();
|
|
}
|
|
|
|
try {
|
|
return $this->ipLocationResolver->resolveIpLocation($ipAddr);
|
|
} catch (WrongIpException $e) {
|
|
throw IpCannotBeLocatedException::forError($e);
|
|
}
|
|
}
|
|
}
|