Completed
Push — master ( f25dc2...e6e51c )
by Peter
03:53
created

Configuration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.85%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 6
c 5
b 1
f 2
lcom 1
cbo 5
dl 0
loc 174
ccs 91
cts 93
cp 0.9785
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getConfigTreeBuilder() 0 40 1
A getDriverMulti() 0 17 1
A getDriverShmop() 0 13 1
A getDriverFile() 0 13 1
A getDriverMemcached() 0 49 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2014, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\DependencyInjection;
10
11
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
12
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
13
use Symfony\Component\Config\Definition\ConfigurationInterface;
14
15
class Configuration implements ConfigurationInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $shmop_salt = '';
21
22
    /**
23
     * @var string
24
     */
25
    protected $file_path = '';
26
27
    /**
28
     * @param string $shmop_salt
29
     * @param string $file_path
30
     */
31 1
    public function __construct($shmop_salt, $file_path)
32
    {
33 1
        $this->shmop_salt = $shmop_salt;
34 1
        $this->file_path = $file_path;
35 1
    }
36
37
    /**
38
     * @return TreeBuilder
39
     */
40 1
    public function getConfigTreeBuilder()
41
    {
42 1
        $tree_builder = new TreeBuilder();
43 1
        $root_node = $tree_builder->root('anime_db_cache_time_keeper');
44
45
        /*
46
         * Example config:
47
         *
48
         * anime_db_cache_time_keeper:
49
         *     use_driver: file
50
         *     drivers:
51
         *         multi:
52
         *             fast: shmop
53
         *             slow: file
54
         *         shmop:
55
         *             salt: '%secret%'
56
         *         file:
57
         *             path: '%kernel.root_dir%/cache/cache-time-keeper/'
58
         *         memcached:
59
         *             prefix: 'cache_time_keeper_'
60
         *             persistent_id: 'cache_time_keeper'
61
         *             hosts:
62
         *                 - {host: 'localhost', port: 11211, weight: 100}
63
         */
64
        $root_node
65 1
            ->children()
66 1
                ->scalarNode('use_driver')
67 1
                    ->cannotBeEmpty()
68 1
                    ->defaultValue('file')
69 1
                ->end()
70 1
                ->arrayNode('drivers')
71 1
                    ->append($this->getDriverFile())
72 1
                    ->append($this->getDriverMemcached())
73 1
                    ->append($this->getDriverMulti())
74 1
                    ->append($this->getDriverShmop())
75 1
                ->end()
76 1
            ->end();
77
78 1
        return $tree_builder;
79
    }
80
81
    /**
82
     * @return ArrayNodeDefinition
83
     */
84 1
    protected function getDriverMulti()
85
    {
86 1
        $tree_builder = new TreeBuilder();
87
88
        return $tree_builder
89 1
            ->root('multi')
90 1
                ->children()
91 1
                    ->scalarNode('fast')
92 1
                        ->cannotBeEmpty()
93 1
                        ->defaultValue('shmop')
94 1
                    ->end()
95 1
                    ->scalarNode('slow')
96 1
                        ->cannotBeEmpty()
97 1
                        ->defaultValue('file')
98 1
                    ->end()
99 1
                ->end();
100
    }
101
102
    /**
103
     * @return ArrayNodeDefinition
104
     */
105 1
    protected function getDriverShmop()
106
    {
107 1
        $tree_builder = new TreeBuilder();
108
109
        return $tree_builder
110 1
            ->root('shmop')
111 1
                ->children()
112 1
                    ->scalarNode('salt')
113 1
                        ->cannotBeEmpty()
114 1
                        ->defaultValue($this->shmop_salt)
115 1
                    ->end()
116 1
                ->end();
117
    }
118
119
    /**
120
     * @return ArrayNodeDefinition
121
     */
122 1
    protected function getDriverFile()
123
    {
124 1
        $tree_builder = new TreeBuilder();
125
126
        return $tree_builder
127 1
            ->root('file')
128 1
                ->children()
129 1
                    ->scalarNode('path')
130 1
                        ->cannotBeEmpty()
131 1
                        ->defaultValue($this->file_path)
132 1
                    ->end()
133 1
                ->end();
134
    }
135
136
    /**
137
     * @return ArrayNodeDefinition
138
     */
139 1
    protected function getDriverMemcached()
140
    {
141 1
        $tree_builder = new TreeBuilder();
142
143
        return $tree_builder
144 1
            ->root('memcached')
145 1
                ->children()
146 1
                    ->scalarNode('prefix')
147 1
                        ->defaultValue('cache_time_keeper_')
148 1
                    ->end()
149 1
                    ->scalarNode('persistent_id')
150 1
                        ->defaultValue('cache_time_keeper')
151 1
                        ->info(
152
                            'Specify to enable persistent connections. '.
153
                            'All clients with the same ID share connections.'
154 1
                        )
155 1
                    ->end()
156 1
                    ->arrayNode('hosts')
157 1
                        ->requiresAtLeastOneElement()
158 1
                        ->prototype('array')
159 1
                            ->children()
160 1
                                ->scalarNode('host')
161 1
                                    ->cannotBeEmpty()
162 1
                                    ->defaultValue('localhost')
163 1
                                ->end()
164 1
                                ->scalarNode('port')
165 1
                                    ->cannotBeEmpty()
166 1
                                    ->defaultValue(11211)
167 1
                                    ->validate()
168
                                    ->ifTrue(function ($v) {
169
                                        return !is_numeric($v);
170 1
                                    })
171 1
                                        ->thenInvalid('Host port must be numeric')
172 1
                                    ->end()
173 1
                                ->end()
174 1
                                ->scalarNode('weight')
175 1
                                    ->defaultValue(0)
176 1
                                    ->validate()
177 1
                                    ->ifTrue(function ($v) {
178
                                        return !is_numeric($v);
179 1
                                    })
180 1
                                        ->thenInvalid('Host weight must be numeric')
181 1
                                    ->end()
182 1
                                ->end()
183 1
                            ->end()
184 1
                        ->end()
185 1
                    ->end()
186 1
                ->end();
187
    }
188
}
189