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 . '');
+ }
+ }
+}