mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 09:13:11 +08:00
Defined stricter model to represent one geo location
This commit is contained in:
@@ -4,8 +4,8 @@ declare(strict_types=1);
|
||||
namespace Shlinkio\Shlink\Core\Entity;
|
||||
|
||||
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
|
||||
use Shlinkio\Shlink\Common\IpGeolocation\Model\Location;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
|
||||
use function array_key_exists;
|
||||
|
||||
class VisitLocation extends AbstractEntity implements VisitLocationInterface
|
||||
{
|
||||
@@ -24,9 +24,9 @@ class VisitLocation extends AbstractEntity implements VisitLocationInterface
|
||||
/** @var string */
|
||||
private $timezone;
|
||||
|
||||
public function __construct(array $locationInfo)
|
||||
public function __construct(Location $location)
|
||||
{
|
||||
$this->exchangeArray($locationInfo);
|
||||
$this->exchangeLocationInfo($location);
|
||||
}
|
||||
|
||||
public function getCountryName(): string
|
||||
@@ -49,32 +49,15 @@ class VisitLocation extends AbstractEntity implements VisitLocationInterface
|
||||
return $this->cityName ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Exchange internal values from provided array
|
||||
*/
|
||||
private function exchangeArray(array $array): void
|
||||
private function exchangeLocationInfo(Location $info): void
|
||||
{
|
||||
if (array_key_exists('country_code', $array)) {
|
||||
$this->countryCode = (string) $array['country_code'];
|
||||
}
|
||||
if (array_key_exists('country_name', $array)) {
|
||||
$this->countryName = (string) $array['country_name'];
|
||||
}
|
||||
if (array_key_exists('region_name', $array)) {
|
||||
$this->regionName = (string) $array['region_name'];
|
||||
}
|
||||
if (array_key_exists('city', $array)) {
|
||||
$this->cityName = (string) $array['city'];
|
||||
}
|
||||
if (array_key_exists('latitude', $array)) {
|
||||
$this->latitude = (string) $array['latitude'];
|
||||
}
|
||||
if (array_key_exists('longitude', $array)) {
|
||||
$this->longitude = (string) $array['longitude'];
|
||||
}
|
||||
if (array_key_exists('time_zone', $array)) {
|
||||
$this->timezone = (string) $array['time_zone'];
|
||||
}
|
||||
$this->countryCode = $info->countryCode();
|
||||
$this->countryName = $info->countryName();
|
||||
$this->regionName = $info->regionName();
|
||||
$this->cityName = $info->city();
|
||||
$this->latitude = (string) $info->latitude();
|
||||
$this->longitude = (string) $info->longitude();
|
||||
$this->timezone = $info->timeZone();
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
namespace Shlinkio\Shlink\Core\Service;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Shlinkio\Shlink\Common\IpGeolocation\Model\Location;
|
||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||
use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
||||
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
|
||||
@@ -19,7 +20,7 @@ class VisitService implements VisitServiceInterface
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
public function locateVisits(callable $getGeolocationData, ?callable $locatedVisit = null): void
|
||||
public function locateVisits(callable $geolocateVisit, ?callable $notifyVisitWithLocation = null): void
|
||||
{
|
||||
/** @var VisitRepository $repo */
|
||||
$repo = $this->em->getRepository(Visit::class);
|
||||
@@ -27,26 +28,27 @@ class VisitService implements VisitServiceInterface
|
||||
|
||||
foreach ($results as [$visit]) {
|
||||
try {
|
||||
$locationData = $getGeolocationData($visit);
|
||||
/** @var Location $location */
|
||||
$location = $geolocateVisit($visit);
|
||||
} catch (IpCannotBeLocatedException $e) {
|
||||
// Skip if the visit's IP could not be located
|
||||
continue;
|
||||
}
|
||||
|
||||
$location = new VisitLocation($locationData);
|
||||
$this->locateVisit($visit, $location, $locatedVisit);
|
||||
$location = new VisitLocation($location);
|
||||
$this->locateVisit($visit, $location, $notifyVisitWithLocation);
|
||||
}
|
||||
}
|
||||
|
||||
private function locateVisit(Visit $visit, VisitLocation $location, ?callable $locatedVisit): void
|
||||
private function locateVisit(Visit $visit, VisitLocation $location, ?callable $notifyVisitWithLocation): void
|
||||
{
|
||||
$visit->locate($location);
|
||||
|
||||
$this->em->persist($visit);
|
||||
$this->em->flush();
|
||||
|
||||
if ($locatedVisit !== null) {
|
||||
$locatedVisit($location, $visit);
|
||||
if ($notifyVisitWithLocation !== null) {
|
||||
$notifyVisitWithLocation($location, $visit);
|
||||
}
|
||||
|
||||
$this->em->clear();
|
||||
|
||||
@@ -5,5 +5,5 @@ namespace Shlinkio\Shlink\Core\Service;
|
||||
|
||||
interface VisitServiceInterface
|
||||
{
|
||||
public function locateVisits(callable $getGeolocationData, ?callable $locatedVisit = null): void;
|
||||
public function locateVisits(callable $geolocateVisit, ?callable $notifyVisitWithLocation = null): void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user