Added option to filter by date in shortcode:views CLI command

This commit is contained in:
Alejandro Celaya 2016-07-21 09:58:33 +02:00
parent 0a57f52309
commit 45d194aced

View File

@ -2,6 +2,7 @@
namespace Shlinkio\Shlink\CLI\Command; namespace Shlinkio\Shlink\CLI\Command;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject; use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Service\VisitsTracker; use Shlinkio\Shlink\Core\Service\VisitsTracker;
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface; use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
@ -9,6 +10,7 @@ use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Question\Question;
@ -35,7 +37,19 @@ class GetVisitsCommand extends Command
{ {
$this->setName('shortcode:visits') $this->setName('shortcode:visits')
->setDescription('Returns the detailed visits information for provided short code') ->setDescription('Returns the detailed visits information for provided short code')
->addArgument('shortCode', InputArgument::REQUIRED, 'The short code which visits we want to get'); ->addArgument('shortCode', InputArgument::REQUIRED, 'The short code which visits we want to get')
->addOption(
'startDate',
's',
InputOption::VALUE_OPTIONAL,
'Allows to filter visits, returning only those older than start date'
)
->addOption(
'endDate',
'e',
InputOption::VALUE_OPTIONAL,
'Allows to filter visits, returning only those newer than end date'
);
} }
public function interact(InputInterface $input, OutputInterface $output) public function interact(InputInterface $input, OutputInterface $output)
@ -60,7 +74,10 @@ class GetVisitsCommand extends Command
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
{ {
$shortCode = $input->getArgument('shortCode'); $shortCode = $input->getArgument('shortCode');
$visits = $this->visitsTracker->info($shortCode); $startDate = $this->getDateOption($input, 'startDate');
$endDate = $this->getDateOption($input, 'endDate');
$visits = $this->visitsTracker->info($shortCode, new DateRange($startDate, $endDate));
$table = new Table($output); $table = new Table($output);
$table->setHeaders([ $table->setHeaders([
'Referer', 'Referer',
@ -78,4 +95,14 @@ class GetVisitsCommand extends Command
} }
$table->render(); $table->render();
} }
protected function getDateOption(InputInterface $input, $key)
{
$value = $input->getOption($key);
if (isset($value)) {
$value = new \DateTime($value);
}
return $value;
}
} }