mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Add more strict parameter for GeolocationDbUpdater
This commit is contained in:
@@ -8,6 +8,7 @@ use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Shlinkio\Shlink\Core\EventDispatcher\Event\GeoLiteDbCreated;
|
||||
use Shlinkio\Shlink\Core\Geolocation\GeolocationDbUpdaterInterface;
|
||||
use Shlinkio\Shlink\Core\Geolocation\GeolocationDownloadProgressHandlerInterface;
|
||||
use Shlinkio\Shlink\Core\Geolocation\GeolocationResult;
|
||||
use Throwable;
|
||||
|
||||
@@ -24,21 +25,35 @@ readonly class UpdateGeoLiteDb
|
||||
|
||||
public function __invoke(): void
|
||||
{
|
||||
$beforeDownload = fn (bool $olderDbExists) => $this->logger->notice(
|
||||
sprintf('%s GeoLite2 db file...', $olderDbExists ? 'Updating' : 'Downloading'),
|
||||
);
|
||||
$messageLogged = false;
|
||||
$handleProgress = function (int $total, int $downloaded, bool $olderDbExists) use (&$messageLogged): void {
|
||||
if ($messageLogged || $total > $downloaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
$messageLogged = true;
|
||||
$this->logger->notice(sprintf('Finished %s GeoLite2 db file', $olderDbExists ? 'updating' : 'downloading'));
|
||||
};
|
||||
|
||||
try {
|
||||
$result = $this->dbUpdater->checkDbUpdate($beforeDownload, $handleProgress);
|
||||
$result = $this->dbUpdater->checkDbUpdate(
|
||||
new class ($this->logger) implements GeolocationDownloadProgressHandlerInterface {
|
||||
public function __construct(
|
||||
private readonly LoggerInterface $logger,
|
||||
private bool $messageLogged = false,
|
||||
) {
|
||||
}
|
||||
|
||||
public function beforeDownload(bool $olderDbExists): void
|
||||
{
|
||||
$this->logger->notice(
|
||||
sprintf('%s GeoLite2 db file...', $olderDbExists ? 'Updating' : 'Downloading'),
|
||||
);
|
||||
}
|
||||
|
||||
public function handleProgress(int $total, int $downloaded, bool $olderDbExists): void
|
||||
{
|
||||
if ($this->messageLogged || $total > $downloaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->messageLogged = true;
|
||||
$this->logger->notice(
|
||||
sprintf('Finished %s GeoLite2 db file', $olderDbExists ? 'updating' : 'downloading'),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
if ($result === GeolocationResult::DB_CREATED) {
|
||||
$this->eventDispatcher->dispatch(new GeoLiteDbCreated());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user