Modernize VisitLocation entity

This commit is contained in:
Alejandro Celaya 2024-03-18 19:11:42 +01:00
parent 60e9443b12
commit b2dee43bb0
5 changed files with 45 additions and 80 deletions

View file

@ -55,8 +55,8 @@ abstract class AbstractVisitsListCommand extends Command
$rowData = [
...$visit->jsonSerialize(),
'country' => $visit->getVisitLocation()?->getCountryName() ?? 'Unknown',
'city' => $visit->getVisitLocation()?->getCityName() ?? 'Unknown',
'country' => $visit->getVisitLocation()?->countryName ?? 'Unknown',
'city' => $visit->getVisitLocation()?->cityName ?? 'Unknown',
...$extraFields,
];

View file

@ -154,8 +154,8 @@ class LocateVisitsCommand extends AbstractLockedCommand implements VisitGeolocat
public function onVisitLocated(VisitLocation $visitLocation, Visit $visit): void
{
if (! $visitLocation->isEmpty()) {
$this->io->writeln(sprintf(' [<info>Address located in "%s"</info>]', $visitLocation->getCountryName()));
if (! $visitLocation->isEmpty) {
$this->io->writeln(sprintf(' [<info>Address located in "%s"</info>]', $visitLocation->countryName));
} elseif ($visit->hasRemoteAddr() && $visit->getRemoteAddr() !== IpAddress::LOCALHOST) {
$this->io->writeln(' <comment>[Could not locate address]</comment>');
}

View file

@ -53,10 +53,10 @@ class SendVisitToMatomo
$location = $visit->getVisitLocation();
if ($location !== null) {
$tracker
->setCity($location->getCityName())
->setCountry($location->getCountryName())
->setLatitude($location->getLatitude())
->setLongitude($location->getLongitude());
->setCity($location->cityName)
->setCountry($location->countryName)
->setLatitude($location->latitude)
->setLongitude($location->longitude);
}
// Set not obfuscated IP if possible, as matomo handles obfuscation itself

View file

@ -11,89 +11,54 @@ use Shlinkio\Shlink\IpGeolocation\Model\Location;
class VisitLocation extends AbstractEntity implements JsonSerializable
{
private string $countryCode;
private string $countryName;
private string $regionName;
private string $cityName;
private float $latitude;
private float $longitude;
private string $timezone;
private bool $isEmpty;
public readonly bool $isEmpty;
private function __construct()
{
private function __construct(
public readonly string $countryCode,
public readonly string $countryName,
public readonly string $regionName,
public readonly string $cityName,
public readonly float $latitude,
public readonly float $longitude,
public readonly string $timezone,
) {
$this->isEmpty = (
$countryCode === '' &&
$countryName === '' &&
$regionName === '' &&
$cityName === '' &&
$latitude === 0.0 &&
$longitude === 0.0 &&
$timezone === ''
);
}
public static function fromGeolocation(Location $location): self
{
$instance = new self();
$instance->countryCode = $location->countryCode;
$instance->countryName = $location->countryName;
$instance->regionName = $location->regionName;
$instance->cityName = $location->city;
$instance->latitude = $location->latitude;
$instance->longitude = $location->longitude;
$instance->timezone = $location->timeZone;
$instance->computeIsEmpty();
return $instance;
return new self(
countryCode: $location->countryCode,
countryName: $location->countryName,
regionName: $location->regionName,
cityName: $location->city,
latitude: $location->latitude,
longitude: $location->longitude,
timezone: $location->timeZone,
);
}
public static function fromImport(ImportedShlinkVisitLocation $location): self
{
$instance = new self();
$instance->countryCode = $location->countryCode;
$instance->countryName = $location->countryName;
$instance->regionName = $location->regionName;
$instance->cityName = $location->cityName;
$instance->latitude = $location->latitude;
$instance->longitude = $location->longitude;
$instance->timezone = $location->timezone;
$instance->computeIsEmpty();
return $instance;
}
private function computeIsEmpty(): void
{
$this->isEmpty = (
$this->countryCode === '' &&
$this->countryName === '' &&
$this->regionName === '' &&
$this->cityName === '' &&
$this->latitude === 0.0 &&
$this->longitude === 0.0 &&
$this->timezone === ''
return new self(
countryCode: $location->countryCode,
countryName: $location->countryName,
regionName: $location->regionName,
cityName: $location->cityName,
latitude: $location->latitude,
longitude: $location->longitude,
timezone: $location->timezone,
);
}
public function getCountryName(): string
{
return $this->countryName;
}
public function getLatitude(): float
{
return $this->latitude;
}
public function getLongitude(): float
{
return $this->longitude;
}
public function getCityName(): string
{
return $this->cityName;
}
public function isEmpty(): bool
{
return $this->isEmpty;
}
public function jsonSerialize(): array
{
return [

View file

@ -18,7 +18,7 @@ class VisitLocationTest extends TestCase
$payload = new Location(...$args);
$location = VisitLocation::fromGeolocation($payload);
self::assertEquals($isEmpty, $location->isEmpty());
self::assertEquals($isEmpty, $location->isEmpty);
}
public static function provideArgs(): iterable