WifiSwitchCommand::execute()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 57
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 32
c 2
b 0
f 0
nc 5
nop 2
dl 0
loc 57
rs 8.7857

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
/**
3
 * Created by PhpStorm.
4
 * User: nicolas
5
 * Date: 05/02/17
6
 * Time: 14:32
7
 */
8
9
namespace Devgiants\Command;
10
11
use Buzz\Message\Request;
12
use Devgiants\Configuration\ConfigurationManager;
13
use Devgiants\Configuration\ApplicationConfiguration as AppConf;
14
use Devgiants\Model\ApplicationCommand;
15
use Pimple\Container;
16
use Symfony\Component\Console\Exception\InvalidOptionException;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
class WifiSwitchCommand extends ApplicationCommand {
22
23
	const STATUS = 'status';
24
25
	const ON = 'on';
26
27
	const OFF = 'off';
28
29
	/**
30
	 * WifiSwitchCommand constructor.
31
	 *
32
	 * @param null|string $name
33
	 * @param Container $container
34
	 */
35
	public function __construct( $name, Container $container ) {
36
		parent::__construct( $name, $container );
37
	}
38
39
	/**
40
	 * @inheritdoc
41
	 */
42
	protected function configure() {
43
		$this
44
			->setName( 'wifi:switch' )
45
			->setDescription( 'Switch wifi on or off' )
46
			->addArgument( static::STATUS, InputArgument::OPTIONAL, "Switch wifi on or off" )
47
			->setHelp( "This command allows you to enable/disable Wifi" );
48
49
		parent::configure();
50
	}
51
52
	/**
53
	 * @inheritdoc
54
	 */
55
	protected function execute( InputInterface $input, OutputInterface $output ) {
56
57
		$ymlFile = $this->getConfigurationFile( $input );
58
59
		if ( $ymlFile !== NULL && is_file( $ymlFile ) ) {
60
61
			// Structures check and configuration loading
62
			$configurationManager = new ConfigurationManager( $ymlFile );
63
			$configuration        = $configurationManager->load();
64
65
			// Authentication
66
			$this->tools->authenticate(
67
				$configuration[ AppConf::HOST[ AppConf::NODE_NAME ] ],
68
				$configuration[ AppConf::USER[ AppConf::NODE_NAME ] ],
69
				$configuration[ AppConf::PASSWORD ]
70
			);
71
72
			// Get status
73
			if ( $input->hasArgument( static::STATUS ) ) {
74
75
				$status = $input->getArgument( static::STATUS );
76
				switch ( $status ) {
77
					case static::ON:
78
						$parameters = [
79
							"Enable" => "True",
80
						];
81
						break;
82
					case static::OFF:
83
						$parameters = [
84
							"Enable" => "False",
85
						];
86
						break;
87
					default:
88
						throw new InvalidOptionException( "Status argument get only \"on\" or \"off\" value." );
89
				}
90
91
				// Execute request
92
				$response = $this->tools->createRequest(
93
					Request::METHOD_POST,
94
					"{$configuration[ AppConf::HOST[ AppConf::NODE_NAME ] ]}/ws",
95
					[
96
						"service"    => "NMC.Wifi",
97
						"method"     => "set",
98
						"parameters" => $parameters,
99
					]
100
				);
101
102
				$output->write( $response->getContent() );
103
104
			}
105
106
107
			// Handle post command stuff
108
			parent::execute( $input, $output );
109
		} else {
110
			$output->writeln( "<error>Filename is not correct : {$ymlFile}</error>" );
111
			$this->log->addError( "Filename is not correct : {$ymlFile}" );
112
		}
113
114
	}
115
}