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 (#1061)
by Maxim
04:23 queued 01:35
created

StageStrategy::getServers()   C

Complexity

Conditions 11
Paths 60

Size

Total Lines 50
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 11.0295

Importance

Changes 0
Metric Value
cc 11
eloc 25
nc 60
nop 1
dl 0
loc 50
ccs 30
cts 32
cp 0.9375
crap 11.0295
rs 5.4893
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Stage;
9
10
use Deployer\Server\Environment;
11
use Deployer\Server\EnvironmentCollection;
12
use Deployer\Server\Local;
13
use Deployer\Server\ServerCollection;
14
15
class StageStrategy implements StageStrategyInterface
16
{
17
    /**
18
     * @var EnvironmentCollection
19
     */
20
    private $environments;
21
22
    /**
23
     * @var ServerCollection
24
     */
25
    private $servers;
26
27
    /**
28
     * @var string
29
     */
30
    private $defaultStage;
31
32 17
    public function __construct(ServerCollection $servers, EnvironmentCollection $environments, $defaultStage = null)
33
    {
34 17
        $this->servers = $servers;
35 17
        $this->environments = $environments;
36 17
        $this->defaultStage = $defaultStage;
37 17
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 17
    public function getServers($stage)
43
    {
44 17
        $servers = [];
45
46
        // Get a default stage (if any) if no stage given
47 17
        if (empty($stage)) {
48 15
            $stage = $this->getDefaultStage();
49 15
        }
50
51 17
        if (!empty($stage)) {
52
53
            // Look for servers which has in set `stages` current stage name.
54 3
            foreach ($this->environments as $name => $env) {
55
                // If server does not have any stage category, skip them
56 3
                if (in_array($stage, $env->get('stages', []), true)) {
57 2
                    $servers[$name] = $this->servers->get($name);
58 2
                }
59 3
            }
60
61
            // If still is empty, try to find server by name.
62 3
            if (empty($servers)) {
63 1
                if ($this->servers->has($stage)) {
64 1
                    $servers = [$stage => $this->servers->get($stage)];
65 1
                } else {
66
                    // Nothing found.
67
                    throw new \RuntimeException("Stage or server `$stage` was not found.");
68
                }
69 1
            }
70 3
        } else {
71
            // Otherwise run on all servers what does not specify stage.
72 14
            foreach ($this->environments as $name => $env) {
73 12
                if (!$env->has('stages')) {
74 12
                    $servers[$name] = $this->servers->get($name);
75 12
                }
76 14
            }
77
        }
78
79 17
        if (empty($servers)) {
80 2
            if (count($this->servers) === 0) {
81 2
                $local = new Local();
82 2
                $this->environments['localhost'] = new Environment();
83
84 2
                $servers = ['localhost' => $local];
85 2
            } else {
86
                throw new \RuntimeException('You need to specify at least one server or stage.');
87
            }
88 2
        }
89
90 17
        return $servers;
91
    }
92
93
    /**
94
     * Returns the default stage
95
     *
96
     * @return string
97
     */
98 15
    public function getDefaultStage()
99
    {
100 15
        return $this->defaultStage;
101
    }
102
}
103