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

NatDeleteCommand::execute()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 54
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 29
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 54
rs 8.8337

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