mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-27 16:26:37 +03:00
Updated VisitService to have a method which locates visits and allows entity manager to be cleared
This commit is contained in:
parent
1363194909
commit
c1906606c6
8 changed files with 66 additions and 56 deletions
|
@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\CLI\Command\Visit;
|
|||
use Shlinkio\Shlink\Common\Exception\WrongIpException;
|
||||
use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface;
|
||||
use Shlinkio\Shlink\Common\Util\IpAddress;
|
||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||
use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
||||
use Shlinkio\Shlink\Core\Service\VisitServiceInterface;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
|
@ -57,12 +58,20 @@ class ProcessVisitsCommand extends Command
|
|||
$visits = $this->visitService->getUnlocatedVisits();
|
||||
|
||||
foreach ($visits as $visit) {
|
||||
$this->processVisit($io, $visit, false);
|
||||
}
|
||||
|
||||
$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
|
||||
);
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
$ipAddr = $visit->getRemoteAddr();
|
||||
|
@ -71,20 +80,11 @@ class ProcessVisitsCommand extends Command
|
|||
$io->writeln(
|
||||
sprintf(' [<comment>%s</comment>]', $this->translator->translate('Ignored localhost address'))
|
||||
);
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
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(
|
||||
|
@ -95,9 +95,15 @@ class ProcessVisitsCommand extends Command
|
|||
if ($io->isVerbose()) {
|
||||
$this->getApplication()->renderException($e, $output);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$io->success($this->translator->translate('Finished processing all IPs'));
|
||||
$location = new VisitLocation($result);
|
||||
$this->visitService->locateVisit($visit, $location, $clear);
|
||||
$io->writeln(sprintf(
|
||||
' [<info>' . $this->translator->translate('Address located at "%s"') . '</info>]',
|
||||
$location->getCountryName()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -81,7 +81,7 @@ class GetVisitsCommandTest extends TestCase
|
|||
{
|
||||
$shortCode = 'abc123';
|
||||
$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'])
|
||||
),
|
||||
])->shouldBeCalledOnce();
|
||||
|
|
|
@ -64,7 +64,7 @@ class ProcessVisitsCommandTest extends TestCase
|
|||
$this->visitService->getUnlocatedVisits()->willReturn($visits)
|
||||
->shouldBeCalledOnce();
|
||||
|
||||
$this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(count($visits));
|
||||
$this->visitService->locateVisit(Argument::cetera())->shouldBeCalledTimes(count($visits));
|
||||
$this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
|
||||
->shouldBeCalledTimes(count($visits));
|
||||
|
||||
|
@ -96,7 +96,7 @@ class ProcessVisitsCommandTest extends TestCase
|
|||
$this->visitService->getUnlocatedVisits()->willReturn($visits)
|
||||
->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([])
|
||||
->shouldBeCalledTimes(count($visits) - 4);
|
||||
|
||||
|
|
|
@ -93,7 +93,7 @@ class Visit extends AbstractEntity implements JsonSerializable
|
|||
return $this->visitLocation;
|
||||
}
|
||||
|
||||
public function setVisitLocation(VisitLocation $visitLocation): self
|
||||
public function locate(VisitLocation $visitLocation): self
|
||||
{
|
||||
$this->visitLocation = $visitLocation;
|
||||
return $this;
|
||||
|
|
|
@ -5,6 +5,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\Repository\VisitRepository;
|
||||
|
||||
class VisitService implements VisitServiceInterface
|
||||
|
@ -22,19 +23,23 @@ class VisitService implements VisitServiceInterface
|
|||
/**
|
||||
* @return Visit[]
|
||||
*/
|
||||
public function getUnlocatedVisits()
|
||||
public function getUnlocatedVisits(): array
|
||||
{
|
||||
/** @var VisitRepository $repo */
|
||||
$repo = $this->em->getRepository(Visit::class);
|
||||
return $repo->findUnlocatedVisits();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Visit $visit
|
||||
*/
|
||||
public function saveVisit(Visit $visit)
|
||||
public function locateVisit(Visit $visit, VisitLocation $location, bool $clear = false): void
|
||||
{
|
||||
$visit->locate($location);
|
||||
|
||||
$this->em->persist($visit);
|
||||
$this->em->flush();
|
||||
|
||||
if ($clear) {
|
||||
$this->em->clear(VisitLocation::class);
|
||||
$this->em->clear(Visit::class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,16 +4,14 @@ 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();
|
||||
public function getUnlocatedVisits(): array;
|
||||
|
||||
/**
|
||||
* @param Visit $visit
|
||||
*/
|
||||
public function saveVisit(Visit $visit);
|
||||
public function locateVisit(Visit $visit, VisitLocation $location, bool $clear = false): void;
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ class VisitRepositoryTest extends DatabaseTestCase
|
|||
if ($i % 2 === 0) {
|
||||
$location = new VisitLocation([]);
|
||||
$this->getEntityManager()->persist($location);
|
||||
$visit->setVisitLocation($location);
|
||||
$visit->locate($location);
|
||||
}
|
||||
|
||||
$this->getEntityManager()->persist($visit);
|
||||
|
|
|
@ -8,6 +8,7 @@ use PHPUnit\Framework\TestCase;
|
|||
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\Model\Visitor;
|
||||
use Shlinkio\Shlink\Core\Repository\VisitRepository;
|
||||
use Shlinkio\Shlink\Core\Service\VisitService;
|
||||
|
@ -32,12 +33,12 @@ class VisitServiceTest extends TestCase
|
|||
/**
|
||||
* @test
|
||||
*/
|
||||
public function saveVisitsPersistsProvidedVisit()
|
||||
public function locateVisitPersistsProvidedVisit()
|
||||
{
|
||||
$visit = new Visit(new ShortUrl(''), Visitor::emptyInstance());
|
||||
$this->em->persist($visit)->shouldBeCalledOnce();
|
||||
$this->em->flush()->shouldBeCalledOnce();
|
||||
$this->visitService->saveVisit($visit);
|
||||
$this->visitService->locateVisit($visit, new VisitLocation([]));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue