Added event dispatcher to track when a short URL is visited

This commit is contained in:
Alejandro Celaya
2019-07-13 12:04:21 +02:00
parent 014eb2a924
commit 91698034e7
16 changed files with 488 additions and 8 deletions

View File

@@ -65,6 +65,11 @@ class Visit extends AbstractEntity implements JsonSerializable
return $this->visitLocation ?? new UnknownVisitLocation();
}
public function isLocatable(): bool
{
return $this->hasRemoteAddr() && $this->remoteAddr !== IpAddress::LOCALHOST;
}
public function locate(VisitLocation $visitLocation): self
{
$this->visitLocation = $visitLocation;

View File

@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\EventDispatcher;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface;
use Shlinkio\Shlink\Common\IpGeolocation\Model\Location;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Entity\VisitLocation;
use function sprintf;
class LocateShortUrlVisit
{
/** @var IpLocationResolverInterface */
private $ipLocationResolver;
/** @var EntityManagerInterface */
private $em;
/** @var LoggerInterface */
private $logger;
public function __construct(
IpLocationResolverInterface $ipLocationResolver,
EntityManagerInterface $em,
LoggerInterface $logger
) {
$this->ipLocationResolver = $ipLocationResolver;
$this->em = $em;
$this->logger = $logger;
}
public function __invoke(ShortUrlVisited $shortUrlVisited): void
{
$visitId = $shortUrlVisited->visitId();
/** @var Visit|null $visit */
$visit = $this->em->find(Visit::class, $visitId);
if ($visit === null) {
$this->logger->warning(sprintf('Tried to locate visit with id "%s", but it does not exist.', $visitId));
return;
}
$location = $visit->isLocatable()
? $this->ipLocationResolver->resolveIpLocation($visit->getRemoteAddr())
: Location::emptyInstance();
$visit->locate(new VisitLocation($location));
$this->em->flush($visit);
}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\EventDispatcher;
final class ShortUrlVisited
{
/** @var string */
private $visitId;
public function __construct(string $visitId)
{
$this->visitId = $visitId;
}
public function visitId(): string
{
return $this->visitId;
}
}

View File

@@ -4,8 +4,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Service;
use Doctrine\ORM;
use Psr\EventDispatcher\EventDispatcherInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\EventDispatcher\ShortUrlVisited;
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Model\VisitsParams;
@@ -19,10 +21,13 @@ class VisitsTracker implements VisitsTrackerInterface
{
/** @var ORM\EntityManagerInterface */
private $em;
/** @var EventDispatcherInterface */
private $eventDispatcher;
public function __construct(ORM\EntityManagerInterface $em)
public function __construct(ORM\EntityManagerInterface $em, EventDispatcherInterface $eventDispatcher)
{
$this->em = $em;
$this->eventDispatcher = $eventDispatcher;
}
/**
@@ -41,6 +46,8 @@ class VisitsTracker implements VisitsTrackerInterface
$em = $this->em;
$em->persist($visit);
$em->flush($visit);
$this->eventDispatcher->dispatch(new ShortUrlVisited($visit->getId()));
}
/**