Passed
Pull Request — master (#1)
by
unknown
03:32
created

NatDeleteCommand::execute()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 52
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 27
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 52
rs 8.8657

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 NatDeleteCommand extends ApplicationCommand
17
{
18
19
	const ARGUMENT_ID = 'id';
20
21
	/**
22
	 * Wan command constructor.
23
	 *
24
	 * @param null|string $name
25
	 * @param Container $container
26
	 */
27
	public function __construct( $name, Container $container ) {
28
		parent::__construct( $name, $container );
29
	}
30
31
    /**
32
     * @inheritdoc
33
     */
34
    protected function configure()
35
    {
36
        $this
37
            ->setName('nat:delete')
38
            ->setDescription('Delete NAT entry')
39
			->addArgument( static::ARGUMENT_ID, InputArgument::REQUIRED, 'Id of the NAT rule' )
40
            ->setHelp("This command allows you to remove livebox NAT entry")
41
        ;
42
43
        parent::configure();
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
52
	    $ymlFile = $this->getConfigurationFile( $input );
53
54
	    if ( $ymlFile !== NULL && is_file( $ymlFile ) ) {
55
56
		    // Structures check and configuration loading
57
		    $configurationManager = new ConfigurationManager( $ymlFile );
58
		    $configuration        = $configurationManager->load();
59
60
61
			// Authentication
62
			$this->tools->authenticate(
63
				$configuration[ AppConf::HOST[ AppConf::NODE_NAME ] ],
64
				$configuration[ AppConf::USER[ AppConf::NODE_NAME ] ],
65
				$configuration[ AppConf::PASSWORD ]
66
			);
67
68
			$ruleId = $input->getArgument( static::ARGUMENT_ID );
69
70
			$response = $this->getRunOutput(new ArrayInput([
71
				'command' => 'nat:infos',
72
			]));
73
74
			$json = json_decode($response);
75
76
			if (!empty($json->result) && !empty($json->result->status)) {
77
				$natRules = $json->result->status;
78
				$fullName = NatRule::ORIGIN . '_' . $ruleId;
79
				if (isset($natRules->$fullName)) {
80
					$natRule = NatRule::buildFrom($natRules->$fullName);
81
82
83
					// Execute request
84
					$response = $this->tools->createRequest(
85
						Request::METHOD_POST,
86
						"{$configuration[ AppConf::HOST[ AppConf::NODE_NAME ] ]}/ws",
87
						[
88
							"service"    => "Firewall",
89
							"method"     => "deletePortForwarding",
90
							"parameters" => $natRule->getOutputForDelete()
91
						]
92
					);
93
					$output->write($response->getContent() );
94
				} else {
95
					throw new InvalidOptionException( "Id argument invalid, can't find it in existing NAT rules." );
96
				}
97
			}
98
99
		    // Handle post command stuff
100
		    parent::execute( $input, $output );
101
	    }
102
    }
103
}