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   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.87%

Importance

Changes 0
Metric Value
dl 0
loc 88
ccs 37
cts 39
cp 0.9487
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C getServers() 0 50 11
A getDefaultStage() 0 4 1
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