Configuration::getDriverShmop()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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