Updated VisitService to have a method which locates visits and allows entity manager to be cleared

This commit is contained in:
Alejandro Celaya 2018-11-17 07:47:42 +01:00
parent 1363194909
commit c1906606c6
8 changed files with 66 additions and 56 deletions

View file

@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\CLI\Command\Visit;
use Shlinkio\Shlink\Common\Exception\WrongIpException; use Shlinkio\Shlink\Common\Exception\WrongIpException;
use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface; use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface;
use Shlinkio\Shlink\Common\Util\IpAddress; use Shlinkio\Shlink\Common\Util\IpAddress;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Entity\VisitLocation; use Shlinkio\Shlink\Core\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Service\VisitServiceInterface; use Shlinkio\Shlink\Core\Service\VisitServiceInterface;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
@ -57,47 +58,52 @@ class ProcessVisitsCommand extends Command
$visits = $this->visitService->getUnlocatedVisits(); $visits = $this->visitService->getUnlocatedVisits();
foreach ($visits as $visit) { foreach ($visits as $visit) {
if (! $visit->hasRemoteAddr()) { $this->processVisit($io, $visit, false);
$io->writeln(
sprintf('<comment>%s</comment>', $this->translator->translate('Ignored visit with no IP address')),
OutputInterface::VERBOSITY_VERBOSE
);
continue;
}
$ipAddr = $visit->getRemoteAddr();
$io->write(sprintf('%s <fg=blue>%s</>', $this->translator->translate('Processing IP'), $ipAddr));
if ($ipAddr === IpAddress::LOCALHOST) {
$io->writeln(
sprintf(' [<comment>%s</comment>]', $this->translator->translate('Ignored localhost address'))
);
continue;
}
try {
$result = $this->ipLocationResolver->resolveIpLocation($ipAddr);
$location = new VisitLocation($result);
$visit->setVisitLocation($location);
$this->visitService->saveVisit($visit);
$io->writeln(sprintf(
' [<info>' . $this->translator->translate('Address located at "%s"') . '</info>]',
$location->getCountryName()
));
} catch (WrongIpException $e) {
$io->writeln(
sprintf(
' [<fg=red>%s</>]',
$this->translator->translate('An error occurred while locating IP. Skipped')
)
);
if ($io->isVerbose()) {
$this->getApplication()->renderException($e, $output);
}
}
} }
$io->success($this->translator->translate('Finished processing all IPs')); $io->success($this->translator->translate('Finished processing all IPs'));
} }
private function processVisit(SymfonyStyle $io, Visit $visit, bool $clear): void
{
if (! $visit->hasRemoteAddr()) {
$io->writeln(
sprintf('<comment>%s</comment>', $this->translator->translate('Ignored visit with no IP address')),
OutputInterface::VERBOSITY_VERBOSE
);
return;
}
$ipAddr = $visit->getRemoteAddr();
$io->write(sprintf('%s <fg=blue>%s</>', $this->translator->translate('Processing IP'), $ipAddr));
if ($ipAddr === IpAddress::LOCALHOST) {
$io->writeln(
sprintf(' [<comment>%s</comment>]', $this->translator->translate('Ignored localhost address'))
);
return;
}
try {
$result = $this->ipLocationResolver->resolveIpLocation($ipAddr);
} catch (WrongIpException $e) {
$io->writeln(
sprintf(
' [<fg=red>%s</>]',
$this->translator->translate('An error occurred while locating IP. Skipped')
)
);
if ($io->isVerbose()) {
$this->getApplication()->renderException($e, $output);
}
return;
}
$location = new VisitLocation($result);
$this->visitService->locateVisit($visit, $location, $clear);
$io->writeln(sprintf(
' [<info>' . $this->translator->translate('Address located at "%s"') . '</info>]',
$location->getCountryName()
));
}
} }

View file

