Completed
Push — master ( e9ba9c...f3d53d )
by Peter
03:53 queued 01:17
created

Configuration   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.73%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 2
c 3
b 0
f 2
lcom 1
cbo 3
dl 0
loc 136
ccs 86
cts 88
cp 0.9773
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getConfigTreeBuilder() 0 110 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\TreeBuilder;
12
use Symfony\Component\Config\Definition\ConfigurationInterface;
13
14
class Configuration implements ConfigurationInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $shmop_salt = '';
20
21
    /**
22
     * @var string
23
     */
24
    protected $file_path = '';
25
26
    /**
27
     * @param string $shmop_salt
28
     * @param string $file_path
29
     */
30 1
    public function __construct($shmop_salt, $file_path)
31
    {
32 1
        $this->shmop_salt = $shmop_salt;
33 1
        $this->file_path = $file_path;
34 1
    }
35
36
    /**
37
     * @return TreeBuilder
38
     */
39 1
    public function getConfigTreeBuilder()
40 1
    {
41 1
        $treeBuilder = new TreeBuilder();
42 1
        $rootNode = $treeBuilder->root('anime_db_cache_time_keeper');
43
44
        /**
45
         * Example config:
46
         *
47
         * anime_db_cache_time_keeper:
48
         *     use_driver: file
49
         *     drivers:
50
         *         multi:
51
         *             fast: shmop
52
         *             slow: file
53
         *         shmop:
54
         *             salt: '%secret%'
55
         *         file:
56
         *             path: '%kernel.root_dir%/cache/cache-time-keeper/'
57
         *         memcached:
58
         *             prefix: 'cache_time_keeper_'
59
         *             persistent_id: 'cache_time_keeper'
60
         *             hosts:
61
         *                 - {host: 'localhost', port: 11211, weight: 100}
62
         */
63
        $rootNode
64 1
            ->children()
65 1
                ->scalarNode('use_driver')
66 1
                    ->cannotBeEmpty()
67 1
                    ->defaultValue('file')
68 1
                ->end()
69 1
                ->arrayNode('drivers')
70 1
                    ->children()
71 1
                        ->arrayNode('multi')
72 1
                            ->children()
73 1
                                ->scalarNode('fast')
74 1
                                    ->cannotBeEmpty()
75 1
                                    ->defaultValue('shmop')
76 1
                                ->end()
77 1
                                ->scalarNode('slow')
78 1
                                    ->cannotBeEmpty()
79 1
                                    ->defaultValue('file')
80 1
                                ->end()
81 1
                            ->end()
82 1
                        ->end() // multi
83 1
                        ->arrayNode('shmop')
84 1
                            ->children()
85 1
                                ->scalarNode('salt')
86 1
                                    ->cannotBeEmpty()
87 1
                                    ->defaultValue($this->shmop_salt)
88 1
                                ->end()
89 1
                            ->end()
90 1
                        ->end() // shmop
91 1
                        ->arrayNode('file')
92 1
                            ->children()
93 1
                                ->scalarNode('path')
94 1
                                    ->cannotBeEmpty()
95 1
                                    ->defaultValue($this->file_path)
96 1
                                ->end()
97 1
                            ->end()
98 1
                        ->end() // file
99 1
                        ->arrayNode('memcached')
100 1
                            ->children()
101 1
                                ->scalarNode('prefix')
102 1
                                    ->defaultValue('cache_time_keeper_')
103 1
                                ->end()
104 1
                                ->scalarNode('persistent_id')
105 1
                                    ->defaultValue('cache_time_keeper')
106 1
                                    ->info(
107
                                        'Specify to enable persistent connections. '.
108
                                        'All clients with the same ID share connections.'
109 1
                                    )
110 1
                                ->end()
111 1
                                ->arrayNode('hosts')
112 1
                                    ->requiresAtLeastOneElement()
113 1
                                    ->prototype('array')
114 1
                                        ->children()
115 1
                                            ->scalarNode('host')
116 1
                                                ->cannotBeEmpty()
117 1
                                                ->defaultValue('localhost')
118 1
                                            ->end()
119 1
                                            ->scalarNode('port')
120 1
                                                ->cannotBeEmpty()
121 1
                                                ->defaultValue(11211)
122 1
                                                ->validate()
123
                                                ->ifTrue(function($v) {
124
                                                    return !is_numeric($v);
125 1
                                                })
126 1
                                                    ->thenInvalid('Host port must be numeric')
127 1
                                                ->end()
128 1
                                            ->end()
129 1
                                            ->scalarNode('weight')
130 1
                                                ->defaultValue(0)
131 1
                                                ->validate()
132 1
                                                ->ifTrue(function ($v) {
133
                                                    return !is_numeric($v);
134 1
                                                })
135 1
                                                    ->thenInvalid('Host weight must be numeric')
136 1
                                                ->end()
137 1
                                            ->end()
138 1
                                        ->end()
139 1
                                    ->end()
140 1
                                ->end()
141 1
                            ->end()
142 1
                        ->end() // memcached
143 1
                    ->end()
144 1
                ->end() // drivers
145 1
            ->end();
146
147 1
        return $treeBuilder;
148
    }
149
}
150