NatSwitchCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
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\ArrayInput;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
class NatSwitchCommand extends ApplicationCommand
17
{
18
	const ARGUMENT_STATUS = 'status';
19
	const STATUS_ENABLE = 'enable';
20
	const STATUS_DISABLE = 'disable';
21
22
	const ARGUMENT_ID = 'id';
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:switch')
41
            ->setDescription('Switch NAT entry status')
42
			->addArgument( static::ARGUMENT_STATUS, InputArgument::REQUIRED, 'Switch status : enable or disable' )
43
			->addArgument( static::ARGUMENT_ID, InputArgument::REQUIRED, 'Id of the NAT rule' )
44
            ->setHelp("This command allows you to enable/disable livebox NAT entry")
45
        ;
46
47
        parent::configure();
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
56
	    $ymlFile = $this->getConfigurationFile( $input );
57
58
	    if ( $ymlFile !== NULL && is_file( $ymlFile ) ) {
59
60
		    // Structures check and configuration loading
61
		    $configurationManager = new ConfigurationManager( $ymlFile );
62
		    $configuration        = $configurationManager->load();
63
64
			// Authentication
65
			$this->tools->authenticate(
66
				$configuration[ AppConf::HOST[ AppConf::NODE_NAME ] ],
67
				$configuration[ AppConf::USER[ AppConf::NODE_NAME ] ],
68
				$configuration[ AppConf::PASSWORD ]
69
			);
70
71
			$ruleId = $input->getArgument( static::ARGUMENT_ID );
72
			$status = $input->getArgument( static::ARGUMENT_STATUS );
73
			if (!in_array($status, [static::STATUS_ENABLE, static::STATUS_DISABLE])) {
74
				throw new InvalidOptionException( "Status argument get only \"enable\" or \"disable\" value." );
75
			}
76
77
            $commandInput = new ArrayInput([
78
                'command' => 'nat:infos',
79
                '--file' => $input->getOption('file')
80
            ]);
81
82
			$response = $this->runAnotherCommand($commandInput, $input);
83
			$json = json_decode($response);
84
85
			if (!empty($json->result) && !empty($json->result->status)) {
86
				$natRules = $json->result->status;
87
				$fullName = NatRule::ORIGIN . '_' . $ruleId;
88
				if (isset($natRules->$fullName)) {
89
					$natRule = NatRule::buildFrom($natRules->$fullName);
90
91
					$natRule->setEnable($status === 'enable');
92
93
					// Execute request
94
					$response = $this->tools->createRequest(
95
						Request::METHOD_POST,
96
						"{$configuration[ AppConf::HOST[ AppConf::NODE_NAME ] ]}/ws",
97
						[
98
							"service"    => "Firewall",
99
							"method"     => "setPortForwarding",
100
							"parameters" => $natRule->getOutput()
101
						]
102
					);
103
					$output->write($response->getContent() );
104
				} else {
105
					throw new InvalidOptionException( "Id argument invalid, can't find it in existing NAT rules." );
106
				}
107
			} else {
108
				throw new \LogicException('Wrong format from nat:infos command');
109
			}
110
		    // Handle post command stuff
111
		    parent::execute( $input, $output );
112
	    }
113
    }
114
}