Ensured default domain is stripped when creating short URLs from CLI

This commit is contained in:
Alejandro Celaya
2021-12-09 10:24:58 +01:00
parent f8a48c16f0
commit cbd4b4849f
3 changed files with 59 additions and 3 deletions

View File

@@ -30,10 +30,13 @@ class CreateShortUrlCommand extends BaseCommand
{
public const NAME = 'short-url:create';
private ?SymfonyStyle $io;
public function __construct(
private UrlShortenerInterface $urlShortener,
private ShortUrlStringifierInterface $stringifier,
private int $defaultShortCodeLength,
private string $defaultDomain,
) {
parent::__construct();
}
@@ -123,21 +126,33 @@ class CreateShortUrlCommand extends BaseCommand
protected function interact(InputInterface $input, OutputInterface $output): void
{
$io = new SymfonyStyle($input, $output);
$this->verifyLongUrlArgument($input, $output);
$this->verifyDomainArgument($input);
}
private function verifyLongUrlArgument(InputInterface $input, OutputInterface $output): void
{
$longUrl = $input->getArgument('longUrl');
if (! empty($longUrl)) {
return;
}
$io = $this->getIO($input, $output);
$longUrl = $io->ask('Which URL do you want to shorten?');
if (! empty($longUrl)) {
$input->setArgument('longUrl', $longUrl);
}
}
private function verifyDomainArgument(InputInterface $input): void
{
$domain = $input->getOption('domain');
$input->setOption('domain', $domain === $this->defaultDomain ? null : $domain);
}
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$io = new SymfonyStyle($input, $output);
$io = $this->getIO($input, $output);
$longUrl = $input->getArgument('longUrl');
if (empty($longUrl)) {
$io->error('A URL was not provided!');
@@ -197,4 +212,9 @@ class CreateShortUrlCommand extends BaseCommand
return null;
}
private function getIO(InputInterface $input, OutputInterface $output): SymfonyStyle
{
return $this->io ?? ($this->io = new SymfonyStyle($input, $output));
}
}