RunCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 7.53 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 57.41%

Importance

Changes 0
Metric Value
dl 7
loc 93
ccs 31
cts 54
cp 0.5741
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 32 1
B execute() 7 36 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Console;
9
10
use Deployer\Deployer;
11
use Deployer\Exception\Exception;
12
use function Deployer\run;
13
use Deployer\Task\Context;
14
use Deployer\Task\Task;
15
use function Deployer\write;
16
use function Deployer\writeln;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface as Input;
20
use Symfony\Component\Console\Input\InputOption as Option;
21
use Symfony\Component\Console\Output\OutputInterface as Output;
22
23
class RunCommand extends Command
24
{
25
    /**
26
     * @var Deployer
27
     */
28
    private $deployer;
29
30
    /**
31
     * @param Deployer $deployer
32
     */
33 17
    public function __construct(Deployer $deployer)
34
    {
35 17
        parent::__construct('run');
36 17
        $this->setDescription('Run any arbitrary command on hosts');
37 17
        $this->deployer = $deployer;
38 17
    }
39
40
    /**
41
     * Configures the command
42
     */
43 17
    protected function configure()
44
    {
45 17
        $this->addArgument(
46 17
            'command-to-run',
47 17
            InputArgument::REQUIRED,
48 17
            'Command to run'
49
        );
50 17
        $this->addOption(
51 17
            'log',
52 17
            null,
53 17
            Option::VALUE_REQUIRED,
54 17
            'Log to file'
55
        );
56 17
        $this->addOption(
57 17
            'stage',
58 17
            null,
59 17
            Option::VALUE_REQUIRED,
60 17
            'Stage to deploy'
61
        );
62 17
        $this->addOption(
63 17
            'roles',
64 17
            null,
65 17
            Option::VALUE_REQUIRED,
66 17
            'Roles to deploy'
67
        );
68 17
        $this->addOption(
69 17
            'hosts',
70 17
            null,
71 17
            Option::VALUE_REQUIRED,
72 17
            'Host to deploy, comma separated, supports ranges [:]'
73
        );
74 17
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    protected function execute(Input $input, Output $output)
80
    {
81
        $command = $input->getArgument('command-to-run');
82
        $stage = $input->getOption('stage');
83
        $roles = $input->getOption('roles');
84
        $hosts = $input->getOption('hosts');
85
86
        if (!empty($input->getOption('log'))) {
87
            $this->deployer->config['log_file'] = $input->getOption('log');
88
        }
89
90 View Code Duplication
        if (!empty($hosts)) {
91
            $hosts = $this->deployer->hostSelector->getByHostnames($hosts);
92
        } elseif (!empty($roles)) {
93
            $hosts = $this->deployer->hostSelector->getByRoles($roles);
94
        } else {
95
            $hosts = $this->deployer->hostSelector->getHosts($stage);
96
        }
97
98
        if (empty($hosts)) {
99
            throw new Exception('No host selected');
100
        }
101
102
        $task = new Task($command, function () use ($command, $hosts) {
103
            $output = run($command);
104
            if (count($hosts) > 1) {
105
                writeln("[{{hostname}}] > $output");
106
            } else {
107
                write($output);
108
            }
109
        });
110
111
        foreach ($hosts as $host) {
112
            $task->run(new Context($host, $input, $output));
113
        }
114
    }
115
}
116