ServerExtension   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 90
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 3 1
A load() 0 15 1
A configure() 0 50 3
A getName() 0 4 1
1
<?php
2
3
namespace Openl10n\Cli\ServiceContainer\Extension;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
class ServerExtension implements ConfiguredExtension
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function initialize(ContainerBuilder $container)
15
    {
16
    }
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function load(array $config, ContainerBuilder $container)
22
    {
23
        $container
24
            ->register('api.config', 'Openl10n\Sdk\Config')
25
            ->addArgument($config['hostname'])
26
            ->addArgument($config['use_ssl'])
27
            ->addArgument($config['port'])
28
            ->addMethodCall('setAuth', array($config['username'], $config['password']))
29
        ;
30
31
        $container
32
            ->register('api', 'Openl10n\Sdk\Api')
33
            ->addArgument(new Reference('api.config'))
34
        ;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function configure(ArrayNodeDefinition $node)
41
    {
42
        $node
43
            ->beforeNormalization()
44
            ->ifString()
45
                ->then(function ($v) {
46
                    $home = getenv('HOME');
47
                    $filepath = $home.'/.openl10n/server.conf';
48
                    $data = array();
49
                    if (file_exists($filepath)) {
50
                        $data = parse_ini_file($filepath, true);
51
                    }
52
                    if (isset($data[$v])) {
53
                        return $data[$v];
54
                    }
55
56
                    return array(
57
                        'hostname' => $v
58
                    );
59
                })
60
            ->end()
61
            ->children()
62
                ->scalarNode('hostname')
63
                    ->isRequired()
64
                    ->cannotBeEmpty()
65
                ->end()
66
                ->scalarNode('username')
67
                    ->isRequired()
68
                    ->cannotBeEmpty()
69
                ->end()
70
                ->scalarNode('password')
71
                    ->isRequired()
72
                    ->cannotBeEmpty()
73
                ->end()
74
                ->booleanNode('use_ssl')
75
                    // Because data parsed from an INI file is not
76
                    // interpreted as boolean, then cast automatically.
77
                    ->beforeNormalization()
78
                    ->ifString()
79
                        ->then(function ($v) {
80
                            return (boolean) $v;
81
                        })
82
                    ->end()
83
                    ->defaultFalse()
84
                ->end()
85
                ->integerNode('port')
86
                    ->defaultNull()
87
                ->end()
88
            ->end();
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getName()
95
    {
96
        return 'server';
97
    }
98
}
99