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.
Completed
Push — master ( 99078a...9605c1 )
by Oanh
02:49
created

StageStrategy::getDefaultStage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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