Passed
Push — master ( 1a588d...65a263 )
by Nils
02:38
created

ConfigCommand::execute()   C

Complexity

Conditions 12
Paths 18

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 30
c 2
b 0
f 0
nc 18
nop 2
dl 0
loc 50
rs 6.9666

How to fix   Complexity   

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
namespace Startwind\Inventorio\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\ArrayInput;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class ConfigCommand extends InventorioCommand
12
{
13
    protected static $defaultName = 'config';
14
    protected static $defaultDescription = 'Set features';
15
16
    protected function configure(): void
17
    {
18
        $this->addOption('remote', null, InputOption::VALUE_REQUIRED, 'Start remote command mode');
19
        $this->addOption('logfile', null, InputOption::VALUE_REQUIRED, 'Start logfile mode');
20
        // $this->addOption('show', null, InputOption::VALUE_REQUIRED, 'Show config');
21
22
        parent::configure();
23
    }
24
25
26
    /**
27
     * @inheritDoc
28
     */
29
    protected function execute(InputInterface $input, OutputInterface $output): int
30
    {
31
        $set = false;
32
33
        $this->initConfiguration($input->getOption('configFile'));
34
35
        if ($input->getOption('remote')) {
36
            $value = $input->getOption('remote');
37
38
            if ($value == 'on' || $value == 'true' || $value === true) {
39
                $value = true;
40
            } else {
41
                $value = false;
42
            }
43
44
            $output->writeln("");
45
            $output->writeln('Remote command mode: <info>' . ($value ? 'on' : 'off') . '</info>');
46
47
            $this->setRemoteEnabled($value);
48
49
            $set = true;
50
        }
51
52
        if ($input->getOption('logfile')) {
53
            $value = $input->getOption('logfile');
54
55
            if ($value == 'on' || $value == 'true' || $value === true) {
56
                $value = true;
57
            } else {
58
                $value = false;
59
            }
60
61
            $output->writeln("");
62
            $output->writeln('Logfile mode: <info>' . ($value ? 'on' : 'off') . '</info>');
63
64
            $this->setLogfileEnabled($value);
65
66
            $set = true;
67
        }
68
69
        if (!$set) {
70
            $output->writeln('<error>Configuration was not changed. Please provide at least one flag.</error>');
71
        } else {
72
            $output->writeln("");
73
            $this->getApplication()->find('collect')->run(new ArrayInput([]), $output);
74
            $output->writeln("");
75
            $output->writeln('If you are running inventorio via SystemD please call: <info>systemctl restart inventorio.service</info>');
76
        }
77
78
        return Command::SUCCESS;
79
    }
80
81
}
82