Updated LocateShortUrlVisit so that it dispatches a VisitLocated event

This commit is contained in:
Alejandro Celaya 2019-12-28 13:07:11 +01:00
parent 4886825564
commit b17bcb6c93
6 changed files with 58 additions and 14 deletions

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core; namespace Shlinkio\Shlink\Core;
use Psr\EventDispatcher\EventDispatcherInterface;
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater; use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater;
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface; use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory; use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
@ -16,7 +17,7 @@ return [
EventDispatcher\ShortUrlVisited::class => [ EventDispatcher\ShortUrlVisited::class => [
EventDispatcher\LocateShortUrlVisit::class, EventDispatcher\LocateShortUrlVisit::class,
], ],
EventDispatcher\ShortUrlLocated::class => [ EventDispatcher\VisitLocated::class => [
EventDispatcher\NotifyVisitToWebHooks::class, EventDispatcher\NotifyVisitToWebHooks::class,
], ],
], ],
@ -34,6 +35,7 @@ return [
'em', 'em',
'Logger_Shlink', 'Logger_Shlink',
GeolocationDbUpdater::class, GeolocationDbUpdater::class,
EventDispatcherInterface::class,
], ],
EventDispatcher\NotifyVisitToWebHooks::class => [ EventDispatcher\NotifyVisitToWebHooks::class => [
'httpClient', 'httpClient',

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\EventDispatcher; namespace Shlinkio\Shlink\Core\EventDispatcher;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException; use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface; use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface;
@ -26,17 +27,21 @@ class LocateShortUrlVisit
private $logger; private $logger;
/** @var GeolocationDbUpdaterInterface */ /** @var GeolocationDbUpdaterInterface */
private $dbUpdater; private $dbUpdater;
/** @var EventDispatcherInterface */
private $eventDispatcher;
public function __construct( public function __construct(
IpLocationResolverInterface $ipLocationResolver, IpLocationResolverInterface $ipLocationResolver,
EntityManagerInterface $em, EntityManagerInterface $em,
LoggerInterface $logger, LoggerInterface $logger,
GeolocationDbUpdaterInterface $dbUpdater GeolocationDbUpdaterInterface $dbUpdater,
EventDispatcherInterface $eventDispatcher
) { ) {
$this->ipLocationResolver = $ipLocationResolver; $this->ipLocationResolver = $ipLocationResolver;
$this->em = $em; $this->em = $em;
$this->logger = $logger; $this->logger = $logger;
$this->dbUpdater = $dbUpdater; $this->dbUpdater = $dbUpdater;
$this->eventDispatcher = $eventDispatcher;
} }
public function __invoke(ShortUrlVisited $shortUrlVisited): void public function __invoke(ShortUrlVisited $shortUrlVisited): void
@ -52,6 +57,15 @@ class LocateShortUrlVisit
return; return;
} }
if ($this->downloadOrUpdateGeoLiteDb($visitId)) {
$this->locateVisit($visitId, $visit);
}
$this->eventDispatcher->dispatch(new VisitLocated($visitId));
}
private function downloadOrUpdateGeoLiteDb(string $visitId): bool
{
try { try {
$this->dbUpdater->checkDbUpdate(function (bool $olderDbExists) { $this->dbUpdater->checkDbUpdate(function (bool $olderDbExists) {
$this->logger->notice(sprintf('%s GeoLite2 database...', $olderDbExists ? 'Updating' : 'Downloading')); $this->logger->notice(sprintf('%s GeoLite2 database...', $olderDbExists ? 'Updating' : 'Downloading'));
@ -62,25 +76,29 @@ class LocateShortUrlVisit
'GeoLite2 database download failed. It is not possible to locate visit with id {visitId}. {e}', 'GeoLite2 database download failed. It is not possible to locate visit with id {visitId}. {e}',
['e' => $e, 'visitId' => $visitId] ['e' => $e, 'visitId' => $visitId]
); );
return; return false;
} }
$this->logger->warning('GeoLite2 database update failed. Proceeding with old version. {e}', ['e' => $e]); $this->logger->warning('GeoLite2 database update failed. Proceeding with old version. {e}', ['e' => $e]);
} }
return true;
}
private function locateVisit(string $visitId, Visit $visit): void
{
try { try {
$location = $visit->isLocatable() $location = $visit->isLocatable()
? $this->ipLocationResolver->resolveIpLocation($visit->getRemoteAddr()) ? $this->ipLocationResolver->resolveIpLocation($visit->getRemoteAddr())
: Location::emptyInstance(); : Location::emptyInstance();
$visit->locate(new VisitLocation($location));
$this->em->flush();
} catch (WrongIpException $e) { } catch (WrongIpException $e) {
$this->logger->warning( $this->logger->warning(
'Tried to locate visit with id "{visitId}", but its address seems to be wrong. {e}', 'Tried to locate visit with id "{visitId}", but its address seems to be wrong. {e}',
['e' => $e, 'visitId' => $visitId] ['e' => $e, 'visitId' => $visitId]
); );
return;
} }
$visit->locate(new VisitLocation($location));
$this->em->flush();
} }
} }

