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 ( 155980...282e4d )
by Anton
03:03
created

SelectCommand   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 94
rs 10
wmc 21

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 3 1
A __construct() 0 4 1
B complete() 0 24 7
C selectHosts() 0 47 12
1
<?php
2
3
declare(strict_types=1);
4
5
/* (c) Anton Medvedev <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Deployer\Command;
12
13
use Deployer\Deployer;
14
use Deployer\Exception\ConfigurationException;
15
use Deployer\Exception\Exception;
16
use Deployer\Host\Host;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Completion\CompletionInput;
19
use Symfony\Component\Console\Completion\CompletionSuggestions;
20
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
21
use Symfony\Component\Console\Helper\QuestionHelper;
22
use Symfony\Component\Console\Input\InputArgument;
23
use Symfony\Component\Console\Input\InputInterface as Input;
24
use Symfony\Component\Console\Output\OutputInterface as Output;
25
use Symfony\Component\Console\Question\ChoiceQuestion;
26
27
abstract class SelectCommand extends Command
28
{
29
    /**
30
     * @var Deployer
31
     */
32
    protected $deployer;
33
34
    public function __construct(string $name, Deployer $deployer)
35
    {
36
        $this->deployer = $deployer;
37
        parent::__construct($name);
38
    }
39
40
    protected function configure()
41
    {
42
        $this->addArgument('selector', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Host selector');
43
    }
44
45
    /**
46
     * @return Host[]
47
     */
48
    protected function selectHosts(Input $input, Output $output): array
49
    {
50
        $output->getFormatter()->setStyle('success', new OutputFormatterStyle('green'));
51
        if (!$output->isDecorated() && !defined('NO_ANSI')) {
52
            define('NO_ANSI', 'true');
53
        }
54
        $selector = $input->getArgument('selector');
55
        $selector = empty($selector) ? Deployer::get()->config->get('default_selector', '') : $selector;
56
        $selectExpression = is_array($selector) ? implode(',', $selector) : $selector;
57
58
        if (empty($selectExpression)) {
59
            if (count($this->deployer->hosts) === 0) {
60
                throw new ConfigurationException("No host configured.\nSpecify at least one host: `localhost();`.");
61
            } elseif (count($this->deployer->hosts) === 1) {
62
                $hosts = $this->deployer->hosts->all();
63
            } elseif ($input->isInteractive()) {
64
                $hostsAliases = [];
65
                foreach ($this->deployer->hosts as $host) {
66
                    $hostsAliases[] = $host->getAlias();
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->getAlias(), $answer, true);
80
                });
81
            }
82
        } else {
83
            $hosts = $this->deployer->selector->select($selectExpression);
84
        }
85
86
        if (empty($hosts)) {
87
            $message = 'No host selected.';
88
            if (!empty($selectExpression)) {
89
                $message .= " Please, check your selector:\n\n    $selectExpression";
90
            }
91
            throw new Exception($message);
92
        }
93
94
        return $hosts;
95
    }
96
97
    public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
98
    {
99
        parent::complete($input, $suggestions);
100
        if ($input->mustSuggestArgumentValuesFor('selector')) {
101
            $selectors = ['all'];
102
            $configs = [];
103
            foreach ($this->deployer->hosts as $host) {
104
                $configs[$host->getAlias()] = $host->config()->persist();
105
            }
106
            foreach ($configs as $alias => $c) {
107
                $selectors[] = $alias;
108
                foreach ($c['labels'] ?? [] as $label => $value) {
109
                    $selectors[] = "$label=$value";
110
                }
111
            }
112
            $selectors = array_unique($selectors);
113
            $suggestions->suggestValues($selectors);
114
        }
115
        if ($input->mustSuggestOptionValuesFor('option')) {
116
            $values = [];
117
            foreach ($this->deployer->config->keys() as $key) {
118
                $values[] = $key . '=';
119
            }
120
            $suggestions->suggestValues($values);
121
        }
122
    }
123
}
124