Created command to list visits for a shortcode

This commit is contained in:
Alejandro Celaya 2016-07-06 20:21:34 +02:00
parent 43f1f790dd
commit 2e00a8dec6
5 changed files with 79 additions and 4 deletions

View File

@ -8,6 +8,7 @@ return [
Command\GenerateShortcodeCommand::class,
Command\ResolveUrlCommand::class,
Command\ListShortcodesCommand::class,
Command\GetVisitsCommand::class,
]
],

View File

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

View File

@ -88,8 +88,6 @@ class GenerateShortcodeCommand extends Command
$output->writeln(
sprintf('<error>Provided URL "%s" is invalid. Try with a different one.</error>', $longUrl)
);
} catch (\Exception $e) {
$output->writeln('<error>' . $e . '</error>');
}
}
}

View File

@ -0,0 +1,77 @@
<?php
namespace Acelaya\UrlShortener\CLI\Command;
use Acelaya\UrlShortener\Service\VisitsTracker;
use Acelaya\UrlShortener\Service\VisitsTrackerInterface;
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\Question;
class GetVisitsCommand extends Command
{
/**
* @var VisitsTrackerInterface
*/
private $visitsTracker;
/**
* GetVisitsCommand constructor.
* @param VisitsTrackerInterface|VisitsTracker $visitsTracker
*
* @Inject({VisitsTracker::class})
*/
public function __construct(VisitsTrackerInterface $visitsTracker)
{
parent::__construct(null);
$this->visitsTracker = $visitsTracker;
}
public function configure()
{
$this->setName('shortcode:visits')
->setDescription('Returns the detailed visits information for provided short code')
->addArgument('shortCode', InputArgument::REQUIRED, 'The short code which visits we want to get');
}
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 use?:</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');
$visits = $this->visitsTracker->info($shortCode);
$table = new Table($output);
$table->setHeaders([
'Referer',
'Date',
'Temote Address',
'User agent',
]);
foreach ($visits as $row) {
$table->addRow(array_values($row->jsonSerialize()));
}
$table->render();
}
}

View File

@ -73,8 +73,6 @@ class ResolveUrlCommand extends Command
$output->writeln(
sprintf('<error>Provided short code "%s" has an invalid format.</error>', $shortCode)
);
} catch (\Exception $e) {
$output->writeln('<error>' . $e . '</error>');
}
}
}