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

NatSwitchCommand::execute()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 33
c 2
b 0
f 0
nc 5
nop 2
dl 0
loc 57
rs 8.4586

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 statuc')
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
			$response = $this->getRunOutput(new ArrayInput([
78
				'command' => 'nat:infos',
79
			]));
80
81
			$json = json_decode($response);
82
83
			if (!empty($json->result) && !empty($json->result->status)) {
84
				$natRules = $json->result->status;
85
				$fullName = NatRule::ORIGIN . '_' . $ruleId;
86
				if (isset($natRules->$fullName)) {
87
					$natRule = NatRule::buildFrom($natRules->$fullName);
88
89
					$natRule->setEnable($status === 'enable');
90
91
					// Execute request
92
					$response = $this->tools->createRequest(
93
						Request::METHOD_POST,
94
						"{$configuration[ AppConf::HOST[ AppConf::NODE_NAME ] ]}/ws",
95
						[
96
							"service"    => "Firewall",
97
							"method"     => "setPortForwarding",
98
							"parameters" => $natRule->getOutput()
99
						]
100
					);
101
					$output->write($response->getContent() );
102
				} else {
103
					throw new InvalidOptionException( "Id argument invalid, can't find it in existing NAT rules." );
104
				}
105
			} else {
106
				throw new LogicException('Wrong format from nat:infos command');
0 ignored issues
show
Bug introduced by
The type Devgiants\Command\LogicException was not found. Did you mean LogicException? If so, make sure to prefix the type with \.
Loading history...
107
			}
108
		    // Handle post command stuff
109
		    parent::execute( $input, $output );
110
	    }
111
    }
112
}