From 45d194acedb41c22f68283b93ee700db15c30d98 Mon Sep 17 00:00:00 2001
From: Alejandro Celaya <alejandro@alejandrocelaya.com>
Date: Thu, 21 Jul 2016 09:58:33 +0200
Subject: [PATCH] Added option to filter by date in shortcode:views CLI command

---
 module/CLI/src/Command/GetVisitsCommand.php | 31 +++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/module/CLI/src/Command/GetVisitsCommand.php b/module/CLI/src/Command/GetVisitsCommand.php
index 43d87de5..e7221e5e 100644
--- a/module/CLI/src/Command/GetVisitsCommand.php
+++ b/module/CLI/src/Command/GetVisitsCommand.php
@@ -2,6 +2,7 @@
 namespace Shlinkio\Shlink\CLI\Command;
 
 use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
+use Shlinkio\Shlink\Common\Util\DateRange;
 use Shlinkio\Shlink\Core\Service\VisitsTracker;
 use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
 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\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\Console\Question\Question;
 
@@ -35,7 +37,19 @@ class GetVisitsCommand extends Command
     {
         $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');
+            ->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)
@@ -60,7 +74,10 @@ class GetVisitsCommand extends Command
     public function execute(InputInterface $input, OutputInterface $output)
     {
         $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->setHeaders([
             'Referer',
@@ -78,4 +95,14 @@ class GetVisitsCommand extends Command
         }
         $table->render();
     }
+
+    protected function getDateOption(InputInterface $input, $key)
+    {
+        $value = $input->getOption($key);
+        if (isset($value)) {
+            $value = new \DateTime($value);
+        }
+
+        return $value;
+    }
 }