Moved visits iteration logic from command to service to allow lazy loading of entries in resultset

This commit is contained in:
Alejandro Celaya
2018-11-17 09:42:15 +01:00
parent 1bc01057f3
commit 0aae0d888c
9 changed files with 229 additions and 124 deletions

View File

@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Exception;
class IpCannotBeLocatedException extends RuntimeException
{
}

View File

@@ -10,15 +10,12 @@ use Shlinkio\Shlink\Core\Entity\Visit;
class VisitRepository extends EntityRepository implements VisitRepositoryInterface
{
/**
* @return Visit[]
*/
public function findUnlocatedVisits(): array
public function findUnlocatedVisits(): iterable
{
$qb = $this->createQueryBuilder('v');
$qb->where($qb->expr()->isNull('v.visitLocation'));
return $qb->getQuery()->getResult();
return $qb->getQuery()->iterate();
}
/**

View File

@@ -10,10 +10,7 @@ use Shlinkio\Shlink\Core\Entity\Visit;
interface VisitRepositoryInterface extends ObjectRepository
{
/**
* @return Visit[]
*/
public function findUnlocatedVisits(): array;
public function findUnlocatedVisits(): iterable;
/**
* @param ShortUrl|int $shortUrl

View File

@@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\Service;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
use Shlinkio\Shlink\Core\Repository\VisitRepository;
class VisitService implements VisitServiceInterface
@@ -20,26 +21,36 @@ class VisitService implements VisitServiceInterface
$this->em = $em;
}
/**
* @return Visit[]
*/
public function getUnlocatedVisits(): array
public function locateVisits(callable $getGeolocationData, ?callable $locatedVisit = null): void
{
/** @var VisitRepository $repo */
$repo = $this->em->getRepository(Visit::class);
return $repo->findUnlocatedVisits();
$results = $repo->findUnlocatedVisits();
foreach ($results as [$visit]) {
try {
$locationData = $getGeolocationData($visit);
} catch (IpCannotBeLocatedException $e) {
// Skip if the visit's IP could not be located
continue;
}
$location = new VisitLocation($locationData);
$this->locateVisit($visit, $location, $locatedVisit);
}
}
public function locateVisit(Visit $visit, VisitLocation $location, bool $clear = false): void
private function locateVisit(Visit $visit, VisitLocation $location, ?callable $locatedVisit): void
{
$visit->locate($location);
$this->em->persist($visit);
$this->em->flush();
if ($clear) {
$this->em->clear(VisitLocation::class);
$this->em->clear(Visit::class);
if ($locatedVisit !== null) {
$locatedVisit($location, $visit);
}
$this->em->clear();
}
}

View File

@@ -3,15 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Service;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Entity\VisitLocation;
interface VisitServiceInterface
{
/**
* @return Visit[]
*/
public function getUnlocatedVisits(): array;
public function locateVisit(Visit $visit, VisitLocation $location, bool $clear = false): void;
public function locateVisits(callable $getGeolocationData, ?callable $locatedVisit = null): void;
}

View File

@@ -52,7 +52,13 @@ class VisitRepositoryTest extends DatabaseTestCase
}
$this->getEntityManager()->flush();
$this->assertCount(3, $this->repo->findUnlocatedVisits());
$resultsCount = 0;
$results = $this->repo->findUnlocatedVisits();
foreach ($results as $value) {
$resultsCount++;
}
$this->assertEquals(3, $resultsCount);
}
/**

View File

@@ -5,13 +5,18 @@ namespace ShlinkioTest\Shlink\Core\Service;
use Doctrine\ORM\EntityManager;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Repository\VisitRepository;
use Shlinkio\Shlink\Core\Service\VisitService;
use function array_shift;
use function count;
use function func_get_args;
class VisitServiceTest extends TestCase
{
@@ -33,22 +38,68 @@ class VisitServiceTest extends TestCase
/**
* @test
*/
public function locateVisitPersistsProvidedVisit()
public function locateVisitsIteratesAndLocatesUnlocatedVisits()
{
$visit = new Visit(new ShortUrl(''), Visitor::emptyInstance());
$this->em->persist($visit)->shouldBeCalledOnce();
$this->em->flush()->shouldBeCalledOnce();
$this->visitService->locateVisit($visit, new VisitLocation([]));
$unlocatedVisits = [
[new Visit(new ShortUrl('foo'), Visitor::emptyInstance())],
[new Visit(new ShortUrl('bar'), Visitor::emptyInstance())],
];
$repo = $this->prophesize(VisitRepository::class);
$findUnlocatedVisits = $repo->findUnlocatedVisits()->willReturn($unlocatedVisits);
$getRepo = $this->em->getRepository(Visit::class)->willReturn($repo->reveal());
$persist = $this->em->persist(Argument::type(Visit::class))->will(function () {
});
$flush = $this->em->flush()->will(function () {
});
$clear = $this->em->clear()->will(function () {
});
$this->visitService->locateVisits(function () {
return [];
}, function () {
$args = func_get_args();
$this->assertInstanceOf(VisitLocation::class, array_shift($args));
$this->assertInstanceOf(Visit::class, array_shift($args));
});
$findUnlocatedVisits->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledOnce();
$persist->shouldHaveBeenCalledTimes(count($unlocatedVisits));
$flush->shouldHaveBeenCalledTimes(count($unlocatedVisits));
$clear->shouldHaveBeenCalledTimes(count($unlocatedVisits));
}
/**
* @test
*/
public function getUnlocatedVisitsFallbacksToRepository()
public function visitsWhichCannotBeLocatedAreIgnored()
{
$unlocatedVisits = [
[new Visit(new ShortUrl('foo'), Visitor::emptyInstance())],
];
$repo = $this->prophesize(VisitRepository::class);
$repo->findUnlocatedVisits()->shouldBeCalledOnce();
$this->em->getRepository(Visit::class)->willReturn($repo->reveal())->shouldBeCalledOnce();
$this->visitService->getUnlocatedVisits();
$findUnlocatedVisits = $repo->findUnlocatedVisits()->willReturn($unlocatedVisits);
$getRepo = $this->em->getRepository(Visit::class)->willReturn($repo->reveal());
$persist = $this->em->persist(Argument::type(Visit::class))->will(function () {
});
$flush = $this->em->flush()->will(function () {
});
$clear = $this->em->clear()->will(function () {
});
$this->visitService->locateVisits(function () {
throw new IpCannotBeLocatedException('Cannot be located');
});
$findUnlocatedVisits->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledOnce();
$persist->shouldNotHaveBeenCalled();
$flush->shouldNotHaveBeenCalled();
$clear->shouldNotHaveBeenCalled();
}
}