Created new CLI command to parse a shortcode

This commit is contained in:
Alejandro Celaya 2016-07-05 23:16:23 +02:00
parent 490b72539e
commit 9ce5e255f1
4 changed files with 84 additions and 1 deletions

View File

@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php
use Acelaya\UrlShortener\CliCommands\GenerateShortcodeCommand;
use Acelaya\UrlShortener\CliCommands\ResolveUrlCommand;
use Interop\Container\ContainerInterface;
use Symfony\Component\Console\Application as CliApp;
@ -10,5 +11,6 @@ $container = include __DIR__ . '/../config/container.php';
$app = new CliApp();
$app->addCommands([
$container->get(GenerateShortcodeCommand::class),
$container->get(ResolveUrlCommand::class),
]);
$app->run();

View File

@ -43,6 +43,7 @@ return [
// Cli commands
CliCommands\GenerateShortcodeCommand::class => AnnotatedFactory::class,
CliCommands\ResolveUrlCommand::class => AnnotatedFactory::class,
// Middleware
Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class,

View File

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

View File

@ -0,0 +1,80 @@
<?php
namespace Acelaya\UrlShortener\CliCommands;
use Acelaya\UrlShortener\Exception\InvalidShortCodeException;
use Acelaya\UrlShortener\Service\UrlShortener;
use Acelaya\UrlShortener\Service\UrlShortenerInterface;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
class ResolveUrlCommand extends Command
{
/**
* @var UrlShortenerInterface
*/
private $urlShortener;
/**
* ResolveUrlCommand constructor.
* @param UrlShortenerInterface|UrlShortener $urlShortener
*
* @Inject({UrlShortener::class})
*/
public function __construct(UrlShortenerInterface $urlShortener)
{
parent::__construct(null);
$this->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(
'<question>A short code was not provided. Which short code do you want to parse?:</question> '
);
$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('<error>No URL found for short code "%s"</error>', $shortCode));
return;
}
$output->writeln(sprintf('Long URL <info>%s</info>', $longUrl));
} catch (InvalidShortCodeException $e) {
$output->writeln(
sprintf('<error>Provided short code "%s" has an invalid format.</error>', $shortCode)
);
} catch (\Exception $e) {
$output->writeln('<error>' . $e . '</error>');
}
}
}