From 9ce5e255f1122250e07506936c6a22fb599ed13f Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 5 Jul 2016 23:16:23 +0200 Subject: [PATCH] Created new CLI command to parse a shortcode --- bin/cli | 2 + config/autoload/services.global.php | 1 + src/CliCommands/GenerateShortcodeCommand.php | 2 +- src/CliCommands/ResolveUrlCommand.php | 80 ++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/CliCommands/ResolveUrlCommand.php diff --git a/bin/cli b/bin/cli index 1816b80f..8a2983c1 100755 --- a/bin/cli +++ b/bin/cli @@ -1,6 +1,7 @@ #!/usr/bin/env php addCommands([ $container->get(GenerateShortcodeCommand::class), + $container->get(ResolveUrlCommand::class), ]); $app->run(); diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php index b08229e7..b3103ac3 100644 --- a/config/autoload/services.global.php +++ b/config/autoload/services.global.php @@ -43,6 +43,7 @@ return [ // Cli commands CliCommands\GenerateShortcodeCommand::class => AnnotatedFactory::class, + CliCommands\ResolveUrlCommand::class => AnnotatedFactory::class, // Middleware Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class, diff --git a/src/CliCommands/GenerateShortcodeCommand.php b/src/CliCommands/GenerateShortcodeCommand.php index 9f545dce..68b076c3 100644 --- a/src/CliCommands/GenerateShortcodeCommand.php +++ b/src/CliCommands/GenerateShortcodeCommand.php @@ -41,7 +41,7 @@ class GenerateShortcodeCommand extends Command public function configure() { - $this->setName('generate-shortcode') + $this->setName('shortcode:generate') ->setDescription('Generates a shortcode for provided URL and returns the short URL') ->addArgument('longUrl', InputArgument::REQUIRED, 'The long URL to parse'); } diff --git a/src/CliCommands/ResolveUrlCommand.php b/src/CliCommands/ResolveUrlCommand.php new file mode 100644 index 00000000..e10cd73e --- /dev/null +++ b/src/CliCommands/ResolveUrlCommand.php @@ -0,0 +1,80 @@ +urlShortener = $urlShortener; + } + + public function configure() + { + $this->setName('shortcode:parse') + ->setDescription('Returns the long URL behind a short code') + ->addArgument('shortCode', InputArgument::REQUIRED, '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( + '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); + if (! isset($longUrl)) { + $output->writeln(sprintf('No URL found for short code "%s"', $shortCode)); + return; + } + + $output->writeln(sprintf('Long URL %s', $longUrl)); + } catch (InvalidShortCodeException $e) { + $output->writeln( + sprintf('Provided short code "%s" has an invalid format.', $shortCode) + ); + } catch (\Exception $e) { + $output->writeln('' . $e . ''); + } + } +}