Created enum to determine what was the result of updating a geolite DB

This commit is contained in:
Alejandro Celaya
2022-09-18 10:31:14 +02:00
parent 59bcd62717
commit eab9347522
6 changed files with 41 additions and 14 deletions

View File

@@ -30,17 +30,17 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
/**
* @throws GeolocationDbUpdateFailedException
*/
public function checkDbUpdate(?callable $beforeDownload = null, ?callable $handleProgress = null): void
public function checkDbUpdate(?callable $beforeDownload = null, ?callable $handleProgress = null): GeolocationResult
{
if ($this->trackingOptions->disableTracking || $this->trackingOptions->disableIpTracking) {
return;
return GeolocationResult::CHECK_SKIPPED;
}
$lock = $this->locker->createLock(self::LOCK_NAME);
$lock->acquire(true); // Block until lock is released
try {
$this->downloadIfNeeded($beforeDownload, $handleProgress);
return $this->downloadIfNeeded($beforeDownload, $handleProgress);
} finally {
$lock->release();
}
@@ -49,17 +49,20 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
/**
* @throws GeolocationDbUpdateFailedException
*/
private function downloadIfNeeded(?callable $beforeDownload, ?callable $handleProgress): void
private function downloadIfNeeded(?callable $beforeDownload, ?callable $handleProgress): GeolocationResult
{
if (! $this->dbUpdater->databaseFileExists()) {
$this->downloadNewDb(false, $beforeDownload, $handleProgress);
return;
return GeolocationResult::DB_CREATED;
}
$meta = $this->geoLiteDbReader->metadata();
if ($this->buildIsTooOld($meta)) {
$this->downloadNewDb(true, $beforeDownload, $handleProgress);
return GeolocationResult::DB_UPDATED;
}
return GeolocationResult::DB_IS_UP_TO_DATE;
}
private function buildIsTooOld(Metadata $meta): bool

View File

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

View File

@@ -0,0 +1,11 @@
<?php
namespace Shlinkio\Shlink\CLI\GeoLite;
enum GeolocationResult
{
case CHECK_SKIPPED;
case DB_CREATED;
case DB_UPDATED;
case DB_IS_UP_TO_DATE;
}

View File

@@ -10,6 +10,7 @@ use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\Visit\DownloadGeoLiteDbCommand;
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
use Shlinkio\Shlink\CLI\GeoLite\GeolocationDbUpdaterInterface;
use Shlinkio\Shlink\CLI\GeoLite\GeolocationResult;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
use Symfony\Component\Console\Tester\CommandTester;
@@ -97,11 +98,12 @@ class DownloadGeoLiteDbCommandTest extends TestCase
public function provideSuccessParams(): iterable
{
yield 'up to date db' => [function (): void {
}, '[INFO] GeoLite2 db file is up to date.'];
yield 'outdated db' => [function (array $args): void {
yield 'up to date db' => [fn () => GeolocationResult::CHECK_SKIPPED, '[INFO] GeoLite2 db file is up to date.'];
yield 'outdated db' => [function (array $args): GeolocationResult {
[$beforeDownload] = $args;
$beforeDownload(true);
return GeolocationResult::DB_CREATED;
}, '[OK] GeoLite2 db file properly downloaded.'];
}
}

View File

@@ -13,6 +13,7 @@ use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
use Shlinkio\Shlink\CLI\GeoLite\GeolocationDbUpdater;
use Shlinkio\Shlink\CLI\GeoLite\GeolocationResult;
use Shlinkio\Shlink\Core\Options\TrackingOptions;
use Shlinkio\Shlink\IpGeolocation\Exception\RuntimeException;
use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdaterInterface;
@@ -110,15 +111,16 @@ class GeolocationDbUpdaterTest extends TestCase
* @test
* @dataProvider provideSmallDays
*/
public function databaseIsNotUpdatedIfItIsYoungerThanOneWeek(string|int $buildEpoch): void
public function databaseIsNotUpdatedIfItIsNewEnough(string|int $buildEpoch): void
{
$fileExists = $this->dbUpdater->databaseFileExists()->willReturn(true);
$getMeta = $this->geoLiteDbReader->metadata()->willReturn($this->buildMetaWithBuildEpoch($buildEpoch));
$download = $this->dbUpdater->downloadFreshCopy(null)->will(function (): void {
});
$this->geolocationDbUpdater()->checkDbUpdate();
$result = $this->geolocationDbUpdater()->checkDbUpdate();
self::assertEquals(GeolocationResult::DB_IS_UP_TO_DATE, $result);
$fileExists->shouldHaveBeenCalledOnce();
$getMeta->shouldHaveBeenCalledOnce();
$download->shouldNotHaveBeenCalled();
@@ -174,8 +176,9 @@ class GeolocationDbUpdaterTest extends TestCase
*/
public function downloadDbIsSkippedIfTrackingIsDisabled(TrackingOptions $options): void
{
$this->geolocationDbUpdater($options)->checkDbUpdate();
$result = $this->geolocationDbUpdater($options)->checkDbUpdate();
self::assertEquals(GeolocationResult::CHECK_SKIPPED, $result);
$this->dbUpdater->databaseFileExists(Argument::cetera())->shouldNotHaveBeenCalled();
$this->geoLiteDbReader->metadata(Argument::cetera())->shouldNotHaveBeenCalled();
}