DeployConfiguration::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webcreate\Conveyor\Config\Definition;
13
14
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
18
use Webcreate\Conveyor\Factory\StrategyFactory;
19
use Webcreate\Conveyor\Factory\TaskFactory;
20
use Webcreate\Conveyor\Factory\TransporterFactory;
21
22
class DeployConfiguration implements ConfigurationInterface
23
{
24
    protected $taskFactory;
25
    protected $transporterFactory;
26
    protected $strategyFactory;
27
28 2
    public function __construct(TaskFactory $taskFactory = null, TransporterFactory $transporterFactory = null, StrategyFactory $strategyFactory = null)
29
    {
30 2
        $this->taskFactory        = $taskFactory;
31 2
        $this->transporterFactory = $transporterFactory;
32 2
        $this->strategyFactory    = $strategyFactory;
33 2
    }
34
35 2
    public function getConfigTreeBuilder()
36
    {
37 2
        $nodeBuilder = new NodeBuilder();
38 2
        $nodeBuilder->setNodeClass('task', __NAMESPACE__ . '\\Builder\\TaskNodeDefinition');
39 2
        $nodeBuilder->setNodeClass('transporter', __NAMESPACE__ . '\\Builder\\TransporterNodeDefinition');
40 2
        $nodeBuilder->setNodeClass('strategy', __NAMESPACE__ . '\\Builder\\StrategyNodeDefinition');
41
42 2
        $treeBuilder = new TreeBuilder();
43 2
        $rootNode = $treeBuilder->root('conveyor', 'array', $nodeBuilder);
44
45 2
        $validSchemes = array('git', 'svn');
46
47
        $rootNode
48 2
            ->children()
49 2
                ->arrayNode('repository')
50 2
                    ->isRequired()
51 2
                    ->children()
52 2
                        ->scalarNode('type')->isRequired()->cannotBeEmpty()->end()
53 2
                        ->scalarNode('url')->isRequired()->cannotBeEmpty()->end()
54 2
                    ->end()
55 2
                    ->beforeNormalization()
56 2
                        ->ifString()
57 2
                        ->then(function ($v) {
58 1
                            $result = parse_url($v);
59
60 1
                            if (false === $result) {
61
                                throw new InvalidConfigurationException(sprintf(
62
                                    'Could not parse repository url "%s"',
63
                                    $v
64
                                ));
65
                            }
66
67
                            return array(
68 1
                                'type' => $result['scheme'],
69 1
                                'url'  => $v,
70
                            );
71 2
                        })
72 2
                    ->end()
73 2
                    ->validate()
74
                        ->ifTrue(function ($v) use ($validSchemes) {
75 2
                            return false === in_array($v['type'], $validSchemes);
76 2
                        })
77 2
                        ->thenInvalid('Invalid repository type "%s". Valid types are: '.implode(', ', $validSchemes).'.')
78 2
                    ->end()
79 2
                ->end()
80 2
                ->arrayNode('targets')
81 2
                    ->prototype('array')
82 2
                        ->children()
83 2
                            ->scalarNode('name')->end()
84 2
                            ->scalarNode('url')->end()
85 2
                            ->arrayNode('groups')
86 2
                                ->defaultValue(array())
87 2
                                ->beforeNormalization()
88 2
                                    ->ifString()
89 2
                                    ->then(function ($v) {
90
                                            return array($v);
91 2
                                        })
0 ignored issues
show
Coding Style introduced by
Closing brace indented incorrectly; expected 36 spaces, found 40
Loading history...
92 2
                                ->end()
93 2
                                ->prototype('scalar')->end()
94 2
                            ->end()
95 2
                            ->node('transport', 'transporter')
96 2
                                ->setTransporterFactory($this->transporterFactory)
97 2
                                ->isRequired()
98 2
                                ->beforeNormalization()
99 2
                                ->ifString()
100 2
                                ->then(function ($v) {
101
                                        $regex = '/^(?P<type>\w+):\/\/(?P<user>\w+)(:(?P<pass>\w+))?@(?P<host>[.\w]+)(:(?P<port>\w+))?(?P<path>\/[\/\w]+)/';
102
103
                                        if (is_string($v)) {
104
                                            if (preg_match($regex, $v, $matches)) {
105
                                                return array(
106
                                                    'type' => $matches['type'],
107
                                                    'user' => $matches['user'],
108
                                                    'pass' => $matches['pass'] ?: null,
109
                                                    'host' => $matches['host'],
110
                                                    'port' => $matches['port'],
111
                                                    'path' => $matches['path'],
112
                                                );
113
                                            } else {
114
                                                throw new InvalidConfigurationException(sprintf('Could not parse "%s" as DSN', $v));
115
                                            }
116
                                        }
117
118
                                        return $v;
119 2
                                    })
0 ignored issues
show
Coding Style introduced by
Closing brace indented incorrectly; expected 32 spaces, found 36
Loading history...
120 2
                                ->end()
121 2
                            ->end()
122 2
                            ->arrayNode('parameters')
123 2
                                ->prototype('scalar')->end()
124 2
                            ->end()
125 2
                        ->end()
126 2
                    ->end()
127 2
                ->end()
128 2
                ->arrayNode('build')
129 2
                    ->children()
130 2
                        ->scalarNode('dir')
131 2
                            ->defaultValue('/tmp/conveyor')
132 2
                            ->beforeNormalization()
133 2
                                ->ifString()
134 2
                                ->then(function ($v) {
135
                                    // resolve home dir
136 1
                                    $path = preg_replace('/^\~/', getenv('HOME'), $v);
137
138 1
                                    return $path;
139 2
                                })
140 2
                            ->end()
141 2
                        ->end()
142 2
                        ->arrayNode('derived')
143 2
                            ->prototype('array')
144 2
                                ->children()
145 2
                                    ->scalarNode('source')->end()
146 2
                                    ->scalarNode('derived')->end()
147 2
                                ->end()
148 2
                            ->end()
149 2
                        ->end()
150 2
                        ->arrayNode('tasks')
151 2
                            ->prototype('task')
152 2
                                ->setTaskFactory($this->taskFactory)
153 2
                                ->children()
154 2
                                    ->arrayNode('targets')
155 2
                                        ->beforeNormalization()
156 2
                                            ->ifString()
157 2
                                            ->then(function ($v) {
158
                                                return array($v);
159 2
                                            })
160 2
                                        ->end()
161 2
                                        ->prototype('scalar')->end()
162 2
                                    ->end()
163 2
                                ->end()
164 2
                            ->end()
165 2
                        ->end()
166 2
                    ->end()
167 2
                ->end()
168 2
                ->arrayNode('undeploy')
169 2
                    ->children()
170 2
                        ->arrayNode('tasks')
171 2
                            ->prototype('task')
172 2
                                ->setTaskFactory($this->taskFactory)
173 2
                                ->children()
174 2
                                    ->arrayNode('targets')
175 2
                                        ->beforeNormalization()
176 2
                                            ->ifString()
177 2
                                            ->then(function ($v) {
178
                                                return array($v);
179 2
                                            })
180 2
                                        ->end()
181 2
                                        ->prototype('scalar')->end()
182 2
                                    ->end()
183 2
                                ->end()
184 2
                            ->end()
185 2
                        ->end()
186 2
                    ->end()
187 2
                ->end()
188 2
                ->arrayNode('deploy')
189 2
                    ->addDefaultsIfNotSet()
190 2
                    ->children()
191 2
                        ->node('strategy', 'strategy')
192 2
                            ->beforeNormalization()
193 2
                                ->ifString()
194 2
                                ->then(function ($v) {
195
                                    return array('type' => $v);
196 2
                                })
197 2
                            ->end()
198 2
                            ->setStrategyFactory($this->strategyFactory)
199 2
                            ->addDefaultsIfNotSet()
200 2
                            ->children()
201 2
                                ->scalarNode('type')->isRequired()->defaultValue('releases')->end()
202 2
                            ->end()
203 2
                        ->end()
204 2
                        ->arrayNode('before')
205 2
                            ->prototype('task')
206 2
                                ->setTaskFactory($this->taskFactory)
207 2
                                ->children()
208 2
                                    ->arrayNode('targets')
209 2
                                        ->beforeNormalization()
210 2
                                            ->ifString()
211 2
                                            ->then(function ($v) {
212
                                                return array($v);
213 2
                                            })
214 2
                                        ->end()
215 2
                                        ->prototype('scalar')->end()
216 2
                                    ->end()
217 2
                                ->end()
218 2
                            ->end()
219 2
                        ->end()
220 2
                        ->arrayNode('after')
221 2
                            ->prototype('task')
222 2
                                ->setTaskFactory($this->taskFactory)
223 2
                                ->children()
224 2
                                    ->arrayNode('targets')
225 2
                                        ->beforeNormalization()
226 2
                                            ->ifString()
227 2
                                            ->then(function ($v) {
228
                                                    return array($v);
229 2
                                                })
0 ignored issues
show
Coding Style introduced by
Closing brace indented incorrectly; expected 44 spaces, found 48
Loading history...
230 2
                                        ->end()
231 2
                                        ->prototype('scalar')->end()
232 2
                                    ->end()
233 2
                                ->end()
234 2
                            ->end()
235 2
                        ->end()
236 2
                        ->arrayNode('final')
237 2
                            ->prototype('task')
238 2
                                ->setTaskFactory($this->taskFactory)
239 2
                                ->children()
240 2
                                    ->arrayNode('targets')
241 2
                                        ->beforeNormalization()
242 2
                                            ->ifString()
243 2
                                            ->then(function ($v) {
244
                                                    return array($v);
245 2
                                                })
0 ignored issues
show
Coding Style introduced by
Closing brace indented incorrectly; expected 44 spaces, found 48
Loading history...
246 2
                                        ->end()
247 2
                                        ->prototype('scalar')->end()
248 2
                                    ->end()
249 2
                                ->end()
250 2
                            ->end()
251 2
                        ->end()
252 2
                    ->end()
253 2
                ->end()
254 2
            ->end()
255
        ;
256
257 2
        return $treeBuilder;
258
    }
259
}
260