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 (#1904)
by Anton
02:17
created

TaskCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 39
ccs 0
cts 39
cp 0
crap 2
rs 9.296
c 0
b 0
f 0
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 Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
15
use Symfony\Component\Console\Input\InputInterface as Input;
16
use Symfony\Component\Console\Input\InputOption as Option;
17
use Symfony\Component\Console\Output\OutputInterface as Output;
18
19
class TaskCommand extends Command
20
{
21
    protected $deployer;
22
23
    public function __construct(string $name, ?string $description, Deployer $deployer)
24
    {
25
        parent::__construct($name);
26
        if ($description) {
27
            $this->setDescription($description);
28
        }
29
        $this->deployer = $deployer;
30
    }
31
32
    protected function configure()
33
    {
34
        $this->addOption(
35
            'hosts',
36
            null,
37
            Option::VALUE_REQUIRED,
38
            'Hosts to deploy, comma separated, supports ranges [:]'
39
        );
40
        $this->addOption(
41
            'roles',
42
            null,
43
            Option::VALUE_REQUIRED,
44
            'Roles to deploy'
45
        );
46
        $this->addOption(
47
            'limit',
48
            'l',
49
            Option::VALUE_REQUIRED,
50
            'How many host to run in parallel?'
51
        );
52
        $this->addOption(
53
            'no-hooks',
54
            null,
55
            Option::VALUE_NONE,
56
            'Run task without after/before hooks'
57
        );
58
        $this->addOption(
59
            'log',
60
            null,
61
            Option::VALUE_REQUIRED,
62
            'Log to file'
63
        );
64
        $this->addOption(
65
            'option',
66
            'o',
67
            Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY,
68
            'Sets configuration option'
69
        );
70
    }
71
72
    protected function execute(Input $input, Output $output)
73
    {
74
        $output->getFormatter()->setStyle('success', new OutputFormatterStyle('green'));
75
        if (!$output->isDecorated()) {
76
            define('NO_ANSI', 'true');
77
        }
78
79
        $roles = $input->getOption('roles');
80
        $hosts = $input->getOption('hosts');
81
        $logFile = $input->getOption('log');
82
        $hooksEnabled = !$input->getOption('no-hooks');
83
84
        foreach ($this->deployer->console->getUserDefinition()->getOptions() as $option) {
85
            if (!empty($input->getOption($option->getName()))) {
86
                $this->deployer->config[$option->getName()] = $input->getOption($option->getName());
87
            }
88
        }
89
90
        $this->parseOptions($input->getOption('option'));
91
        $this->deployer->config['log_file'] = $logFile;
92
93
        if (empty($hosts) && empty($roles) && $this->deployer->config->has('default_roles')) {
94
            $roles = $this->deployer->config->get('default_roles');
95
        }
96
97
        $selectedHosts = $this->deployer->hostSelector->select($hosts, $roles);
98
        if (empty($selectedHosts)) {
99
            throw new Exception('No host selected');
100
        }
101
102
        $tasks = $this->deployer->scriptManager->getTasks(
103
            $this->getName(),
104
            $selectedHosts,
105
            $hooksEnabled
106
        );
107
        if (empty($tasks)) {
108
            throw new Exception('No task will be executed, because the selected hosts do not meet the conditions of the tasks');
109
        }
110
111
        $exitCode = $this->deployer->executor->run($tasks, $selectedHosts);
112
        if ($exitCode === 0) {
113
            return 0;
114
        }
115
        if ($exitCode === GracefulShutdownException::EXIR_CODE) {
116
            return 1;
117
        }
118
119
        // Check if we have tasks to execute on failure.
120
        if ($this->deployer['fail']->has($this->getName())) {
121
            $taskName = $this->deployer['fail']->get($this->getName());
122
            $tasks = $this->deployer->scriptManager->getTasks(
123
                $taskName,
124
                $selectedHosts,
125
                $hooksEnabled
126
            );
127
            $this->deployer->executor->run($tasks, $selectedHosts);
128
        }
129
130
        return $exitCode;
131
    }
132
133
    private function parseOptions(array $options)
134
    {
135
        foreach ($options as $option) {
136
            list($name, $value) = explode('=', $option);
137
            $value = $this->castValueToPhpType($value);
138
            $this->deployer->config->set($name, $value);
139
        }
140
    }
141
142
    private function castValueToPhpType($value)
143
    {
144
        switch ($value) {
145
            case 'true':
146
                return true;
147
            case 'false':
148
                return false;
149
            default:
150
                return $value;
151
        }
152
    }
153
}
154