GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Pull Request — master (#1470)
by Markus
03:01 queued 51s
created

TaskCommand   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 160
Duplicated Lines 4.38 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
dl 7
loc 160
ccs 75
cts 90
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 50 1
A parseOptions() 0 8 2
A castValueToPhpType() 0 11 3
C execute() 7 57 11

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 Deployer\Exception\GracefulShutdownException;
13
use Deployer\Executor\ExecutorInterface;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface as Input;
17
use Symfony\Component\Console\Input\InputOption as Option;
18
use Symfony\Component\Console\Output\OutputInterface as Output;
19
20
class TaskCommand extends Command
21
{
22
    /**
23
     * @var Deployer
24
     */
25
    private $deployer;
26
27
    /**
28
     * @var ExecutorInterface
29
     */
30
    public $executor;
31
32
    /**
33
     * @param string $name
34
     * @param string $description
35
     * @param Deployer $deployer
36
     */
37 7
    public function __construct($name, $description, Deployer $deployer)
38
    {
39 7
        parent::__construct($name);
40 7
        $this->setDescription($description);
41 7
        $this->deployer = $deployer;
42 7
    }
43
44
    /**
45
     * Configures the command
46
     */
47 7
    protected function configure()
48
    {
49 7
        $this->addArgument(
50 7
            'stage',
51 7
            InputArgument::OPTIONAL,
52 7
            'Stage or hostname'
53
        );
54 7
        $this->addOption(
55 7
            'parallel',
56 7
            'p',
57 7
            Option::VALUE_NONE,
58 7
            'Run tasks in parallel'
59
        );
60 7
        $this->addOption(
61 7
            'limit',
62 7
            'l',
63 7
            Option::VALUE_REQUIRED,
64 7
            'How many host to run in parallel?'
65
        );
66 7
        $this->addOption(
67 7
            'no-hooks',
68 7
            null,
69 7
            Option::VALUE_NONE,
70 7
            'Run task without after/before hooks'
71
        );
72 7
        $this->addOption(
73 7
            'log',
74 7
            null,
75 7
            Option::VALUE_REQUIRED,
76 7
            'Log to file'
77
        );
78 7
        $this->addOption(
79 7
            'roles',
80 7
            null,
81 7
            Option::VALUE_REQUIRED,
82 7
            'Roles to deploy'
83
        );
84 7
        $this->addOption(
85 7
            'hosts',
86 7
            null,
87 7
            Option::VALUE_REQUIRED,
88 7
            'Host to deploy, comma separated, supports ranges [:]'
89
        );
90 7
        $this->addOption(
91 7
            'option',
92 7
            'o',
93 7
            Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY,
94 7
            'Sets configuration option'
95
        );
96 7
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 7
    protected function execute(Input $input, Output $output)
102
    {
103 7
        $stage = $input->hasArgument('stage') ? $input->getArgument('stage') : null;
104 7
        $roles = $input->getOption('roles');
105 7
        $hosts = $input->getOption('hosts');
106 7
        $this->parseOptions($input->getOption('option'));
107
108 7
        $hooksEnabled = !$input->getOption('no-hooks');
109 7
        if (!empty($input->getOption('log'))) {
110
            $this->deployer->config['log_file'] = $input->getOption('log');
111
        }
112
113 7 View Code Duplication
        if (!empty($hosts)) {
114
            $hosts = $this->deployer->hostSelector->getByHostnames($hosts);
115 7
        } elseif (!empty($roles)) {
116
            $hosts = $this->deployer->hostSelector->getByRoles($roles);
117
        } else {
118 7
            $hosts = $this->deployer->hostSelector->getHosts($stage);
119
        }
120
121 7
        if (empty($hosts)) {
122
            throw new Exception('No host selected');
123
        }
124
125 7
        $tasks = $this->deployer->scriptManager->getTasks(
126 7
            $this->getName(),
127
            $hosts,
128
            $hooksEnabled
129
        );
130
131 7
        if (empty($tasks)) {
132
            throw new Exception('No task will be executed, because the selected hosts do not meet the conditions of the tasks');
133
        }
134
135 7
        if ($input->getOption('parallel')) {
136 1
            $executor = $this->deployer->parallelExecutor;
137
        } else {
138 6
            $executor = $this->deployer->seriesExecutor;
139
        }
140
141
        try {
142 7
            $executor->run($tasks, $hosts);
143
        } catch (\Throwable $exception) {
144
            if ($exception instanceof GracefulShutdownException) {
145
                throw $exception;
146
            } else {
147
                // Check if we have tasks to execute on failure
148
                if ($this->deployer['fail']->has($this->getName())) {
149
                    $taskName = $this->deployer['fail']->get($this->getName());
150
                    $tasks = $this->deployer->scriptManager->getTasks($taskName, $hosts, $hooksEnabled);
151
152
                    $executor->run($tasks, $hosts);
153
                }
154
                throw $exception;
155
            }
156
        }
157 7
    }
158
159 7
    private function parseOptions(array $options)
160
    {
161 7
        foreach ($options as $option) {
162 1
            list($name, $value) = explode('=', $option);
163 1
            $value = $this->castValueToPhpType($value);
164 1
            $this->deployer->config->set($name, $value);
165
        }
166 7
    }
167
168 1
    private function castValueToPhpType($value)
169
    {
170
        switch ($value) {
171 1
            case 'true':
172
                return true;
173 1
            case 'false':
174
                return false;
175
            default:
176 1
                return $value;
177
        }
178
    }
179
}
180