Used ShlinkTable on every location rendering a CLI table

This commit is contained in:
Alejandro Celaya 2018-12-08 12:12:11 +01:00
parent 46a35c553e
commit bb40d84212
7 changed files with 76 additions and 12 deletions

View file

@ -3,13 +3,13 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Api;
use Shlinkio\Shlink\Common\Console\ShlinkTable;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function array_filter;
use function array_map;
use function sprintf;
@ -46,7 +46,6 @@ class ListKeysCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): void
{
$io = new SymfonyStyle($input, $output);
$enabledOnly = $input->getOption('enabledOnly');
$rows = array_map(function (ApiKey $apiKey) use ($enabledOnly) {
@ -62,7 +61,7 @@ class ListKeysCommand extends Command
return $rowData;
}, $this->apiKeyService->listKeys($enabledOnly));
$io->table(array_filter([
ShlinkTable::fromOutput($output)->render(array_filter([
'Key',
! $enabledOnly ? 'Is enabled' : null,
'Expiration date',

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
use Cake\Chronos\Chronos;
use Shlinkio\Shlink\Common\Console\ShlinkTable;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Model\VisitsParams;
@ -69,7 +70,6 @@ class GetVisitsCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): void
{
$io = new SymfonyStyle($input, $output);
$shortCode = $input->getArgument('shortCode');
$startDate = $this->getDateOption($input, 'startDate');
$endDate = $this->getDateOption($input, 'endDate');
@ -82,7 +82,7 @@ class GetVisitsCommand extends Command
$rowData['country'] = $visit->getVisitLocation()->getCountryName();
return select_keys($rowData, ['referer', 'date', 'userAgent', 'country']);
}, $visits);
$io->table(['Referer', 'Date', 'User agent', 'Country'], $rows);
ShlinkTable::fromOutput($output)->render(['Referer', 'Date', 'User agent', 'Country'], $rows);
}
private function getDateOption(InputInterface $input, $key)

View file

@ -3,12 +3,12 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Tag;
use Shlinkio\Shlink\Common\Console\ShlinkTable;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function Functional\map;
class ListTagsCommand extends Command
@ -33,8 +33,7 @@ class ListTagsCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): void
{
$io = new SymfonyStyle($input, $output);
$io->table(['Name'], $this->getTagsRows());
ShlinkTable::fromOutput($output)->render(['Name'], $this->getTagsRows());
}
private function getTagsRows(): array

View file

@ -11,6 +11,8 @@ use Shlinkio\Shlink\Common\Exception\WrongIpException;
use Shlinkio\Shlink\Common\Util\IpAddress;
use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Repository\VisitRepository;
use Shlinkio\Shlink\Core\Visit\Model\UnknownVisitLocation;
use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
/**
* Class Visit
@ -88,9 +90,9 @@ class Visit extends AbstractEntity implements JsonSerializable
return ! empty($this->remoteAddr);
}
public function getVisitLocation(): VisitLocation
public function getVisitLocation(): VisitLocationInterface
{
return $this->visitLocation;
return $this->visitLocation ?? new UnknownVisitLocation();
}
public function locate(VisitLocation $visitLocation): self

View file

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Entity;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
use function array_key_exists;
/**
@ -16,7 +16,7 @@ use function array_key_exists;
* @ORM\Entity()
* @ORM\Table(name="visit_locations")
*/
class VisitLocation extends AbstractEntity implements JsonSerializable
class VisitLocation extends AbstractEntity implements VisitLocationInterface
{
/**
* @var string

View file

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Model;
final class UnknownVisitLocation implements VisitLocationInterface
{
public function getCountryName(): string
{
return 'Unknown';
}
public function getLatitude(): string
{
return '0.0';
}
public function getLongitude(): string
{
return '0.0';
}
public function getCityName(): string
{
return 'Unknown';
}
/**
* Specify data which should be serialized to JSON
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize()
{
return [
'countryCode' => 'Unknown',
'countryName' => 'Unknown',
'regionName' => 'Unknown',
'cityName' => 'Unknown',
'latitude' => '0.0',
'longitude' => '0.0',
'timezone' => 'Unknown',
];
}
}

View file

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Model;
use JsonSerializable;
interface VisitLocationInterface extends JsonSerializable
{
public function getCountryName(): string;
public function getLatitude(): string;
public function getLongitude(): string;
public function getCityName(): string;
}