urlShortener = $urlShortener; $this->translator = $translator; parent::__construct(null); } public function configure() { $this->setName(self::NAME) ->setDescription($this->translator->translate('Returns the long URL behind a short code')) ->addArgument( 'shortCode', InputArgument::REQUIRED, $this->translator->translate('The short code to parse') ); } public function interact(InputInterface $input, OutputInterface $output) { $shortCode = $input->getArgument('shortCode'); if (! empty($shortCode)) { return; } /** @var QuestionHelper $helper */ $helper = $this->getHelper('question'); $question = new Question(sprintf( '%s ', $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)) { $input->setArgument('shortCode', $shortCode); } } public function execute(InputInterface $input, OutputInterface $output) { $shortCode = $input->getArgument('shortCode'); try { $longUrl = $this->urlShortener->shortCodeToUrl($shortCode); $output->writeln(sprintf('%s %s', $this->translator->translate('Long URL:'), $longUrl)); } catch (InvalidShortCodeException $e) { $output->writeln(sprintf('' . $this->translator->translate( 'Provided short code "%s" has an invalid format.' ) . '', $shortCode)); } catch (EntityDoesNotExistException $e) { $output->writeln(sprintf('' . $this->translator->translate( 'Provided short code "%s" could not be found.' ) . '', $shortCode)); } } }