domain-expiration/src/Deblan/Command/CheckCommand.php
2019-04-25 13:17:40 +02:00

199 lines
4.5 KiB
PHP

<?php
namespace Deblan\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Command\Command as BaseCommand;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Process\Process;
use Deblan\Whois\Parser;
use Symfony\Component\Console\Input\InputOption;
/**
* class CheckCommand.
*
* @author Simon Vieille <simon@deblan.fr>
*/
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 output.')
->setHelp(<<<'EOF'
The <info>%command.name%</info> 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();
$table = new Table($output);
$table->setHeaders(['Domain', 'Date']);
if ($this->input->getOption('short')) {
$table->setStyle('compact');
}
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'],
'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();
return [
'domain' => $domain,
'expiryDate' => $expiryDate,
'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('<fg=%s>%s</>', $color, $date->format('Y-m-d H:i:s'));
}
}