@ -81,7 +81,7 @@ class GetVisitsCommandTest extends TestCase
{ {
$shortCode = 'abc123'; $shortCode = 'abc123';
$this->visitsTracker->info($shortCode, Argument::any())->willReturn([ $this->visitsTracker->info($shortCode, Argument::any())->willReturn([
(new Visit(new ShortUrl(''), new Visitor('bar', 'foo', '')))->setVisitLocation( (new Visit(new ShortUrl(''), new Visitor('bar', 'foo', '')))->locate(
new VisitLocation(['country_name' => 'Spain']) new VisitLocation(['country_name' => 'Spain'])
), ),
])->shouldBeCalledOnce(); ])->shouldBeCalledOnce();

View file

@ -64,7 +64,7 @@ class ProcessVisitsCommandTest extends TestCase
$this->visitService->getUnlocatedVisits()->willReturn($visits) $this->visitService->getUnlocatedVisits()->willReturn($visits)
->shouldBeCalledOnce(); ->shouldBeCalledOnce();
$this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(count($visits)); $this->visitService->locateVisit(Argument::cetera())->shouldBeCalledTimes(count($visits));
$this->ipResolver->resolveIpLocation(Argument::any())->willReturn([]) $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
->shouldBeCalledTimes(count($visits)); ->shouldBeCalledTimes(count($visits));
@ -96,7 +96,7 @@ class ProcessVisitsCommandTest extends TestCase
$this->visitService->getUnlocatedVisits()->willReturn($visits) $this->visitService->getUnlocatedVisits()->willReturn($visits)
->shouldBeCalledOnce(); ->shouldBeCalledOnce();
$this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(count($visits) - 4); $this->visitService->locateVisit(Argument::cetera())->shouldBeCalledTimes(count($visits) - 4);
$this->ipResolver->resolveIpLocation(Argument::any())->willReturn([]) $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
->shouldBeCalledTimes(count($visits) - 4); ->shouldBeCalledTimes(count($visits) - 4);

View file

@ -93,7 +93,7 @@ class Visit extends AbstractEntity implements JsonSerializable
return $this->visitLocation; return $this->visitLocation;
} }
public function setVisitLocation(VisitLocation $visitLocation): self public function locate(VisitLocation $visitLocation): self
{ {
$this->visitLocation = $visitLocation; $this->visitLocation = $visitLocation;
return $this; return $this;

View file

@ -5,6 +5,7 @@ namespace Shlinkio\Shlink\Core\Service;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Repository\VisitRepository; use Shlinkio\Shlink\Core\Repository\VisitRepository;
class VisitService implements VisitServiceInterface class VisitService implements VisitServiceInterface
@ -22,19 +23,23 @@ class VisitService implements VisitServiceInterface
/** /**
* @return Visit[] * @return Visit[]
*/ */
public function getUnlocatedVisits() public function getUnlocatedVisits(): array
{ {
/** @var VisitRepository $repo */ /** @var VisitRepository $repo */
$repo = $this->em->getRepository(Visit::class); $repo = $this->em->getRepository(Visit::class);
return $repo->findUnlocatedVisits(); return $repo->findUnlocatedVisits();
} }
/** public function locateVisit(Visit $visit, VisitLocation $location, bool $clear = false): void
* @param Visit $visit
*/
public function saveVisit(Visit $visit)
{ {
$visit->locate($location);
$this->em->persist($visit); $this->em->persist($visit);
$this->em->flush(); $this->em->flush();
if ($clear) {
$this->em->clear(VisitLocation::class);
$this->em->clear(Visit::class);
}
} }
} }

View file

@ -4,16 +4,14 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Service; namespace Shlinkio\Shlink\Core\Service;
use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Entity\VisitLocation;
interface VisitServiceInterface interface VisitServiceInterface
{ {
/** /**
* @return Visit[] * @return Visit[]
*/ */
public function getUnlocatedVisits(); public function getUnlocatedVisits(): array;
/** public function locateVisit(Visit $visit, VisitLocation $location, bool $clear = false): void;
* @param Visit $visit
*/
public function saveVisit(Visit $visit);
} }

View file

@ -45,7 +45,7 @@ class VisitRepositoryTest extends DatabaseTestCase
if ($i % 2 === 0) { if ($i % 2 === 0) {
$location = new VisitLocation([]); $location = new VisitLocation([]);
$this->getEntityManager()->persist($location); $this->getEntityManager()->persist($location);
$visit->setVisitLocation($location); $visit->locate($location);
} }
$this->getEntityManager()->persist($visit); $this->getEntityManager()->persist($visit);

View file

@ -8,6 +8,7 @@ use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
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\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Repository\VisitRepository; use Shlinkio\Shlink\Core\Repository\VisitRepository;
use Shlinkio\Shlink\Core\Service\VisitService; use Shlinkio\Shlink\Core\Service\VisitService;
@ -32,12 +33,12 @@ class VisitServiceTest extends TestCase
/** /**
* @test * @test
*/ */
public function saveVisitsPersistsProvidedVisit() public function locateVisitPersistsProvidedVisit()
{ {
$visit = new Visit(new ShortUrl(''), Visitor::emptyInstance()); $visit = new Visit(new ShortUrl(''), Visitor::emptyInstance());
$this->em->persist($visit)->shouldBeCalledOnce(); $this->em->persist($visit)->shouldBeCalledOnce();
$this->em->flush()->shouldBeCalledOnce(); $this->em->flush()->shouldBeCalledOnce();
$this->visitService->saveVisit($visit); $this->visitService->locateVisit($visit, new VisitLocation([]));
} }
/** /**