View File

@ -51,7 +51,7 @@ class NotifyVisitToWebHooks
$this->appOptions = $appOptions; $this->appOptions = $appOptions;
} }
public function __invoke(ShortUrlLocated $shortUrlLocated): void public function __invoke(VisitLocated $shortUrlLocated): void
{ {
if (empty($this->webhooks)) { if (empty($this->webhooks)) {
return; return;

View File

@ -6,7 +6,7 @@ namespace Shlinkio\Shlink\Core\EventDispatcher;
use JsonSerializable; use JsonSerializable;
final class ShortUrlLocated implements JsonSerializable final class VisitLocated implements JsonSerializable
{ {
/** @var string */ /** @var string */
private $visitId; private $visitId;

View File

@ -8,6 +8,7 @@ use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException; use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface; use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface;
@ -17,6 +18,7 @@ use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Entity\VisitLocation; use Shlinkio\Shlink\Core\Entity\VisitLocation;
use Shlinkio\Shlink\Core\EventDispatcher\LocateShortUrlVisit; use Shlinkio\Shlink\Core\EventDispatcher\LocateShortUrlVisit;
use Shlinkio\Shlink\Core\EventDispatcher\ShortUrlVisited; use Shlinkio\Shlink\Core\EventDispatcher\ShortUrlVisited;
use Shlinkio\Shlink\Core\EventDispatcher\VisitLocated;
use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\Model\UnknownVisitLocation; use Shlinkio\Shlink\Core\Visit\Model\UnknownVisitLocation;
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException; use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
@ -35,6 +37,8 @@ class LocateShortUrlVisitTest extends TestCase
private $logger; private $logger;
/** @var ObjectProphecy */ /** @var ObjectProphecy */
private $dbUpdater; private $dbUpdater;
/** @var ObjectProphecy */
private $eventDispatcher;
public function setUp(): void public function setUp(): void
{ {
@ -42,12 +46,14 @@ class LocateShortUrlVisitTest extends TestCase
$this->em = $this->prophesize(EntityManagerInterface::class); $this->em = $this->prophesize(EntityManagerInterface::class);
$this->logger = $this->prophesize(LoggerInterface::class); $this->logger = $this->prophesize(LoggerInterface::class);
$this->dbUpdater = $this->prophesize(GeolocationDbUpdaterInterface::class); $this->dbUpdater = $this->prophesize(GeolocationDbUpdaterInterface::class);
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$this->locateVisit = new LocateShortUrlVisit( $this->locateVisit = new LocateShortUrlVisit(
$this->ipLocationResolver->reveal(), $this->ipLocationResolver->reveal(),
$this->em->reveal(), $this->em->reveal(),
$this->logger->reveal(), $this->logger->reveal(),
$this->dbUpdater->reveal() $this->dbUpdater->reveal(),
$this->eventDispatcher->reveal()
); );
} }
@ -59,6 +65,8 @@ class LocateShortUrlVisitTest extends TestCase
$logWarning = $this->logger->warning('Tried to locate visit with id "{visitId}", but it does not exist.', [ $logWarning = $this->logger->warning('Tried to locate visit with id "{visitId}", but it does not exist.', [
'visitId' => 123, 'visitId' => 123,
]); ]);
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function () {
});
($this->locateVisit)($event); ($this->locateVisit)($event);
@ -66,6 +74,7 @@ class LocateShortUrlVisitTest extends TestCase
$this->em->flush()->shouldNotHaveBeenCalled(); $this->em->flush()->shouldNotHaveBeenCalled();
$this->ipLocationResolver->resolveIpLocation(Argument::cetera())->shouldNotHaveBeenCalled(); $this->ipLocationResolver->resolveIpLocation(Argument::cetera())->shouldNotHaveBeenCalled();
$logWarning->shouldHaveBeenCalled(); $logWarning->shouldHaveBeenCalled();
$dispatch->shouldNotHaveBeenCalled();
} }
/** @test */ /** @test */
@ -82,6 +91,8 @@ class LocateShortUrlVisitTest extends TestCase
Argument::containingString('Tried to locate visit with id "{visitId}", but its address seems to be wrong.'), Argument::containingString('Tried to locate visit with id "{visitId}", but its address seems to be wrong.'),
Argument::type('array') Argument::type('array')
); );
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function () {
});
($this->locateVisit)($event); ($this->locateVisit)($event);
@ -89,6 +100,7 @@ class LocateShortUrlVisitTest extends TestCase
$resolveLocation->shouldHaveBeenCalledOnce(); $resolveLocation->shouldHaveBeenCalledOnce();
$logWarning->shouldHaveBeenCalled(); $logWarning->shouldHaveBeenCalled();
$this->em->flush()->shouldNotHaveBeenCalled(); $this->em->flush()->shouldNotHaveBeenCalled();
$dispatch->shouldHaveBeenCalledOnce();
} }
/** /**
@ -102,6 +114,8 @@ class LocateShortUrlVisitTest extends TestCase
$flush = $this->em->flush()->will(function () { $flush = $this->em->flush()->will(function () {
}); });
$resolveIp = $this->ipLocationResolver->resolveIpLocation(Argument::any()); $resolveIp = $this->ipLocationResolver->resolveIpLocation(Argument::any());
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function () {
});
($this->locateVisit)($event); ($this->locateVisit)($event);
@ -110,6 +124,7 @@ class LocateShortUrlVisitTest extends TestCase
$flush->shouldHaveBeenCalledOnce(); $flush->shouldHaveBeenCalledOnce();
$resolveIp->shouldNotHaveBeenCalled(); $resolveIp->shouldNotHaveBeenCalled();
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled(); $this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
$dispatch->shouldHaveBeenCalledOnce();
} }
public function provideNonLocatableVisits(): iterable public function provideNonLocatableVisits(): iterable
@ -133,6 +148,8 @@ class LocateShortUrlVisitTest extends TestCase
$flush = $this->em->flush()->will(function () { $flush = $this->em->flush()->will(function () {
}); });
$resolveIp = $this->ipLocationResolver->resolveIpLocation($ipAddr)->willReturn($location); $resolveIp = $this->ipLocationResolver->resolveIpLocation($ipAddr)->willReturn($location);
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function () {
});
($this->locateVisit)($event); ($this->locateVisit)($event);
@ -141,6 +158,7 @@ class LocateShortUrlVisitTest extends TestCase
$flush->shouldHaveBeenCalledOnce(); $flush->shouldHaveBeenCalledOnce();
$resolveIp->shouldHaveBeenCalledOnce(); $resolveIp->shouldHaveBeenCalledOnce();
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled(); $this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
$dispatch->shouldHaveBeenCalledOnce();
} }
/** @test */ /** @test */
@ -157,6 +175,8 @@ class LocateShortUrlVisitTest extends TestCase
}); });
$resolveIp = $this->ipLocationResolver->resolveIpLocation($ipAddr)->willReturn($location); $resolveIp = $this->ipLocationResolver->resolveIpLocation($ipAddr)->willReturn($location);
$checkUpdateDb = $this->dbUpdater->checkDbUpdate(Argument::cetera())->willThrow($e); $checkUpdateDb = $this->dbUpdater->checkDbUpdate(Argument::cetera())->willThrow($e);
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function () {
});
($this->locateVisit)($event); ($this->locateVisit)($event);
@ -169,6 +189,7 @@ class LocateShortUrlVisitTest extends TestCase
'GeoLite2 database update failed. Proceeding with old version. {e}', 'GeoLite2 database update failed. Proceeding with old version. {e}',
['e' => $e] ['e' => $e]
)->shouldHaveBeenCalledOnce(); )->shouldHaveBeenCalledOnce();
$dispatch->shouldHaveBeenCalledOnce();
} }
/** @test */ /** @test */
@ -189,6 +210,8 @@ class LocateShortUrlVisitTest extends TestCase
'GeoLite2 database download failed. It is not possible to locate visit with id {visitId}. {e}', 'GeoLite2 database download failed. It is not possible to locate visit with id {visitId}. {e}',
['e' => $e, 'visitId' => 123] ['e' => $e, 'visitId' => 123]
); );
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function () {
});
($this->locateVisit)($event); ($this->locateVisit)($event);
@ -198,5 +221,6 @@ class LocateShortUrlVisitTest extends TestCase
$resolveIp->shouldNotHaveBeenCalled(); $resolveIp->shouldNotHaveBeenCalled();
$checkUpdateDb->shouldHaveBeenCalledOnce(); $checkUpdateDb->shouldHaveBeenCalledOnce();
$logError->shouldHaveBeenCalledOnce(); $logError->shouldHaveBeenCalledOnce();
$dispatch->shouldHaveBeenCalledOnce();
} }
} }

