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

BootstrapByConfigFile::executeBuilderMethods()   D

Complexity

Conditions 9
Paths 108

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 11.7422

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
ccs 23
cts 34
cp 0.6765
rs 4.807
cc 9
eloc 26
nc 108
nop 2
crap 11.7422
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\Builder\BuilderInterface;
11
use Deployer\Collection\Collection;
12
use Symfony\Component\Yaml\Yaml;
13
14
/**
15
 * BootstrapByConfigFile
16
 *
17
 * Moved some initialization logic from src/functions.php to here, since
18
 * putting application logic in public functions which callable without
19
 * any restriction is not good.
20
 *
21
 * We do not need any inheritance or interface implementation here,
22
 * it's just simple POPO class.
23
 *
24
 * @author Irfan Durmus (http://github.com/irfan) <[email protected]>
25
 */
26
class BootstrapByConfigFile
27
{
28
    /**
29
     * @var string|null $configFile
30
     */
31
    public $configFile = null;
32
33
    /**
34
     * @var array $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 BuilderInterface[] $clusterBuilders
50
     */
51
    public $clusterBuilders = [];
52
53
    /**
54
     * @var BuilderInterface[] $serverBuilders
55
     */
56
    public $serverBuilders = [];
57
58
    /**
59
     * @param Collection $config
60
     * @param BuilderInterface $builder
61
     */
62 1
    private function executeBuilderMethods(Collection $config, BuilderInterface $builder)
63
    {
64 1
        if ($config->has('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->has('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->has('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->has($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 set vars.
101 1
        foreach ($config->toArray() as $key => $value) {
102 1
            $builder->set($key, $value);
103 1
        }
104 1
    }
105
106
    /**
107
     * @throws \RuntimeException
108
     * @return \Deployer\Bootstrap\BootstrapByConfigFile
109
     */
110 3
    public function parseConfig()
111
    {
112 3
        $this->configFileContent = Yaml::parse(file_get_contents($this->configFile));
113
114 3
        if (!is_array($this->configFileContent)) {
115
            throw new \RuntimeException("Error in parsing " . $this->configFile . " file.");
116
        }
117
118 3
        foreach ($this->configFileContent as $key => $cnf) {
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 Collection($config);
142
143 1
                if ($da->has('local')) {
144
                    $builder = \Deployer\localServer($name);
145
                } else {
146 1
                    $builder = $da->has('port') ?
147 1
                        $this->serverBuilders[] = \Deployer\server($name, $da['host'], $da['port']) :
148 1
                        $this->serverBuilders[] = \Deployer\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 Collection($config);
172
173 1
                $clusterBuilder = $config->has('port') ?
174 1
                    $this->clusterBuilders[] = \Deployer\cluster($name, $config['nodes'], $config['port']) :
175 1
                    $this->clusterBuilders[] = \Deployer\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
     * @return \Deployer\Bootstrap\BootstrapByConfigFile
192
     */
193 7
    public function setConfig($file)
194
    {
195 7
        $this->configFile = $file;
196 7
        return $this;
197
    }
198
}
199