*/ 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::REQUIRED, 'List of domains separated by commas'); } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $this->input = $input; $this->output = $output; $this->checkDomains(); $table = new Table($output); $table->setHeaders(['Domain', 'Date']); foreach ($this->sort($this->successes) as $result) { $table->addRow([ $result['domain'], $this->createDateRender($result['expiryDate']), ]); } foreach ($this->sort($this->fails) as $result) { $table->addRow([ $result['domain'], $result['expiryDate'], ]); } $table->render(); } /** * Extracts domains. * * @return array */ protected function getDomains():array { $domains = explode(',', $this->input->getArgument('domains')); $domains = array_map('trim', $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(); return [ 'domain' => $domain, 'expiryDate' => $expiryDate, 'comparaison' => $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['comparaison'] > $b['comparaison']) { return 1; } if ($a['comparaison'] === $b['comparaison']) { 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')); } }