View File

@ -19,7 +19,7 @@ use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\EventDispatcher\NotifyVisitToWebHooks; use Shlinkio\Shlink\Core\EventDispatcher\NotifyVisitToWebHooks;
use Shlinkio\Shlink\Core\EventDispatcher\ShortUrlLocated; use Shlinkio\Shlink\Core\EventDispatcher\VisitLocated;
use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Options\AppOptions; use Shlinkio\Shlink\Core\Options\AppOptions;
@ -47,7 +47,7 @@ class NotifyVisitToWebHooksTest extends TestCase
{ {
$find = $this->em->find(Visit::class, '1')->willReturn(null); $find = $this->em->find(Visit::class, '1')->willReturn(null);
$this->createListener([])(new ShortUrlLocated('1')); $this->createListener([])(new VisitLocated('1'));
$find->shouldNotHaveBeenCalled(); $find->shouldNotHaveBeenCalled();
} }
@ -66,7 +66,7 @@ class NotifyVisitToWebHooksTest extends TestCase
['visitId' => '1'] ['visitId' => '1']
); );
$this->createListener(['foo', 'bar'])(new ShortUrlLocated('1')); $this->createListener(['foo', 'bar'])(new VisitLocated('1'));
$find->shouldHaveBeenCalledOnce(); $find->shouldHaveBeenCalledOnce();
$logWarning->shouldHaveBeenCalledOnce(); $logWarning->shouldHaveBeenCalledOnce();
@ -111,7 +111,7 @@ class NotifyVisitToWebHooksTest extends TestCase
}) })
); );
$this->createListener($webhooks)(new ShortUrlLocated('1')); $this->createListener($webhooks)(new VisitLocated('1'));
$find->shouldHaveBeenCalledOnce(); $find->shouldHaveBeenCalledOnce();
$requestAsync->shouldHaveBeenCalledTimes(count($webhooks)); $requestAsync->shouldHaveBeenCalledTimes(count($webhooks));