Renamed argument to have a more clear intention

This commit is contained in:
Alejandro Celaya 2021-04-07 11:48:01 +02:00
parent b4d137375a
commit 77d06b4b03
3 changed files with 38 additions and 37 deletions

View file

@ -32,13 +32,13 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
/**
* @throws GeolocationDbUpdateFailedException
*/
public function checkDbUpdate(?callable $mustBeUpdated = null, ?callable $handleProgress = null): void
public function checkDbUpdate(?callable $beforeDownload = null, ?callable $handleProgress = null): void
{
$lock = $this->locker->createLock(self::LOCK_NAME);
$lock->acquire(true); // Block until lock is released
try {
$this->downloadIfNeeded($mustBeUpdated, $handleProgress);
$this->downloadIfNeeded($beforeDownload, $handleProgress);
} finally {
$lock->release();
}
@ -47,26 +47,26 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
/**
* @throws GeolocationDbUpdateFailedException
*/
private function downloadIfNeeded(?callable $mustBeUpdated, ?callable $handleProgress): void
private function downloadIfNeeded(?callable $beforeDownload, ?callable $handleProgress): void
{
if (! $this->dbUpdater->databaseFileExists()) {
$this->downloadNewDb(false, $mustBeUpdated, $handleProgress);
$this->downloadNewDb(false, $beforeDownload, $handleProgress);
return;
}
$meta = $this->geoLiteDbReader->metadata();
if ($this->buildIsTooOld($meta)) {
$this->downloadNewDb(true, $mustBeUpdated, $handleProgress);
$this->downloadNewDb(true, $beforeDownload, $handleProgress);
}
}
/**
* @throws GeolocationDbUpdateFailedException
*/
private function downloadNewDb(bool $olderDbExists, ?callable $mustBeUpdated, ?callable $handleProgress): void
private function downloadNewDb(bool $olderDbExists, ?callable $beforeDownload, ?callable $handleProgress): void
{
if ($mustBeUpdated !== null) {
$mustBeUpdated($olderDbExists);
if ($beforeDownload !== null) {
$beforeDownload($olderDbExists);
}
try {

View file

@ -11,5 +11,5 @@ interface GeolocationDbUpdaterInterface
/**
* @throws GeolocationDbUpdateFailedException
*/
public function checkDbUpdate(?callable $mustBeUpdated = null, ?callable $handleProgress = null): void;
public function checkDbUpdate(?callable $beforeDownload = null, ?callable $handleProgress = null): void;
}

View file

@ -7,31 +7,28 @@ namespace Shlinkio\Shlink\Core\EventDispatcher;
use Doctrine\ORM\EntityManagerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Entity\VisitLocation;
use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited;
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdaterInterface;
use Shlinkio\Shlink\IpGeolocation\Model\Location;
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
use function sprintf;
class LocateVisit
{
private IpLocationResolverInterface $ipLocationResolver;
private EntityManagerInterface $em;
private LoggerInterface $logger;
private GeolocationDbUpdaterInterface $dbUpdater;
private DbUpdaterInterface $dbUpdater;
private EventDispatcherInterface $eventDispatcher;
public function __construct(
IpLocationResolverInterface $ipLocationResolver,
EntityManagerInterface $em,
LoggerInterface $logger,
GeolocationDbUpdaterInterface $dbUpdater,
DbUpdaterInterface $dbUpdater,
EventDispatcherInterface $eventDispatcher
) {
$this->ipLocationResolver = $ipLocationResolver;
@ -54,33 +51,37 @@ class LocateVisit
return;
}
if ($this->downloadOrUpdateGeoLiteDb($visitId)) {
$this->locateVisit($visitId, $shortUrlVisited->originalIpAddress(), $visit);
if (! $this->dbUpdater->databaseFileExists()) {
$this->logger->warning('Tried to locate visit with id "{visitId}", but a GeoLite2 db was not found.', [
'visitId' => $visitId,
]);
return;
}
$this->locateVisit($visitId, $shortUrlVisited->originalIpAddress(), $visit);
$this->eventDispatcher->dispatch(new VisitLocated($visitId));
}
private function downloadOrUpdateGeoLiteDb(string $visitId): bool
{
try {
$this->dbUpdater->checkDbUpdate(function (bool $olderDbExists): void {
$this->logger->notice(sprintf('%s GeoLite2 database...', $olderDbExists ? 'Updating' : 'Downloading'));
});
} catch (GeolocationDbUpdateFailedException $e) {
if (! $e->olderDbExists()) {
$this->logger->error(
'GeoLite2 database download failed. It is not possible to locate visit with id {visitId}. {e}',
['e' => $e, 'visitId' => $visitId],
);
return false;
}
$this->logger->warning('GeoLite2 database update failed. Proceeding with old version. {e}', ['e' => $e]);
}
return true;
}
// private function downloadOrUpdateGeoLiteDb(string $visitId): bool
// {
// try {
// $this->dbUpdater->checkDbUpdate(function (bool $olderDbExists): void {
// $this->logger->notice(sprintf('%s GeoLite2 database...', $olderDbExists ? 'Updating' : 'Downloading'));
// });
// } catch (GeolocationDbUpdateFailedException $e) {
// if (! $e->olderDbExists()) {
// $this->logger->error(
// 'GeoLite2 database download failed. It is not possible to locate visit with id {visitId}. {e}',
// ['e' => $e, 'visitId' => $visitId],
// );
// return false;
// }
//
// $this->logger->warning('GeoLite2 database update failed. Proceeding with old version. {e}', ['e' => $e]);
// }
//
// return true;
// }
private function locateVisit(string $visitId, ?string $originalIpAddress, Visit $visit): void
{