1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Cocotte\DigitalOcean; |
4
|
|
|
|
5
|
|
|
use Cocotte\Console\CommandBeforeInitializeEvent; |
6
|
|
|
use Cocotte\Console\CommandConfigureEvent; |
7
|
|
|
use Cocotte\Console\CommandEventStore; |
8
|
|
|
use Cocotte\Console\OptionProviderRegistry; |
9
|
|
|
use Cocotte\Shell\Env; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
12
|
|
|
|
13
|
|
|
final class SkipDnsValidation implements EventSubscriberInterface |
14
|
|
|
{ |
15
|
|
|
private const OPTION_NAME = 'skip-dns-validation'; |
16
|
|
|
public const SKIP_DNS_VALIDATION = 'SKIP_DNS_VALIDATION'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var OptionProviderRegistry |
20
|
|
|
*/ |
21
|
|
|
private $registry; |
22
|
|
|
/** |
23
|
|
|
* @var Env |
24
|
|
|
*/ |
25
|
|
|
private $env; |
26
|
|
|
|
27
|
|
|
public function __construct(OptionProviderRegistry $registry, Env $env) |
28
|
|
|
{ |
29
|
|
|
$this->registry = $registry; |
30
|
|
|
$this->env = $env; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static function getSubscribedEvents() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
CommandEventStore::COMMAND_CONFIGURE => 'onCommandConfigure', |
37
|
|
|
CommandEventStore::COMMAND_BEFORE_INITIALIZE => 'onCommandBeforeInitialize', |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function onCommandConfigure(CommandConfigureEvent $event) |
42
|
|
|
{ |
43
|
|
|
$command = $event->command(); |
44
|
|
|
|
45
|
|
|
if ($command instanceof DnsValidated) { |
46
|
|
|
$event->inputDefinition()->addOption($this->option()); |
47
|
|
|
} else { |
48
|
|
|
foreach ($command->optionProviders() as $className) { |
49
|
|
|
$optionProvider = $this->registry->providerByClassName($className); |
50
|
|
|
|
51
|
|
|
if ($optionProvider instanceof DnsValidated) { |
52
|
|
|
$event->inputDefinition()->addOption($this->option()); |
53
|
|
|
break; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function onCommandBeforeInitialize(CommandBeforeInitializeEvent $event) |
60
|
|
|
{ |
61
|
|
|
$input = $event->input(); |
62
|
|
|
$name = self::OPTION_NAME; |
63
|
|
|
|
64
|
|
|
if ($input->hasOption($name) && $input->getOption($name)) { |
65
|
|
|
$this->env->put(self::SKIP_DNS_VALIDATION, '1'); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function option(): InputOption |
70
|
|
|
{ |
71
|
|
|
return new InputOption( |
72
|
|
|
self::OPTION_NAME, |
73
|
|
|
null, |
74
|
|
|
InputOption::VALUE_NONE, |
75
|
|
|
"Cocotte uses a third-party library to validate that the name servers of your\n". |
76
|
|
|
"domain point to Digital Ocean. If you are confident that your name servers\n". |
77
|
|
|
"are correct, you can skip DNS validation." |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |