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 ( f4a6d9...4b2c9f )
by Anton
02:33
created

TaskCommand   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 163
Duplicated Lines 4.29 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
dl 7
loc 163
ccs 84
cts 92
cp 0.913
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 50 1
C execute() 7 60 11
A parseOptions() 0 8 2
A castValueToPhpType() 0 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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