| Conditions | 5 |
| Paths | 4 |
| Total Lines | 57 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 56 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 57 | { |
||
| 58 | |||
| 59 | $ymlFile = $this->getConfigurationFile( $input ); |
||
| 60 | |||
| 61 | if ( $ymlFile !== NULL && is_file( $ymlFile ) ) { |
||
| 62 | |||
| 63 | // Structures check and configuration loading |
||
| 64 | $configurationManager = new ConfigurationManager( $ymlFile ); |
||
| 65 | $configuration = $configurationManager->load(); |
||
| 66 | |||
| 67 | // Authentication |
||
| 68 | $this->tools->authenticate( |
||
| 69 | $configuration[ AppConf::HOST[ AppConf::NODE_NAME ] ], |
||
| 70 | $configuration[ AppConf::USER[ AppConf::NODE_NAME ] ], |
||
| 71 | $configuration[ AppConf::PASSWORD ] |
||
| 72 | ); |
||
| 73 | |||
| 74 | $ruleId = $input->getArgument( static::ARGUMENT_ID ); |
||
| 75 | $natRule = new NatRule(); |
||
| 76 | $natRule->setId($ruleId); |
||
| 77 | $natRule->setDescription($ruleId); |
||
| 78 | $natRule->setDestinationIPAddress($input->getArgument( static::ARGUMENT_IP )); |
||
| 79 | $natRule->setExternalPort($input->getArgument( static::ARGUMENT_PORT_EXTERNAL )); |
||
| 80 | $natRule->setInternalPort($input->getArgument( static::ARGUMENT_PORT_INTERNAL )); |
||
| 81 | |||
| 82 | $protocolMapping = [ |
||
| 83 | 'tcp' => NatRule::PROTOCOL_TCP, |
||
| 84 | 'udp' => NatRule::PROTOCOL_UDP, |
||
| 85 | 'both' => NatRule::PROTOCOL_BOTH_INTERNAL |
||
| 86 | ]; |
||
| 87 | $protocol = $input->getArgument( static::ARGUMENT_PROTOCOL ); |
||
| 88 | if (!empty($protocol)) { |
||
| 89 | if (!in_array($protocol, array_keys($protocolMapping))) { |
||
| 90 | $stringProtocol = implode(' or ', array_map(function ($protocol) { |
||
| 91 | return '"'.$protocol.'"'; |
||
| 92 | }, $protocolMapping)); |
||
| 93 | throw new InvalidOptionException( "Protocol argument get only ".$stringProtocol." value." ); |
||
| 94 | } |
||
| 95 | $natRule->setProtocol($protocolMapping[$protocol]); |
||
| 96 | } |
||
| 97 | |||
| 98 | |||
| 99 | // Execute request |
||
| 100 | $response = $this->tools->createRequest( |
||
| 101 | Request::METHOD_POST, |
||
| 102 | "{$configuration[ AppConf::HOST[ AppConf::NODE_NAME ] ]}/ws", |
||
| 103 | [ |
||
| 104 | "service" => "Firewall", |
||
| 105 | "method" => "setPortForwarding", |
||
| 106 | "parameters" => $natRule->getOutput() |
||
| 107 | ] |
||
| 108 | ); |
||
| 109 | $output->write($response->getContent() ); |
||
| 110 | |||
| 111 | // Handle post command stuff |
||
| 112 | parent::execute( $input, $output ); |
||
| 113 | } |
||
| 115 | } |