*/ class CheckCommand extends BaseCommand { /** * @var array */ protected $successes = []; /** * @var array */ protected $fails = []; /** * @var InputInterface */ protected $input; /** * @var OutputInterface */ protected $output; /** * {@inheritdoc} */ protected function configure() { $this ->setName('check') ->addArgument('domains', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'List of domains') ->addOption('short', 's', InputOption::VALUE_NONE, 'Simplify the table output') ->addOption('no-headers', null, InputOption::VALUE_NONE, 'Remove the table headers') ->addOption('json', 'j', InputOption::VALUE_NONE, 'Select json as output') ->addOption('table', 't', InputOption::VALUE_NONE, 'Select table as output') ->setHelp(<<<'EOF' The %command.name% retrieves the expiration dates of the given domains. Example: %command.full_name% example.com other-example.com EOF ); } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $this->input = $input; $this->output = $output; $this->checkDomains(); $short = $this->input->getOption('short'); $json = $this->input->getOption('json'); $table = $this->input->getOption('table') || !$json; $noHeaders = $this->input->getOption('no-headers'); $successes = $this->sort($this->successes); $fails = $this->sort($this->fails); if ($json) { return $this->output->write(json_encode(array_merge($successes, $fails))); } if ($table) { $this->renderTable($successes, $fails, $short, $noHeaders); } } /** * Render a Json. * * @param array $successes * @param array $fails */ protected function renderJson(array $successes, array $fails):void { $data = array_merge($successes, $fails); $json = json_encode($data); $this->output->write($json); } /** * Renders a Table. * * @param array $successes * @param array $fails * @param bool $short * @param bool $noHeader */ protected function renderTable(array $successes, array $fails, bool $short, bool $noHeaders):void { $table = new Table($this->output); if (!$noHeaders) { $table->setHeaders(['Domain', 'Days', 'Date']); } if ($short) { $table->setStyle('compact'); } foreach ($successes as $result) { $table->addRow([ $result['domain'], $result['dayUntilExpiry'], $this->createDateRender($result['expiryDate']), ]); } foreach ($fails as $result) { $table->addRow([ $result['domain'], $result['dayUntilExpiry'], 'FAIL', ]); } $table->render(); } /** * Extracts domains. * * @return array */ protected function getDomains():array { $inputDomains = $this->input->getArgument('domains'); $domains = []; foreach ($inputDomains as $inputDomain) { $value = explode(',', $inputDomain); $value = array_map('trim', $value); $domains = array_merge($value, $domains); } return $domains; } /** * Checks domains. */ protected function checkDomains():void { foreach ($this->getDomains() as $domain) { $data = $this->checkDomain($domain); if ($data['expiryDate'] === null) { $this->fails[] = $data; } else { $this->successes[] = $data; } } } /** * Checks domain. * * @return array */ protected function checkDomain($domain):array { $process = new Process(['whois', $domain]); $process->run(); $whois = $process->getOutput(); $parser = new Parser($whois); $expiryDate = $parser->getExpiryDate(); if ($expiryDate) { $comparison = $expiryDate->getTimestamp(); $dayUntilExpiry = floor(($expiryDate->getTimestamp() - time()) / 3600 / 24); } else { $comparison = 'FAIL'; $dayUntilExpiry = null; } return [ 'domain' => $domain, 'expiryDate' => $expiryDate, 'dayUntilExpiry' => $dayUntilExpiry, 'comparison' => $expiryDate ? $expiryDate->getTimestamp() : 'FAIL', ]; } /** * Sorts by expiry date and domain. * * @param array $data * * @return array */ protected function sort(array $data):array { usort($data, function ($a, $b) { if ($a['comparison'] > $b['comparison']) { return 1; } if ($a['comparison'] === $b['comparison']) { if ($a['domain'] > $b['domain']) { return 1; } return -1; } return 0; }); return $data; } /** * Creates date render for the table. * * @param \DateTime $date * * @return string */ protected function createDateRender(\DateTime $date):string { $timestamp = $date->getTimestamp(); if ($timestamp - time() < 3600 * 24 * 14) { $color = 'red'; } elseif ($timestamp - time() < 3600 * 24 * 30) { $color = 'yellow'; } else { $color = 'green'; } return sprintf('%s', $color, $date->format('Y-m-d H:i:s')); } }