From 60f5e5290eb789376bc3f8b90c9c23be9f9460ae Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 6 Jul 2016 19:41:24 +0200 Subject: [PATCH] Created new command to list short urls --- config/autoload/cli.global.php | 1 + config/autoload/services.global.php | 1 + src/CLI/Command/ListShortcodesCommand.php | 72 +++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 src/CLI/Command/ListShortcodesCommand.php diff --git a/config/autoload/cli.global.php b/config/autoload/cli.global.php index f1a36b7d..1d5ea78f 100644 --- a/config/autoload/cli.global.php +++ b/config/autoload/cli.global.php @@ -7,6 +7,7 @@ return [ 'commands' => [ Command\GenerateShortcodeCommand::class, Command\ResolveUrlCommand::class, + Command\ListShortcodesCommand::class, ] ], diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php index 9f4447f4..1133642d 100644 --- a/config/autoload/services.global.php +++ b/config/autoload/services.global.php @@ -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, diff --git a/src/CLI/Command/ListShortcodesCommand.php b/src/CLI/Command/ListShortcodesCommand.php new file mode 100644 index 00000000..7df61588 --- /dev/null +++ b/src/CLI/Command/ListShortcodesCommand.php @@ -0,0 +1,72 @@ +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('Continue with next page? (y/N) ', false); + } while ($helper->ask($input, $output, $question)); + } +}