Created new command to list short urls

This commit is contained in:
Alejandro Celaya 2016-07-06 19:41:24 +02:00
parent 96478f3400
commit 60f5e5290e
3 changed files with 74 additions and 0 deletions

View file

@ -7,6 +7,7 @@ return [
'commands' => [
Command\GenerateShortcodeCommand::class,
Command\ResolveUrlCommand::class,
Command\ListShortcodesCommand::class,
]
],

View file

@ -46,6 +46,7 @@ return [
// Cli commands
CLI\Command\GenerateShortcodeCommand::class => AnnotatedFactory::class,
CLI\Command\ResolveUrlCommand::class => AnnotatedFactory::class,
CLI\Command\ListShortcodesCommand::class => AnnotatedFactory::class,
// Middleware
Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class,

View file

@ -0,0 +1,72 @@
<?php
namespace Acelaya\UrlShortener\CLI\Command;
use Acelaya\UrlShortener\Paginator\Adapter\PaginableRepositoryAdapter;
use Acelaya\UrlShortener\Service\ShortUrlService;
use Acelaya\UrlShortener\Service\ShortUrlServiceInterface;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
class ListShortcodesCommand extends Command
{
/**
* @var ShortUrlServiceInterface
*/
private $shortUrlService;
/**
* ListShortcodesCommand constructor.
* @param ShortUrlServiceInterface|ShortUrlService $shortUrlService
*
* @Inject({ShortUrlService::class})
*/
public function __construct(ShortUrlServiceInterface $shortUrlService)
{
parent::__construct(null);
$this->shortUrlService = $shortUrlService;
}
public function configure()
{
$this->setName('shortcode:list')
->setDescription('List all short URLs')
->addArgument(
'page',
InputArgument::OPTIONAL,
sprintf('The first page to list (%s items per page)', PaginableRepositoryAdapter::ITEMS_PER_PAGE),
1
);
}
public function execute(InputInterface $input, OutputInterface $output)
{
$page = intval($input->getArgument('page'));
/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
do {
$result = $this->shortUrlService->listShortUrls($page);
$page++;
$table = new Table($output);
$table->setHeaders([
'Short code',
'Original URL',
'Date created',
'Visits count',
]);
foreach ($result as $row) {
$table->addRow(array_values($row->jsonSerialize()));
}
$table->render();
$question = new ConfirmationQuestion('<question>Continue with next page? (y/N)</question> ', false);
} while ($helper->ask($input, $output, $question));
}
}