Completed
Push — master ( 6e12de...23243d )
by Peter
03:07
created

Configuration::getDriverShmop()   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 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
     * 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
     *         memcached:
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 1
    public function getConfigTreeBuilder()
61
    {
62 1
        $tree_builder = new TreeBuilder();
63
        $tree_builder
64 1
            ->root('anime_db_cache_time_keeper')
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