mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 15:23:12 +08:00
Defined stricter model to represent one geo location
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user