| Conditions | 7 |
| Paths | 5 |
| Total Lines | 57 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 | $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'); |
||
|
|
|||
| 107 | } |
||
| 108 | // Handle post command stuff |
||
| 109 | parent::execute( $input, $output ); |
||
| 110 | } |
||
| 112 | } |