Passed
Push — master ( 2bca3d...467e42 )
by Christian
02:02
created

SkipDnsValidation   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 5 1
A onCommandBeforeInitialize() 0 7 3
A option() 0 9 1
A onCommandConfigure() 0 13 4
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
}