Passed
Push — master ( 725e32...761ac2 )
by Nicolas
01:39
created

NatCreateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.9332
1
<?php
2
namespace Devgiants\Command;
3
4
use Buzz\Message\Request;
5
use Devgiants\Model\ApplicationCommand;
6
use Devgiants\Model\NatRule;
7
use Devgiants\Configuration\ConfigurationManager;
8
use Devgiants\Configuration\ApplicationConfiguration as AppConf;
9
use Pimple\Container;
10
use Symfony\Component\Console\Exception\InvalidOptionException;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class NatCreateCommand extends ApplicationCommand
16
{
17
18
	const ARGUMENT_ID = 'id';
19
	const ARGUMENT_PORT_INTERNAL = 'internal';
20
	const ARGUMENT_PORT_EXTERNAL = 'external';
21
	const ARGUMENT_IP = 'ip';
22
	const ARGUMENT_PROTOCOL = 'protocol';
23
24
	/**
25
	 * Wan command constructor.
26
	 *
27
	 * @param null|string $name
28
	 * @param Container $container
29
	 */
30
	public function __construct( $name, Container $container ) {
31
		parent::__construct( $name, $container );
32
	}
33
34
    /**
35
     * @inheritdoc
36
     */
37
    protected function configure()
38
    {
39
        $this
40
            ->setName('nat:create')
41
            ->setDescription('Create NAT entry')
42
			->addArgument( static::ARGUMENT_ID, InputArgument::REQUIRED, 'Id/Name of the NAT rule' )
43
			->addArgument( static::ARGUMENT_IP, InputArgument::REQUIRED, 'IP of the NAT rule' )
44
			->addArgument( static::ARGUMENT_PORT_EXTERNAL, InputArgument::REQUIRED, 'Internal port of the NAT rule' )
45
			->addArgument( static::ARGUMENT_PORT_INTERNAL, InputArgument::REQUIRED, 'External port of the NAT rule' )
46
			->addArgument( static::ARGUMENT_PROTOCOL, InputArgument::OPTIONAL, 'Protocol of the NAT rule: tcp, udp, both' )
47
            ->setHelp("This command allows you to add livebox NAT entry to open port")
48
        ;
49
50
        parent::configure();
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58
59
	    $ymlFile = $this->getConfigurationFile( $input );
60
61
	    if ( $ymlFile !== NULL && is_file( $ymlFile ) ) {
62
63
		    // Structures check and configuration loading
64
		    $configurationManager = new ConfigurationManager( $ymlFile );
65
		    $configuration        = $configurationManager->load();
66
67
			// Authentication
68
			$this->tools->authenticate(
69
				$configuration[ AppConf::HOST[ AppConf::NODE_NAME ] ],
70
				$configuration[ AppConf::USER[ AppConf::NODE_NAME ] ],
71
				$configuration[ AppConf::PASSWORD ]
72
			);
73
74
			$ruleId = $input->getArgument( static::ARGUMENT_ID );
75
			$natRule = new NatRule();
76
			$natRule->setId($ruleId);
77
			$natRule->setDescription($ruleId);
78
			$natRule->setDestinationIPAddress($input->getArgument( static::ARGUMENT_IP ));
79
			$natRule->setExternalPort($input->getArgument( static::ARGUMENT_PORT_EXTERNAL ));
80
			$natRule->setInternalPort($input->getArgument( static::ARGUMENT_PORT_INTERNAL ));
81
82
			$protocolMapping = [
83
				'tcp' => NatRule::PROTOCOL_TCP,
84
				'udp' => NatRule::PROTOCOL_UDP,
85
				'both' => NatRule::PROTOCOL_BOTH_INTERNAL
86
			];
87
			$protocol = $input->getArgument( static::ARGUMENT_PROTOCOL );
88
			if (!empty($protocol)) {
89
				if (!in_array($protocol, array_keys($protocolMapping))) {
90
					$stringProtocol = implode(' or ', array_map(function ($protocol) {
91
						return '"'.$protocol.'"';
92
					}, $protocolMapping));
93
					throw new InvalidOptionException( "Protocol argument get only ".$stringProtocol." value." );
94
				}
95
				$natRule->setProtocol($protocolMapping[$protocol]);
96
			}
97
98
99
			// Execute request
100
			$response = $this->tools->createRequest(
101
				Request::METHOD_POST,
102
				"{$configuration[ AppConf::HOST[ AppConf::NODE_NAME ] ]}/ws",
103
				[
104
					"service"    => "Firewall",
105
					"method"     => "setPortForwarding",
106
					"parameters" => $natRule->getOutput()
107
				]
108
			);
109
			$output->write($response->getContent() );
110
111
		    // Handle post command stuff
112
		    parent::execute( $input, $output );
113
	    }
114
    }
115
}