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.
Passed
Pull Request — master (#1878)
by
unknown
03:26 queued 01:13
created

TaskCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 56
ccs 46
cts 46
cp 1
crap 1
rs 8.9599
c 0
b 0
f 0

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