Completed
Push — master ( abbc89...6b01a6 )
by Peter
03:39
created

Configuration::getDriverFile()   A

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
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
     *     use_driver: file
44
     *     track:
45
     *         clear_cache: true
46
     *     drivers:
47
     *         multi:
48
     *             fast: shmop
49
     *             slow: file
50
     *         shmop:
51
     *             salt: '%secret%'
52
     *         file:
53
     *             path: '%kernel.root_dir%/cache/cache-time-keeper/'
54
     *         memcache:
55
     *             prefix: 'cache_time_keeper_'
56
     *             persistent_id: 'cache_time_keeper'
57
     *             hosts:
58
     *                 - {host: 'localhost', port: 11211, weight: 100}
59
     *
60
     * @return TreeBuilder
61
     */
62 2
    public function getConfigTreeBuilder()
63
    {
64 2
        $tree_builder = new TreeBuilder();
65
        $tree_builder
66 2
            ->root('anime_db_cache_time_keeper')
67 2
                ->children()
68 2
                    ->scalarNode('use_driver')
69 2
                        ->cannotBeEmpty()
70 2
                        ->defaultValue('file')
71 2
                    ->end()
72 2
                    ->arrayNode('track')
73 2
                        ->children()
74 2
                            ->booleanNode('clear_cache')
75 2
                                ->defaultTrue()
76 2
                            ->end()
77 2
                        ->end()
78 2
                    ->end()
79 2
                    ->arrayNode('drivers')
80 2
                        ->append($this->getDriverFile())
81 2
                        ->append($this->getDriverMemcache())
82 2
                        ->append($this->getDriverMulti())
83 2
                        ->append($this->getDriverShmop())
84 2
                    ->end()
85 2
                ->end();
86
87 2
        return $tree_builder;
88
    }
89
90
    /**
91
     * @return ArrayNodeDefinition
92
     */
93 2
    protected function getDriverMulti()
94
    {
95 2
        $tree_builder = new TreeBuilder();
96
97
        return $tree_builder
98 2
            ->root('multi')
99 2
                ->children()
100 2
                    ->scalarNode('fast')
101 2
                        ->cannotBeEmpty()
102 2
                        ->defaultValue('shmop')
103 2
                    ->end()
104 2
                    ->scalarNode('slow')
105 2
                        ->cannotBeEmpty()
106 2
                        ->defaultValue('file')
107 2
                    ->end()
108 2
                ->end();
109
    }
110
111
    /**
112
     * @return ArrayNodeDefinition
113
     */
114 2
    protected function getDriverShmop()
115
    {
116 2
        $tree_builder = new TreeBuilder();
117
118
        return $tree_builder
119 2
            ->root('shmop')
120 2
                ->children()
121 2
                    ->scalarNode('salt')
122 2
                        ->cannotBeEmpty()
123 2
                        ->defaultValue($this->shmop_salt)
124 2
                    ->end()
125 2
                ->end();
126
    }
127
128
    /**
129
     * @return ArrayNodeDefinition
130
     */
131 2
    protected function getDriverFile()
132
    {
133 2
        $tree_builder = new TreeBuilder();
134
135
        return $tree_builder
136 2
            ->root('file')
137 2
                ->children()
138 2
                    ->scalarNode('path')
139 2
                        ->cannotBeEmpty()
140 2
                        ->defaultValue($this->file_path)
141 2
                    ->end()
142 2
                ->end();
143
    }
144
145
    /**
146
     * @return ArrayNodeDefinition
147
     */
148 2
    protected function getDriverMemcache()
149
    {
150 2
        $tree_builder = new TreeBuilder();
151
152
        return $tree_builder
153 2
            ->root('memcache')
154 2
                ->children()
155 2
                    ->scalarNode('prefix')
156 2
                        ->defaultValue('cache_time_keeper_')
157 2
                    ->end()
158 2
                    ->scalarNode('persistent_id')
159 2
                        ->defaultValue('cache_time_keeper')
160 2
                        ->info(
161
                            'Specify to enable persistent connections. '.
162
                            'All clients with the same ID share connections.'
163 2
                        )
164 2
                    ->end()
165 2
                    ->arrayNode('hosts')
166 2
                        ->requiresAtLeastOneElement()
167 2
                        ->prototype('array')
168 2
                            ->children()
169 2
                                ->scalarNode('host')
170 2
                                    ->cannotBeEmpty()
171 2
                                    ->defaultValue('localhost')
172 2
                                ->end()
173 2
                                ->scalarNode('port')
174 2
                                    ->cannotBeEmpty()
175 2
                                    ->defaultValue(11211)
176 2
                                    ->validate()
177
                                    ->ifTrue(function ($v) {
178 1
                                        return !is_numeric($v);
179 2
                                    })
180 2
                                        ->thenInvalid('Host port must be numeric')
181 2
                                    ->end()
182 2
                                ->end()
183 2
                                ->scalarNode('weight')
184 2
                                    ->defaultValue(0)
185 2
                                    ->validate()
186 2
                                    ->ifTrue(function ($v) {
187 1
                                        return !is_numeric($v);
188 2
                                    })
189 2
                                        ->thenInvalid('Host weight must be numeric')
190 2
                                    ->end()
191 2
                                ->end()
192 2
                            ->end()
193 2
                        ->end()
194 2
                    ->end()
195 2
                ->end();
196
    }
197
}
198