Completed
Push — master ( e6f9e9...ade0a3 )
by Peter
03:33
created

Configuration::getDriverMemcache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 1

Importance

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