From e0e522c3f5439f71c5e7d83d056974db7db5a916 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 20 Jul 2019 11:21:00 +0200 Subject: [PATCH] Updated LocateShortUrlVisit listener so that it updates geolite db is needed --- .gitignore | 1 + .../Core/config/event_dispatcher.config.php | 8 +++++- .../EventDispatcher/LocateShortUrlVisit.php | 27 ++++++++++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a5163a27..f01b1741 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ vendor/ data/database.sqlite data/shlink-tests.db data/GeoLite2-City.mmdb +data/GeoLite2-City.mmdb.* docs/swagger-ui* docker-compose.override.yml .phpunit.result.cache diff --git a/module/Core/config/event_dispatcher.config.php b/module/Core/config/event_dispatcher.config.php index ab34fe99..767142f1 100644 --- a/module/Core/config/event_dispatcher.config.php +++ b/module/Core/config/event_dispatcher.config.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core; +use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater; use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface; use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory; @@ -24,7 +25,12 @@ return [ ], ConfigAbstractFactory::class => [ - EventDispatcher\LocateShortUrlVisit::class => [IpLocationResolverInterface::class, 'em', 'Logger_Shlink'], + EventDispatcher\LocateShortUrlVisit::class => [ + IpLocationResolverInterface::class, + 'em', + 'Logger_Shlink', + GeolocationDbUpdater::class, + ], ], ]; diff --git a/module/Core/src/EventDispatcher/LocateShortUrlVisit.php b/module/Core/src/EventDispatcher/LocateShortUrlVisit.php index fa6aaa09..de7eb360 100644 --- a/module/Core/src/EventDispatcher/LocateShortUrlVisit.php +++ b/module/Core/src/EventDispatcher/LocateShortUrlVisit.php @@ -5,6 +5,8 @@ namespace Shlinkio\Shlink\Core\EventDispatcher; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; +use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException; +use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface; use Shlinkio\Shlink\Common\Exception\WrongIpException; use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface; use Shlinkio\Shlink\Common\IpGeolocation\Model\Location; @@ -21,15 +23,19 @@ class LocateShortUrlVisit private $em; /** @var LoggerInterface */ private $logger; + /** @var GeolocationDbUpdaterInterface */ + private $dbUpdater; public function __construct( IpLocationResolverInterface $ipLocationResolver, EntityManagerInterface $em, - LoggerInterface $logger + LoggerInterface $logger, + GeolocationDbUpdaterInterface $dbUpdater ) { $this->ipLocationResolver = $ipLocationResolver; $this->em = $em; $this->logger = $logger; + $this->dbUpdater = $dbUpdater; } public function __invoke(ShortUrlVisited $shortUrlVisited): void @@ -43,6 +49,25 @@ class LocateShortUrlVisit return; } + try { + $this->dbUpdater->checkDbUpdate(function (bool $olderDbExists) { + $this->logger->notice(sprintf('%s GeoLite2 database...', $olderDbExists ? 'Updating' : 'Downloading')); + }); + } catch (GeolocationDbUpdateFailedException $e) { + if (! $e->olderDbExists()) { + $this->logger->error( + sprintf( + 'GeoLite2 database download failed. It is not possible to locate visit with id %s. {e}', + $visitId + ), + ['e' => $e] + ); + return; + } + + $this->logger->warning('GeoLite2 database update failed. Proceeding with old version. {e}', ['e' => $e]); + } + try { $location = $visit->isLocatable() ? $this->ipLocationResolver->resolveIpLocation($visit->getRemoteAddr())