Simplified and improved ResolveUrlCommand with SymfonyStyle

This commit is contained in:
Alejandro Celaya 2017-12-31 18:58:11 +01:00
parent 89ed84ce28
commit a60c45ca4d
2 changed files with 15 additions and 19 deletions

View file

@ -7,11 +7,10 @@ use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Style\SymfonyStyle;
use Zend\I18n\Translator\TranslatorInterface; use Zend\I18n\Translator\TranslatorInterface;
class ResolveUrlCommand extends Command class ResolveUrlCommand extends Command
@ -52,14 +51,10 @@ class ResolveUrlCommand extends Command
return; return;
} }
/** @var QuestionHelper $helper */ $io = new SymfonyStyle($input, $output);
$helper = $this->getHelper('question'); $shortCode = $io->ask(
$question = new Question(sprintf( $this->translator->translate('A short code was not provided. Which short code do you want to parse?')
'<question>%s</question> ', );
$this->translator->translate('A short code was not provided. Which short code do you want to parse?:')
));
$shortCode = $helper->ask($input, $output, $question);
if (! empty($shortCode)) { if (! empty($shortCode)) {
$input->setArgument('shortCode', $shortCode); $input->setArgument('shortCode', $shortCode);
} }
@ -67,19 +62,20 @@ class ResolveUrlCommand extends Command
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
{ {
$io = new SymfonyStyle($input, $output);
$shortCode = $input->getArgument('shortCode'); $shortCode = $input->getArgument('shortCode');
try { try {
$longUrl = $this->urlShortener->shortCodeToUrl($shortCode); $longUrl = $this->urlShortener->shortCodeToUrl($shortCode);
$output->writeln(sprintf('%s <info>%s</info>', $this->translator->translate('Long URL:'), $longUrl)); $output->writeln(\sprintf('%s <info>%s</info>', $this->translator->translate('Long URL:'), $longUrl));
} catch (InvalidShortCodeException $e) { } catch (InvalidShortCodeException $e) {
$output->writeln(sprintf('<error>' . $this->translator->translate( $io->error(
'Provided short code "%s" has an invalid format.' \sprintf($this->translator->translate('Provided short code "%s" has an invalid format.'), $shortCode)
) . '</error>', $shortCode)); );
} catch (EntityDoesNotExistException $e) { } catch (EntityDoesNotExistException $e) {
$output->writeln(sprintf('<error>' . $this->translator->translate( $io->error(
'Provided short code "%s" could not be found.' \sprintf($this->translator->translate('Provided short code "%s" could not be found.'), $shortCode)
) . '</error>', $shortCode)); );
} }
} }
} }

View file

@ -66,7 +66,7 @@ class ResolveUrlCommandTest extends TestCase
'shortCode' => $shortCode, 'shortCode' => $shortCode,
]); ]);
$output = $this->commandTester->getDisplay(); $output = $this->commandTester->getDisplay();
$this->assertEquals('Provided short code "' . $shortCode . '" could not be found.' . PHP_EOL, $output); $this->assertContains('Provided short code "' . $shortCode . '" could not be found.', $output);
} }
/** /**
@ -83,6 +83,6 @@ class ResolveUrlCommandTest extends TestCase
'shortCode' => $shortCode, 'shortCode' => $shortCode,
]); ]);
$output = $this->commandTester->getDisplay(); $output = $this->commandTester->getDisplay();
$this->assertEquals('Provided short code "' . $shortCode . '" has an invalid format.' . PHP_EOL, $output); $this->assertContains('Provided short code "' . $shortCode . '" has an invalid format.', $output);
} }
} }