Improved public API in VisitLocation entity, reducing anemic model

This commit is contained in:
Alejandro Celaya
2018-10-28 15:13:45 +01:00
parent 084b1169d7
commit e046eddda9
7 changed files with 41 additions and 93 deletions

View File

@@ -5,6 +5,7 @@ namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
use Cake\Chronos\Chronos;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
@@ -13,7 +14,8 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Zend\I18n\Translator\TranslatorInterface;
use function array_values;
use function array_map;
use function Shlinkio\Shlink\Common\pick;
class GetVisitsCommand extends Command
{
@@ -87,17 +89,11 @@ class GetVisitsCommand extends Command
$endDate = $this->getDateOption($input, 'endDate');
$visits = $this->visitsTracker->info($shortCode, new DateRange($startDate, $endDate));
$rows = [];
foreach ($visits as $row) {
$rowData = $row->jsonSerialize();
// Unset location info and remote addr
unset($rowData['visitLocation'], $rowData['remoteAddr']);
$rowData['country'] = $row->getVisitLocation()->getCountryName();
$rows[] = array_values($rowData);
}
$rows = array_map(function (Visit $visit) {
$rowData = $visit->jsonSerialize();
$rowData['country'] = $visit->getVisitLocation()->getCountryName();
return pick($rowData, ['referer', 'date', 'userAgent', 'country']);
}, $visits);
$io->table([
$this->translator->translate('Referer'),
$this->translator->translate('Date'),

View File

@@ -80,8 +80,7 @@ class ProcessVisitsCommand extends Command
try {
$result = $this->ipLocationResolver->resolveIpLocation($ipAddr);
$location = new VisitLocation();
$location->exchangeArray($result);
$location = new VisitLocation($result);
$visit->setVisitLocation($location);
$this->visitService->saveVisit($visit);

View File

@@ -80,7 +80,7 @@ class GetVisitsCommandTest extends TestCase
$shortCode = 'abc123';
$this->visitsTracker->info($shortCode, Argument::any())->willReturn([
(new Visit())->setReferer('foo')
->setVisitLocation((new VisitLocation())->setCountryName('Spain'))
->setVisitLocation(new VisitLocation(['country_name' => 'Spain']))
->setUserAgent('bar'),
])->shouldBeCalledTimes(1);