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