visitsTracker = $visitsTracker; $this->translator = $translator; parent::__construct(null); } public function configure() { $this->setName('shortcode:visits') ->setDescription( $this->translator->translate('Returns the detailed visits information for provided short code') ) ->addArgument( 'shortCode', InputArgument::REQUIRED, $this->translator->translate('The short code which visits we want to get') ) ->addOption( 'startDate', 's', InputOption::VALUE_OPTIONAL, $this->translator->translate('Allows to filter visits, returning only those older than start date') ) ->addOption( 'endDate', 'e', InputOption::VALUE_OPTIONAL, $this->translator->translate('Allows to filter visits, returning only those newer than end date') ); } 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(sprintf( '%s ', $this->translator->translate('A short code was not provided. Which short code do you want to use?:') )); $shortCode = $helper->ask($input, $output, $question); if (! empty($shortCode)) { $input->setArgument('shortCode', $shortCode); } } public function execute(InputInterface $input, OutputInterface $output) { $shortCode = $input->getArgument('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([ $this->translator->translate('Referer'), $this->translator->translate('Date'), $this->translator->translate('Remote Address'), $this->translator->translate('User agent'), ]); foreach ($visits as $row) { $rowData = $row->jsonSerialize(); // Unset location info unset($rowData['visitLocation']); $table->addRow(array_values($rowData)); } $table->render(); } protected function getDateOption(InputInterface $input, $key) { $value = $input->getOption($key); if (isset($value)) { $value = new \DateTime($value); } return $value; } }