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
Push — master ( c538d5...8bac07 )
by Anton
02:32
created

MainCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 45

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