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
Push — master ( 287888...09a43c )
by Anton
02:18
created

SelectCommand::selectHosts()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
nc 12
nop 2
dl 0
loc 41
ccs 0
cts 36
cp 0
crap 42
rs 8.6417
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\Executor\Planner;
14
use Deployer\Host\Host;
15
use Deployer\Host\Localhost;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
18
use Symfony\Component\Console\Helper\QuestionHelper;
19
use Symfony\Component\Console\Helper\Table;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface as Input;
22
use Symfony\Component\Console\Input\InputOption as Option;
23
use Symfony\Component\Console\Output\OutputInterface as Output;
24
use Symfony\Component\Console\Question\ChoiceQuestion;
25
26
abstract class SelectCommand extends Command
27
{
28
    protected $deployer;
29
30
    public function __construct(string $name, Deployer $deployer)
31
    {
32
        $this->deployer = $deployer;
33
        parent::__construct($name);
34
    }
35
36
    protected function configure()
37
    {
38
        $this->addOption('selector', 's', Option::VALUE_OPTIONAL, 'Host selector');
39
40
        // Add global options defined with `option()` func.
41
        $this->getDefinition()->addOptions($this->deployer->inputDefinition->getOptions());
42
43
        $this->addOption(
44
            'option',
45
            'o',
46
            Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY,
47
            'Sets configuration option'
48
        );
49
    }
50
51
    protected function selectHosts(Input $input, Output $output)
52
    {
53
        $output->getFormatter()->setStyle('success', new OutputFormatterStyle('green'));
54
        if (!$output->isDecorated()) {
55
            define('NO_ANSI', 'true');
56
        }
57
        $selectExpression = $input->getOption('selector');
58
        $this->parseOptions($input->getOption('option'));
59
60
        if (empty($selectExpression)) {
61
            if (count($this->deployer->hosts) === 1) {
62
                $hosts = $this->deployer->hosts->all();
63
            } else {
64
                $hostsAliases = [];
65
                foreach ($this->deployer->hosts as $host) {
66
                    $hostsAliases[] = $host->alias();
67
                }
68
                /** @var QuestionHelper $helper */
69
                $helper = $this->getHelper('question');
70
                $question = new ChoiceQuestion(
71
                    '<question>Select hosts:</question> (comma separated)',
72
                    $hostsAliases
73
                );
74
                $question->setMultiselect(true);
75
                $question->setErrorMessage('There is no "%s" host.');
76
                $answer = $helper->ask($input, $output, $question);
77
                $answer = array_unique($answer);
78
                $hosts = $this->deployer->hosts->select(function (Host $host) use ($answer) {
79
                    return in_array($host->alias(), $answer, true);
80
                });
81
            }
82
        } else {
83
            $hosts = $this->deployer->selector->selectHosts($selectExpression);
84
        }
85
86
        if (empty($hosts)) {
87
            throw new Exception('No host selected');
88
        }
89
90
        return $hosts;
91
    }
92
93
    private function parseOptions(array $options)
94
    {
95
        foreach ($options as $option) {
96
            list($name, $value) = explode('=', $option);
97
            $value = $this->castValueToPhpType($value);
98
            $this->deployer->config->set($name, $value);
99
        }
100
    }
101
102
    private function castValueToPhpType($value)
103
    {
104
        switch ($value) {
105
            case 'true':
106
                return true;
107
            case 'false':
108
                return false;
109
            default:
110
                return $value;
111
        }
112
    }
113
}
114