mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 19:59:04 +03:00
Modernize VisitLocation entity
This commit is contained in:
parent
60e9443b12
commit
b2dee43bb0
5 changed files with 45 additions and 80 deletions
|
@ -55,8 +55,8 @@ abstract class AbstractVisitsListCommand extends Command
|
||||||
|
|
||||||
$rowData = [
|
$rowData = [
|
||||||
...$visit->jsonSerialize(),
|
...$visit->jsonSerialize(),
|
||||||
'country' => $visit->getVisitLocation()?->getCountryName() ?? 'Unknown',
|
'country' => $visit->getVisitLocation()?->countryName ?? 'Unknown',
|
||||||
'city' => $visit->getVisitLocation()?->getCityName() ?? 'Unknown',
|
'city' => $visit->getVisitLocation()?->cityName ?? 'Unknown',
|
||||||
...$extraFields,
|
...$extraFields,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -154,8 +154,8 @@ class LocateVisitsCommand extends AbstractLockedCommand implements VisitGeolocat
|
||||||
|
|
||||||
public function onVisitLocated(VisitLocation $visitLocation, Visit $visit): void
|
public function onVisitLocated(VisitLocation $visitLocation, Visit $visit): void
|
||||||
{
|
{
|
||||||
if (! $visitLocation->isEmpty()) {
|
if (! $visitLocation->isEmpty) {
|
||||||
$this->io->writeln(sprintf(' [<info>Address located in "%s"</info>]', $visitLocation->getCountryName()));
|
$this->io->writeln(sprintf(' [<info>Address located in "%s"</info>]', $visitLocation->countryName));
|
||||||
} elseif ($visit->hasRemoteAddr() && $visit->getRemoteAddr() !== IpAddress::LOCALHOST) {
|
} elseif ($visit->hasRemoteAddr() && $visit->getRemoteAddr() !== IpAddress::LOCALHOST) {
|
||||||
$this->io->writeln(' <comment>[Could not locate address]</comment>');
|
$this->io->writeln(' <comment>[Could not locate address]</comment>');
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,10 +53,10 @@ class SendVisitToMatomo
|
||||||
$location = $visit->getVisitLocation();
|
$location = $visit->getVisitLocation();
|
||||||
if ($location !== null) {
|
if ($location !== null) {
|
||||||
$tracker
|
$tracker
|
||||||
->setCity($location->getCityName())
|
->setCity($location->cityName)
|
||||||
->setCountry($location->getCountryName())
|
->setCountry($location->countryName)
|
||||||
->setLatitude($location->getLatitude())
|
->setLatitude($location->latitude)
|
||||||
->setLongitude($location->getLongitude());
|
->setLongitude($location->longitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set not obfuscated IP if possible, as matomo handles obfuscation itself
|
// Set not obfuscated IP if possible, as matomo handles obfuscation itself
|
||||||
|
|
|
@ -11,89 +11,54 @@ use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
||||||
|
|
||||||
class VisitLocation extends AbstractEntity implements JsonSerializable
|
class VisitLocation extends AbstractEntity implements JsonSerializable
|
||||||
{
|
{
|
||||||
private string $countryCode;
|
public readonly bool $isEmpty;
|
||||||
private string $countryName;
|
|
||||||
private string $regionName;
|
|
||||||
private string $cityName;
|
|
||||||
private float $latitude;
|
|
||||||
private float $longitude;
|
|
||||||
private string $timezone;
|
|
||||||
private 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
|
public static function fromGeolocation(Location $location): self
|
||||||
{
|
{
|
||||||
$instance = new self();
|
return new self(
|
||||||
|
countryCode: $location->countryCode,
|
||||||
$instance->countryCode = $location->countryCode;
|
countryName: $location->countryName,
|
||||||
$instance->countryName = $location->countryName;
|
regionName: $location->regionName,
|
||||||
$instance->regionName = $location->regionName;
|
cityName: $location->city,
|
||||||
$instance->cityName = $location->city;
|
latitude: $location->latitude,
|
||||||
$instance->latitude = $location->latitude;
|
longitude: $location->longitude,
|
||||||
$instance->longitude = $location->longitude;
|
timezone: $location->timeZone,
|
||||||
$instance->timezone = $location->timeZone;
|
);
|
||||||
$instance->computeIsEmpty();
|
|
||||||
|
|
||||||
return $instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromImport(ImportedShlinkVisitLocation $location): self
|
public static function fromImport(ImportedShlinkVisitLocation $location): self
|
||||||
{
|
{
|
||||||
$instance = new self();
|
return new self(
|
||||||
|
countryCode: $location->countryCode,
|
||||||
$instance->countryCode = $location->countryCode;
|
countryName: $location->countryName,
|
||||||
$instance->countryName = $location->countryName;
|
regionName: $location->regionName,
|
||||||
$instance->regionName = $location->regionName;
|
cityName: $location->cityName,
|
||||||
$instance->cityName = $location->cityName;
|
latitude: $location->latitude,
|
||||||
$instance->latitude = $location->latitude;
|
longitude: $location->longitude,
|
||||||
$instance->longitude = $location->longitude;
|
timezone: $location->timezone,
|
||||||
$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 === ''
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
public function jsonSerialize(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
|
|
@ -18,7 +18,7 @@ class VisitLocationTest extends TestCase
|
||||||
$payload = new Location(...$args);
|
$payload = new Location(...$args);
|
||||||
$location = VisitLocation::fromGeolocation($payload);
|
$location = VisitLocation::fromGeolocation($payload);
|
||||||
|
|
||||||
self::assertEquals($isEmpty, $location->isEmpty());
|
self::assertEquals($isEmpty, $location->isEmpty);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function provideArgs(): iterable
|
public static function provideArgs(): iterable
|
||||||
|
|
Loading…
Add table
Reference in a new issue