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 ( de1347...897205 )
by Oanh
03:26
created

BootstrapByConfigFile::initServers()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.1417

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 19
nc 14
nop 0
dl 0
loc 29
ccs 16
cts 19
cp 0.8421
crap 6.1417
rs 8.439
c 1
b 0
f 0
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\Bootstrap;
9
10
use Deployer\Type\DotArray;
11
use Deployer\Cluster\ClusterBuilder;
12
use Deployer\Server\Builder;
13
use Symfony\Component\Yaml\Yaml;
14
15
/**
16
 * @author Irfan Durmus (http://github.com/irfan) <[email protected]>
17
 *
18
 * Moved some initialization logic from src/functions.php to here, since
19
 * putting application logic in public functions which callable without
20
 * any restriction is not good.
21
 *
22
 * We do not need any inheritance or interface implementation here,
23
 * it's just simple POPO class.
24
 */
25
class BootstrapByConfigFile
26
{
27
    
28
    /**
29
     * @var string|null $configFile
30
     */
31
    public $configFile = null;
32
33
    /**
34
     * @var array|string|\stdClass|null $configFileContent
35
     */
36
    public $configFileContent = null;
37
38
    /**
39
     * @var array $clusterConfig
40
     */
41
    public $clusterConfig = [];
42
43
    /**
44
     * @var array $serverConfig
45
     */
46
    public $serverConfig = [];
47
48
    /**
49
     * @var Deployer\Cluster\ClusterBuilder[] $clusterBuilders
50
     */
51
    public $clusterBuilders = [];
52
    
53
    /**
54
     * @var Deployer\Server\Builder[] $serverBuilders
55
     */
56
    public $serverBuilders = [];
57
58
    /**
59
     * @param \Deployer\Type\DotArray $config
60
     * @param Builder|ClusterBuilder $builder
61
     */
62 1
    private function executeBuilderMethods(DotArray $config, $builder)
63
    {
64 1
        if ($config->hasKey('identity_file')) {
65 1
            if ($config['identity_file'] === null) {
66 1
                $builder->identityFile();
67 1
            } else {
68
                $builder->identityFile(
69
                    $config['identity_file.public_key'],
70
                    $config['identity_file.private_key'],
71
                    $config['identity_file.password']
72
                );
73
            }
74
75 1
            unset($config['identity_file']);
76 1
        }
77
78 1
        if ($config->hasKey('identity_config')) {
79
            if ($config['identity_config'] === null) {
80
                $builder->configFile();
81
            } else {
82
                $builder->configFile($config['identity_config']);
83
            }
84
            unset($config['identity_config']);
85
        }
86
87 1
        if ($config->hasKey('forward_agent')) {
88 1
            $builder->forwardAgent();
89 1
            unset($config['forward_agent']);
90 1
        }
91
92 1
        foreach (['user', 'password', 'stage', 'pem_file'] as $key) {
93 1
            if ($config->hasKey($key)) {
94 1
                $method = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key))));
95 1
                $builder->$method($config[$key]);
96 1
                unset($config[$key]);
97 1
            }
98 1
        }
99
100
        // Everything else are env vars.
101 1
        foreach ($config->toArray() as $key => $value) {
102 1
            $builder->env($key, $value);
103 1
        }
104 1
    }
105
106
    /**
107
     * @throws \RuntimeException
108
     * @return \Deployer\Bootstrap\BootstrapByConfigFile
109
     */
110 3
    public function parseConfig()
111
    {
112
        try {
113 3
            $this->configFileContent = Yaml::parse(file_get_contents($this->configFile));
114 3
        } catch (\RuntimeException $e) {
115
            throw new \RuntimeException("Error in parsing " . $this->configFile . " file.");
116
        }
117
118 3
        foreach ($this->configFileContent as $key => $cnf) {
0 ignored issues
show
Bug introduced by
The expression $this->configFileContent of type array|string|object<stdClass> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
119 3
            if (isset($cnf['cluster']) && $cnf['cluster']) {
120 3
                $this->clusterConfig[$key] = $cnf;
121 3
            } else {
122 3
                $this->serverConfig[$key] = $cnf;
123
            }
124 3
        }
125
126 3
        return $this;
127
    }
128
129
    /**
130
     * @throws \RuntimeException
131
     * @return \Deployer\Bootstrap\BootstrapByConfigFile
132
     */
133 2
    public function initServers()
134
    {
135 2
        foreach ((array) $this->serverConfig as $name => $config) {
136
            try {
137 1
                if (!is_array($config)) {
138
                    throw new \RuntimeException();
139
                }
140
141 1
                $da = new DotArray($config);
142
143 1
                if ($da->hasKey('local')) {
144 1
                    $builder = localServer($name);
145
                } else {
146 1
                    $builder = $da->hasKey('port') ?
147 1
                        $this->serverBuilders[] = server($name, $da['host'], $da['port']) :
148 1
                        $this->serverBuilders[] = server($name, $da['host']);
149
                }
150
151 1
                unset($da['local']);
152 1
                unset($da['host']);
153 1
                unset($da['port']);
154
155 1
                $this->executeBuilderMethods($da, $builder);
156 1
            } catch (\RuntimeException $e) {
157
                throw new \RuntimeException("Error processing servers: ".$name);
158
            }
159 2
        }
160 2
        return $this;
161
    }
162
163
    /**
164
     * @throws \RuntimeException
165
     * @return \Deployer\Bootstrap\BootstrapByConfigFile
166
     */
167 2
    public function initClusters()
168
    {
169 2
        foreach ((array) $this->clusterConfig as $name => $config) {
170
            try {
171 1
                $config = new DotArray($config);
172
173 1
                $clusterBuilder = $config->hasKey('port') ?
174 1
                    $this->clusterBuilders[] = cluster($name, $config['nodes'], $config['port']) :
175 1
                    $this->clusterBuilders[] = cluster($name, $config['nodes']);
176
177 1
                unset($config['local']);
178 1
                unset($config['nodes']);
179 1
                unset($config['port']);
180
181 1
                $this->executeBuilderMethods($config, $clusterBuilder);
182 1
            } catch (\RuntimeException $e) {
183
                throw new \RuntimeException("Error processing clusters: ".$name);
184
            }
185 2
        }
186 2
        return $this;
187
    }
188
189
    /**
190
     * @param string $file
191
     */
192 7
    public function setConfig($file)
193
    {
194 7
        $this->configFile = $file;
195 7
        return $this;
196
    }
197
}
198