Defined stricter model to represent one geo location

This commit is contained in:
Alejandro Celaya
2019-02-17 12:59:55 +01:00
parent 5c5dde48de
commit e2abe23895
19 changed files with 204 additions and 198 deletions

View File

@@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Entity;
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Common\IpGeolocation\Model\Location;
use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
use function array_key_exists;
class VisitLocation extends AbstractEntity implements VisitLocationInterface
{
@@ -24,9 +24,9 @@ class VisitLocation extends AbstractEntity implements VisitLocationInterface
/** @var string */
private $timezone;
public function __construct(array $locationInfo)
public function __construct(Location $location)
{
$this->exchangeArray($locationInfo);
$this->exchangeLocationInfo($location);
}
public function getCountryName(): string
@@ -49,32 +49,15 @@ class VisitLocation extends AbstractEntity implements VisitLocationInterface
return $this->cityName ?? '';
}
/**
* Exchange internal values from provided array
*/
private function exchangeArray(array $array): void
private function exchangeLocationInfo(Location $info): void
{
if (array_key_exists('country_code', $array)) {
$this->countryCode = (string) $array['country_code'];
}
if (array_key_exists('country_name', $array)) {
$this->countryName = (string) $array['country_name'];
}
if (array_key_exists('region_name', $array)) {
$this->regionName = (string) $array['region_name'];
}
if (array_key_exists('city', $array)) {
$this->cityName = (string) $array['city'];
}
if (array_key_exists('latitude', $array)) {
$this->latitude = (string) $array['latitude'];
}
if (array_key_exists('longitude', $array)) {
$this->longitude = (string) $array['longitude'];
}
if (array_key_exists('time_zone', $array)) {
$this->timezone = (string) $array['time_zone'];
}
$this->countryCode = $info->countryCode();
$this->countryName = $info->countryName();
$this->regionName = $info->regionName();
$this->cityName = $info->city();
$this->latitude = (string) $info->latitude();
$this->longitude = (string) $info->longitude();
$this->timezone = $info->timeZone();
}
public function jsonSerialize(): array

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Service;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Common\IpGeolocation\Model\Location;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
@@ -19,7 +20,7 @@ class VisitService implements VisitServiceInterface
$this->em = $em;
}
public function locateVisits(callable $getGeolocationData, ?callable $locatedVisit = null): void
public function locateVisits(callable $geolocateVisit, ?callable $notifyVisitWithLocation = null): void
{
/** @var VisitRepository $repo */
$repo = $this->em->getRepository(Visit::class);
@@ -27,26 +28,27 @@ class VisitService implements VisitServiceInterface
foreach ($results as [$visit]) {
try {
$locationData = $getGeolocationData($visit);
/** @var Location $location */
$location = $geolocateVisit($visit);
} catch (IpCannotBeLocatedException $e) {
// Skip if the visit's IP could not be located
continue;
}
$location = new VisitLocation($locationData);
$this->locateVisit($visit, $location, $locatedVisit);
$location = new VisitLocation($location);
$this->locateVisit($visit, $location, $notifyVisitWithLocation);
}
}
private function locateVisit(Visit $visit, VisitLocation $location, ?callable $locatedVisit): void
private function locateVisit(Visit $visit, VisitLocation $location, ?callable $notifyVisitWithLocation): void
{
$visit->locate($location);
$this->em->persist($visit);
$this->em->flush();
if ($locatedVisit !== null) {
$locatedVisit($location, $visit);
if ($notifyVisitWithLocation !== null) {
$notifyVisitWithLocation($location, $visit);
}
$this->em->clear();

View File

@@ -5,5 +5,5 @@ namespace Shlinkio\Shlink\Core\Service;
interface VisitServiceInterface
{
public function locateVisits(callable $getGeolocationData, ?callable $locatedVisit = null): void;
public function locateVisits(callable $geolocateVisit, ?callable $notifyVisitWithLocation = null): void;
}

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Repository;
use Cake\Chronos\Chronos;
use Shlinkio\Shlink\Common\IpGeolocation\Model\Location;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit;
@@ -29,10 +30,8 @@ class VisitRepositoryTest extends DatabaseTestCase
$this->repo = $this->getEntityManager()->getRepository(Visit::class);
}
/**
* @test
*/
public function findUnlocatedVisitsReturnsProperVisits()
/** @test */
public function findUnlocatedVisitsReturnsProperVisits(): void
{
$shortUrl = new ShortUrl('');
$this->getEntityManager()->persist($shortUrl);
@@ -41,7 +40,7 @@ class VisitRepositoryTest extends DatabaseTestCase
$visit = new Visit($shortUrl, Visitor::emptyInstance());
if ($i % 2 === 0) {
$location = new VisitLocation([]);
$location = new VisitLocation(Location::emptyInstance());
$this->getEntityManager()->persist($location);
$visit->locate($location);
}
@@ -59,10 +58,8 @@ class VisitRepositoryTest extends DatabaseTestCase
$this->assertEquals(3, $resultsCount);
}
/**
* @test
*/
public function findVisitsByShortCodeReturnsProperData()
/** @test */
public function findVisitsByShortCodeReturnsProperData(): void
{
$shortUrl = new ShortUrl('');
$this->getEntityManager()->persist($shortUrl);
@@ -86,10 +83,8 @@ class VisitRepositoryTest extends DatabaseTestCase
$this->assertCount(2, $this->repo->findVisitsByShortCode($shortUrl->getShortCode(), null, 5, 4));
}
/**
* @test
*/
public function countVisitsByShortCodeReturnsProperData()
/** @test */
public function countVisitsByShortCodeReturnsProperData(): void
{
$shortUrl = new ShortUrl('');
$this->getEntityManager()->persist($shortUrl);

View File

@@ -4,20 +4,15 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Entity;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Common\IpGeolocation\Model\Location;
use Shlinkio\Shlink\Core\Entity\VisitLocation;
class VisitLocationTest extends TestCase
{
/**
* @test
*/
public function valuesFoundWhenExchangingArrayAreCastToString()
/** @test */
public function valuesFoundWhenExchangingArrayAreCastToString(): void
{
$payload = [
'latitude' => 1000.7,
'longitude' => -2000.4,
];
$payload = new Location('', '', '', '', 1000.7, -2000.4, '');
$location = new VisitLocation($payload);
$this->assertSame('1000.7', $location->getLatitude());

View File

@@ -7,6 +7,7 @@ use Doctrine\ORM\EntityManager;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Common\IpGeolocation\Model\Location;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Entity\VisitLocation;
@@ -31,10 +32,8 @@ class VisitServiceTest extends TestCase
$this->visitService = new VisitService($this->em->reveal());
}
/**
* @test
*/
public function locateVisitsIteratesAndLocatesUnlocatedVisits()
/** @test */
public function locateVisitsIteratesAndLocatesUnlocatedVisits(): void
{
$unlocatedVisits = [
[new Visit(new ShortUrl('foo'), Visitor::emptyInstance())],
@@ -53,7 +52,7 @@ class VisitServiceTest extends TestCase
});
$this->visitService->locateVisits(function () {
return [];
return Location::emptyInstance();
}, function () {
$args = func_get_args();