Conditions | 7 |
Paths | 5 |
Total Lines | 59 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | } |
||
114 | } |