diff --git a/module/CLI/src/Command/Api/ListKeysCommand.php b/module/CLI/src/Command/Api/ListKeysCommand.php index 3eb96e96..42c8fb73 100644 --- a/module/CLI/src/Command/Api/ListKeysCommand.php +++ b/module/CLI/src/Command/Api/ListKeysCommand.php @@ -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', diff --git a/module/CLI/src/Command/ShortUrl/GetVisitsCommand.php b/module/CLI/src/Command/ShortUrl/GetVisitsCommand.php index 48c3a255..7c2989db 100644 --- a/module/CLI/src/Command/ShortUrl/GetVisitsCommand.php +++ b/module/CLI/src/Command/ShortUrl/GetVisitsCommand.php @@ -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) diff --git a/module/CLI/src/Command/Tag/ListTagsCommand.php b/module/CLI/src/Command/Tag/ListTagsCommand.php index f0aa66f8..4d2e05bb 100644 --- a/module/CLI/src/Command/Tag/ListTagsCommand.php +++ b/module/CLI/src/Command/Tag/ListTagsCommand.php @@ -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 diff --git a/module/Core/src/Entity/Visit.php b/module/Core/src/Entity/Visit.php index d7c44ff7..f8008f8c 100644 --- a/module/Core/src/Entity/Visit.php +++ b/module/Core/src/Entity/Visit.php @@ -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 diff --git a/module/Core/src/Entity/VisitLocation.php b/module/Core/src/Entity/VisitLocation.php index f9ee7250..472de2ee 100644 --- a/module/Core/src/Entity/VisitLocation.php +++ b/module/Core/src/Entity/VisitLocation.php @@ -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 diff --git a/module/Core/src/Visit/Model/UnknownVisitLocation.php b/module/Core/src/Visit/Model/UnknownVisitLocation.php new file mode 100644 index 00000000..118e38bd --- /dev/null +++ b/module/Core/src/Visit/Model/UnknownVisitLocation.php @@ -0,0 +1,47 @@ +json_encode, + * 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', + ]; + } +} diff --git a/module/Core/src/Visit/Model/VisitLocationInterface.php b/module/Core/src/Visit/Model/VisitLocationInterface.php new file mode 100644 index 00000000..83261334 --- /dev/null +++ b/module/Core/src/Visit/Model/VisitLocationInterface.php @@ -0,0 +1,17 @@ +