mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-18 09:02:04 +03:00
Registered GeolocationDbUpdater service and added callable which is invoked when db is going to be updated
This commit is contained in:
parent
b24511b7b5
commit
0f48dd567f
4 changed files with 23 additions and 7 deletions
|
@ -3,6 +3,8 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Shlinkio\Shlink\CLI;
|
namespace Shlinkio\Shlink\CLI;
|
||||||
|
|
||||||
|
use GeoIp2\Database\Reader;
|
||||||
|
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater;
|
||||||
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdater;
|
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdater;
|
||||||
use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface;
|
use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface;
|
||||||
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
|
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
|
||||||
|
@ -19,6 +21,8 @@ return [
|
||||||
'factories' => [
|
'factories' => [
|
||||||
Application::class => Factory\ApplicationFactory::class,
|
Application::class => Factory\ApplicationFactory::class,
|
||||||
|
|
||||||
|
GeolocationDbUpdater::class => ConfigAbstractFactory::class,
|
||||||
|
|
||||||
Command\ShortUrl\GenerateShortUrlCommand::class => ConfigAbstractFactory::class,
|
Command\ShortUrl\GenerateShortUrlCommand::class => ConfigAbstractFactory::class,
|
||||||
Command\ShortUrl\ResolveUrlCommand::class => ConfigAbstractFactory::class,
|
Command\ShortUrl\ResolveUrlCommand::class => ConfigAbstractFactory::class,
|
||||||
Command\ShortUrl\ListShortUrlsCommand::class => ConfigAbstractFactory::class,
|
Command\ShortUrl\ListShortUrlsCommand::class => ConfigAbstractFactory::class,
|
||||||
|
@ -44,6 +48,8 @@ return [
|
||||||
],
|
],
|
||||||
|
|
||||||
ConfigAbstractFactory::class => [
|
ConfigAbstractFactory::class => [
|
||||||
|
GeolocationDbUpdater::class => [DbUpdater::class, Reader::class],
|
||||||
|
|
||||||
Command\ShortUrl\GenerateShortUrlCommand::class => [Service\UrlShortener::class, 'config.url_shortener.domain'],
|
Command\ShortUrl\GenerateShortUrlCommand::class => [Service\UrlShortener::class, 'config.url_shortener.domain'],
|
||||||
Command\ShortUrl\ResolveUrlCommand::class => [Service\UrlShortener::class],
|
Command\ShortUrl\ResolveUrlCommand::class => [Service\UrlShortener::class],
|
||||||
Command\ShortUrl\ListShortUrlsCommand::class => [Service\ShortUrlService::class, 'config.url_shortener.domain'],
|
Command\ShortUrl\ListShortUrlsCommand::class => [Service\ShortUrlService::class, 'config.url_shortener.domain'],
|
||||||
|
|
|
@ -26,16 +26,16 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
|
||||||
/**
|
/**
|
||||||
* @throws GeolocationDbUpdateFailedException
|
* @throws GeolocationDbUpdateFailedException
|
||||||
*/
|
*/
|
||||||
public function checkDbUpdate(callable $handleProgress = null): void
|
public function checkDbUpdate(callable $mustBeUpdated = null, callable $handleProgress = null): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$meta = $this->geoLiteDbReader->metadata();
|
$meta = $this->geoLiteDbReader->metadata();
|
||||||
if ($this->buildIsOlderThanOneWeek($meta->__get('buildEpoch'))) {
|
if ($this->buildIsOlderThanOneWeek($meta->__get('buildEpoch'))) {
|
||||||
$this->downloadNewDb(true, $handleProgress);
|
$this->downloadNewDb(true, $mustBeUpdated, $handleProgress);
|
||||||
}
|
}
|
||||||
} catch (InvalidArgumentException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
// This is the exception thrown by the reader when the database file does not exist
|
// This is the exception thrown by the reader when the database file does not exist
|
||||||
$this->downloadNewDb(false, $handleProgress);
|
$this->downloadNewDb(false, $mustBeUpdated, $handleProgress);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,8 +49,15 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
|
||||||
/**
|
/**
|
||||||
* @throws GeolocationDbUpdateFailedException
|
* @throws GeolocationDbUpdateFailedException
|
||||||
*/
|
*/
|
||||||
private function downloadNewDb(bool $olderDbExists, callable $handleProgress = null): void
|
private function downloadNewDb(
|
||||||
{
|
bool $olderDbExists,
|
||||||
|
callable $mustBeUpdated = null,
|
||||||
|
callable $handleProgress = null
|
||||||
|
): void {
|
||||||
|
if ($mustBeUpdated !== null) {
|
||||||
|
$mustBeUpdated();
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->dbUpdater->downloadFreshCopy($handleProgress);
|
$this->dbUpdater->downloadFreshCopy($handleProgress);
|
||||||
} catch (RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
|
|
|
@ -10,5 +10,5 @@ interface GeolocationDbUpdaterInterface
|
||||||
/**
|
/**
|
||||||
* @throws GeolocationDbUpdateFailedException
|
* @throws GeolocationDbUpdateFailedException
|
||||||
*/
|
*/
|
||||||
public function checkDbUpdate(callable $handleProgress = null): void;
|
public function checkDbUpdate(callable $mustBeUpdated = null, callable $handleProgress = null): void;
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,12 +41,15 @@ class GeolocationDbUpdaterTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function exceptionIsThrownWhenOlderDbDoesNotExistAndDownloadFails(): void
|
public function exceptionIsThrownWhenOlderDbDoesNotExistAndDownloadFails(): void
|
||||||
{
|
{
|
||||||
|
$mustBeUpdated = function () {
|
||||||
|
$this->assertTrue(true);
|
||||||
|
};
|
||||||
$getMeta = $this->geoLiteDbReader->metadata()->willThrow(InvalidArgumentException::class);
|
$getMeta = $this->geoLiteDbReader->metadata()->willThrow(InvalidArgumentException::class);
|
||||||
$prev = new RuntimeException('');
|
$prev = new RuntimeException('');
|
||||||
$download = $this->dbUpdater->downloadFreshCopy(null)->willThrow($prev);
|
$download = $this->dbUpdater->downloadFreshCopy(null)->willThrow($prev);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->geolocationDbUpdater->checkDbUpdate();
|
$this->geolocationDbUpdater->checkDbUpdate($mustBeUpdated);
|
||||||
$this->assertTrue(false); // If this is reached, the test will fail
|
$this->assertTrue(false); // If this is reached, the test will fail
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
/** @var GeolocationDbUpdateFailedException $e */
|
/** @var GeolocationDbUpdateFailedException $e */
|
||||||
|
|
Loading…
Reference in a new issue