Completed
Push — master ( a7c3c6...973485 )
by Peter
04:02
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 1

Importance

Changes 10
Bugs 1 Features 4
Metric Value
c 10
b 1
f 4
dl 0
loc 30
ccs 26
cts 26
cp 1
rs 8.8571
cc 1
eloc 27
nc 1
nop 0
crap 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 2
    public function __construct($shmop_salt, $file_path)
32
    {
33 2
        $this->shmop_salt = $shmop_salt;
34 2
        $this->file_path = $file_path;
35 2
    }
36
37
    /**
38
     * Config tree builder.
39
     *
40
     * Example config:
41
     *
42
     * anime_db_cache_time_keeper:
43
     *     enable: true
44
     *     use_driver: 'file'
45
     *     private_headers: ['Authorization', 'Cookie']
46
     *     etag_hasher:
47
     *         driver: 'cache_time_keeper.cache_key_builder.default_etag_hasher'
48
     *         algorithm: sha256
49
     *     track:
50
     *         clear_cache: true
51
     *         individually_entity: false
52
     *     drivers:
53
     *         multi:
54
     *             fast: 'shmop'
55
     *             slow: 'file'
56
     *         shmop:
57
     *             salt: '%secret%'
58
     *         file:
59
     *             path: '%kernel.root_dir%/cache/cache-time-keeper/'
60
     *         memcache:
61
     *             prefix: 'cache_time_keeper_'
62
     *             hosts:
63
     *                 - {host: 'localhost', port: 11211, weight: 100}
64
     *
65
     * @return TreeBuilder
66
     */
67 2
    public function getConfigTreeBuilder()
68
    {
69 2
        $tree_builder = new TreeBuilder();
70
        $tree_builder
71 2
            ->root('anime_db_cache_time_keeper')
72 2
                ->children()
73 2
                    ->booleanNode('enable')
74 2
                        ->defaultTrue()
75 2
                    ->end()
76 2
                    ->scalarNode('use_driver')
77 2
                        ->cannotBeEmpty()
78 2
                        ->defaultValue('file')
79 2
                    ->end()
80 2
                    ->arrayNode('private_headers')
81 2
                        ->treatNullLike([])
82 2
                        ->prototype('scalar')->end()
83 2
                        ->defaultValue(['Authorization', 'Cookie'])
84 2
                    ->end()
85 2
                    ->append($this->getEtagHasher())
86 2
                    ->append($this->getTrack())
87 2
                    ->arrayNode('drivers')
88 2
                        ->append($this->getDriverFile())
89 2
                        ->append($this->getDriverMemcache())
90 2
                        ->append($this->getDriverMulti())
91 2
                        ->append($this->getDriverShmop())
92 2
                    ->end()
93 2
                ->end();
94
95 2
        return $tree_builder;
96
    }
97
98
    /**
99
     * @return ArrayNodeDefinition
100
     */
101 2
    protected function getEtagHasher()
102
    {
103 2
        $tree_builder = new TreeBuilder();
104
105
        return $tree_builder
106 2
            ->root('etag_hasher')
107 2
                ->children()
108 2
                    ->scalarNode('driver')
109 2
                        ->cannotBeEmpty()
110 2
                        ->defaultValue('cache_time_keeper.cache_key_builder.default_etag_hasher')
111 2
                    ->end()
112 2
                    ->scalarNode('algorithm')
113 2
                        ->cannotBeEmpty()
114 2
                        ->defaultValue('sha256')
115 2
                    ->end()
116 2
                ->end();
117
    }
118
119
    /**
120
     * @return ArrayNodeDefinition
121
     */
122 2
    protected function getTrack()
123
    {
124 2
        $tree_builder = new TreeBuilder();
125
126
        return $tree_builder
127 2
            ->root('track')
128 2
                ->children()
129 2
                    ->booleanNode('clear_cache')
130 2
                        ->defaultTrue()
131 2
                    ->end()
132 2
                    ->booleanNode('individually_entity')
133 2
                        ->defaultFalse()
134 2
                    ->end()
135 2
                ->end();
136
    }
137
138
    /**
139
     * @return ArrayNodeDefinition
140
     */
141 2
    protected function getDriverMulti()
142
    {
143 2
        $tree_builder = new TreeBuilder();
144
145
        return $tree_builder
146 2
            ->root('multi')
147 2
                ->children()
148 2
                    ->scalarNode('fast')
149 2
                        ->cannotBeEmpty()
150 2
                        ->defaultValue('shmop')
151 2
                    ->end()
152 2
                    ->scalarNode('slow')
153 2
                        ->cannotBeEmpty()
154 2
                        ->defaultValue('file')
155 2
                    ->end()
156 2
                ->end();
157
    }
158
159
    /**
160
     * @return ArrayNodeDefinition
161
     */
162 2
    protected function getDriverShmop()
163
    {
164 2
        $tree_builder = new TreeBuilder();
165
166
        return $tree_builder
167 2
            ->root('shmop')
168 2
                ->children()
169 2
                    ->scalarNode('salt')
170 2
                        ->cannotBeEmpty()
171 2
                        ->defaultValue($this->shmop_salt)
172 2
                    ->end()
173 2
                ->end();
174
    }
175
176
    /**
177
     * @return ArrayNodeDefinition
178
     */
179 2
    protected function getDriverFile()
180
    {
181 2
        $tree_builder = new TreeBuilder();
182
183
        return $tree_builder
184 2
            ->root('file')
185 2
                ->children()
186 2
                    ->scalarNode('path')
187 2
                        ->cannotBeEmpty()
188 2
                        ->defaultValue($this->file_path)
189 2
                    ->end()
190 2
                ->end();
191
    }
192
193
    /**
194
     * @return ArrayNodeDefinition
195
     */
196 2
    protected function getDriverMemcache()
197
    {
198 2
        $tree_builder = new TreeBuilder();
199
200
        return $tree_builder
201 2
            ->root('memcache')
202 2
                ->children()
203 2
                    ->scalarNode('prefix')
204 2
                        ->defaultValue('cache_time_keeper_')
205 2
                    ->end()
206 2
                    ->arrayNode('hosts')
207 2
                        ->requiresAtLeastOneElement()
208 2
                        ->prototype('array')
209 2
                            ->children()
210 2
                                ->scalarNode('host')
211 2
                                    ->cannotBeEmpty()
212 2
                                    ->defaultValue('localhost')
213 2
                                ->end()
214 2
                                ->scalarNode('port')
215 2
                                    ->cannotBeEmpty()
216 2
                                    ->defaultValue(11211)
217 2
                                    ->validate()
218
                                    ->ifTrue(function ($v) {
219 1
                                        return !is_numeric($v);
220 2
                                    })
221 2
                                        ->thenInvalid('Host port must be numeric')
222 2
                                    ->end()
223 2
                                ->end()
224 2
                                ->scalarNode('weight')
225 2
                                    ->defaultValue(0)
226 2
                                    ->validate()
227 2
                                    ->ifTrue(function ($v) {
228 1
                                        return !is_numeric($v);
229 2
                                    })
230 2
                                        ->thenInvalid('Host weight must be numeric')
231 2
                                    ->end()
232 2
                                ->end()
233 2
                            ->end()
234 2
                        ->end()
235 2
                    ->end()
236 2
                ->end();
237
    }
238
}
239