Passed
Push — master ( 956163...dedbc7 )
by Christian
03:02
created

NetworkingCommand::doExecute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\Command;
4
5
use Cocotte\Console\AbstractCommand;
6
use Cocotte\Console\Style;
7
use Cocotte\DigitalOcean\ApiToken;
8
use Cocotte\DigitalOcean\ApiTokenOptionProvider;
9
use Cocotte\DigitalOcean\HostnameCollection;
10
use Cocotte\DigitalOcean\NetworkingConfigurator;
11
use Cocotte\Environment\LazyEnvironment;
12
use Darsyn\IP\IP;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
19
final class NetworkingCommand extends AbstractCommand implements LazyEnvironment
20
{
21
    /**
22
     * @var NetworkingConfigurator
23
     */
24
    private $networkingConfigurator;
25
26
    /**
27
     * @var EventDispatcherInterface
28
     */
29
    private $eventDispatcher;
30
31
    /**
32
     * @var Style
33
     */
34
    private $style;
35
36
    public function __construct(
37
        NetworkingConfigurator $networkingConfigurator,
38
        EventDispatcherInterface $eventDispatcher,
39
        Style $style
40
    ) {
41
        $this->networkingConfigurator = $networkingConfigurator;
42
        $this->eventDispatcher = $eventDispatcher;
43
        $this->style = $style;
44
        parent::__construct();
45
    }
46
47
    public function lazyEnvironmentValues(): array
48
    {
49
        return [
50
            ApiToken::class,
51
        ];
52
    }
53
54
    public function optionProviders(): array
55
    {
56
        return [
57
            ApiTokenOptionProvider::class,
58
        ];
59
    }
60
61
    public function isHidden()
62
    {
63
        return !getenv('SHOW_HIDDEN_COMMANDS');
64
    }
65
66
    protected function eventDispatcher(): EventDispatcherInterface
67
    {
68
        return $this->eventDispatcher;
69
    }
70
71
    protected function doConfigure(): void
72
    {
73
        $this->setName('networking')
74
            ->setDescription('Configure networking of Digital Ocean')
75
            ->addArgument('hostnames', InputArgument::REQUIRED, 'Comma-separated list of hostnames')
76
            ->addOption('ip',
77
                null,
78
                InputOption::VALUE_REQUIRED,
79
                'IP to use for hostnames (required without the --remove option)')
80
            ->addOption('remove', null, InputOption::VALUE_NONE, 'Remove networking for hostnames');
81
    }
82
83
    protected function doExecute(InputInterface $input, OutputInterface $output)
84
    {
85
        $hostnames = HostnameCollection::fromString($input->getArgument('hostnames'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('hostnames') can also be of type null and string[]; however, parameter $string of Cocotte\DigitalOcean\Hos...ollection::fromString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
        $hostnames = HostnameCollection::fromString(/** @scrutinizer ignore-type */ $input->getArgument('hostnames'));
Loading history...
86
87
        if ($input->getOption('remove')) {
88
            $this->networkingConfigurator->remove($hostnames);
89
            $this->style->success("Networking successfully removed.");
90
        } else {
91
            $ip = new IP($input->getOption('ip'));
0 ignored issues
show
Bug introduced by
It seems like $input->getOption('ip') can also be of type string[]; however, parameter $ip of Darsyn\IP\IP::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
            $ip = new IP(/** @scrutinizer ignore-type */ $input->getOption('ip'));
Loading history...
92
            $this->networkingConfigurator->configure($hostnames, $ip);
93
            $this->style->success("Networking successfully configured.");
94
        }
95
    }
96
97
}