refactoring
This commit is contained in:
parent
7899c45545
commit
c13d3131bb
178
src/Deblan/Command/CheckCommand.php
Normal file
178
src/Deblan/Command/CheckCommand.php
Normal file
|
@ -0,0 +1,178 @@
|
|||
<?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\Parser\WhoisParser as Parser;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
/**
|
||||
* class CheckDomainsCommand.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
abstract 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
|
||||
->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('wait', 'w', InputOption::VALUE_REQUIRED, 'Wait between each whois (in second, default: 0)')
|
||||
->addOption('json', 'j', InputOption::VALUE_NONE, 'Select json as output')
|
||||
->addOption('table', 't', InputOption::VALUE_NONE, 'Select table as output (default)');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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'));
|
||||
}
|
||||
}
|
||||
|
|
@ -4,53 +4,26 @@ 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;
|
||||
use Deblan\Parser\WhoisParser as Parser;
|
||||
|
||||
/**
|
||||
* class CheckDomainsCommand.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class CheckDomainsCommand extends BaseCommand
|
||||
class CheckDomainsCommand extends CheckCommand
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $successes = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $fails = [];
|
||||
|
||||
/**
|
||||
* @var InputInterface
|
||||
*/
|
||||
protected $input;
|
||||
|
||||
/**
|
||||
* @var OutputInterface
|
||||
*/
|
||||
protected $output;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this
|
||||
->setName('domains')
|
||||
->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('wait', 'w', InputOption::VALUE_REQUIRED, 'Wait between each whois (in second, default: 0)')
|
||||
->addOption('json', 'j', InputOption::VALUE_NONE, 'Select json as output')
|
||||
->addOption('table', 't', InputOption::VALUE_NONE, 'Select table as output (default)')
|
||||
->setHelp(<<<'EOF'
|
||||
The <info>%command.name%</info> retrieves the expiration dates of the given domains.
|
||||
|
||||
|
@ -87,81 +60,10 @@ EOF
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param int $wait
|
||||
*/
|
||||
protected function checkDomains(int $wait):void
|
||||
{
|
||||
|
@ -186,6 +88,8 @@ EOF
|
|||
/**
|
||||
* Checks domain.
|
||||
*
|
||||
* @param mixed $domain
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function checkDomain($domain):array
|
||||
|
@ -212,54 +116,4 @@ EOF
|
|||
'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'));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ 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\SslCert\Parser;
|
||||
use Deblan\Parser\SslCertParser as Parser;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
/**
|
||||
|
@ -16,43 +16,19 @@ use Symfony\Component\Console\Input\InputOption;
|
|||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class CheckHttpsCertificatesCommand extends BaseCommand
|
||||
class CheckHttpsCertificatesCommand extends CheckCommand
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $successes = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $fails = [];
|
||||
|
||||
/**
|
||||
* @var InputInterface
|
||||
*/
|
||||
protected $input;
|
||||
|
||||
/**
|
||||
* @var OutputInterface
|
||||
*/
|
||||
protected $output;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this
|
||||
->setName('https-certificates')
|
||||
->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('wait', 'w', InputOption::VALUE_REQUIRED, 'Wait between each whois (in second, default: 0)')
|
||||
->addOption('json', 'j', InputOption::VALUE_NONE, 'Select json as output')
|
||||
->addOption('table', 't', InputOption::VALUE_NONE, 'Select table as output (default)')
|
||||
->setHelp(<<<'EOF'
|
||||
The <info>%command.name%</info> retrieves the expiration dates of the given domains.
|
||||
The <info>%command.name%</info> retrieves the expiration dates of the HTTPS certificates of domains.
|
||||
|
||||
Example: %command.full_name% example.com other-example.com
|
||||
EOF
|
||||
|
@ -88,80 +64,9 @@ EOF
|
|||
}
|
||||
|
||||
/**
|
||||
* Render a Json.
|
||||
* Check HTTPS Certificates.
|
||||
*
|
||||
* @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.
|
||||
* @param int $wait
|
||||
*/
|
||||
protected function checkHttpsCertificates(int $wait):void
|
||||
{
|
||||
|
@ -184,7 +89,9 @@ EOF
|
|||
}
|
||||
|
||||
/**
|
||||
* Checks domain.
|
||||
* Check HTTPS Certificate using a domain.
|
||||
*
|
||||
* @param mixed $domain
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
@ -216,54 +123,4 @@ EOF
|
|||
'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'));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Deblan\SslCert;
|
||||
namespace Deblan\Parser;
|
||||
|
||||
/**
|
||||
* class Parser.
|
||||
* class SslCertParser.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class Parser
|
||||
class SslCertParser
|
||||
{
|
||||
/**
|
||||
* @var string
|
|
@ -1,13 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Deblan\Whois;
|
||||
namespace Deblan\Parser;
|
||||
|
||||
/**
|
||||
* class Parser.
|
||||
* class WhoisParser.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class Parser
|
||||
class WhoisParser
|
||||
{
|
||||
/**
|
||||
* @var string
|
Loading…
Reference in a new issue