Completed
Pull Request — master (#14)
by Peter
06:30 queued 04:00
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 110
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 110
ccs 0
cts 88
cp 0
rs 8.2857
cc 1
eloc 84
nc 1
nop 0
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public function __construct($shmop_salt, $file_path)
31
    {
32
        $this->shmop_salt = $shmop_salt;
33
        $this->file_path = $file_path;
34
    }
35
36
    /**
37
     * @return TreeBuilder
38
     */
39
    public function getConfigTreeBuilder()
40
    {
41
        $treeBuilder = new TreeBuilder();
42
        $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
            ->children()
65
                ->scalarNode('use_driver')
66
                    ->cannotBeEmpty()
67
                    ->defaultValue('file')
68
                ->end()
69
                ->arrayNode('drivers')
70
                    ->children()
71
                        ->arrayNode('multi')
72
                            ->children()
73
                                ->scalarNode('fast')
74
                                    ->cannotBeEmpty()
75
                                    ->defaultValue('shmop')
76
                                ->end()
77
                                ->scalarNode('slow')
78
                                    ->cannotBeEmpty()
79
                                    ->defaultValue('file')
80
                                ->end()
81
                            ->end()
82
                        ->end() // multi
83
                        ->arrayNode('shmop')
84
                            ->children()
85
                                ->scalarNode('salt')
86
                                    ->cannotBeEmpty()
87
                                    ->defaultValue($this->shmop_salt)
88
                                ->end()
89
                            ->end()
90
                        ->end() // shmop
91
                        ->arrayNode('file')
92
                            ->children()
93
                                ->scalarNode('path')
94
                                    ->cannotBeEmpty()
95
                                    ->defaultValue($this->file_path)
96
                                ->end()
97
                            ->end()
98
                        ->end() // file
99
                        ->arrayNode('memcached')
100
                            ->children()
101
                                ->scalarNode('prefix')
102
                                    ->defaultValue('cache_time_keeper_')
103
                                ->end()
104
                                ->scalarNode('persistent_id')
105
                                    ->defaultValue('cache_time_keeper')
106
                                    ->info(
107
                                        'Specify to enable persistent connections. '.
108
                                        'All clients with the same ID share connections.'
109
                                    )
110
                                ->end()
111
                                ->arrayNode('hosts')
112
                                    ->requiresAtLeastOneElement()
113
                                    ->prototype('array')
114
                                        ->children()
115
                                            ->scalarNode('host')
116
                                                ->cannotBeEmpty()
117
                                                ->defaultValue('localhost')
118
                                            ->end()
119
                                            ->scalarNode('port')
120
                                                ->cannotBeEmpty()
121
                                                ->defaultValue(11211)
122
                                                ->validate()
123
                                                ->ifTrue(function ($v) {
124
                                                    return !is_numeric($v);
125
                                                })
126
                                                    ->thenInvalid('Host port must be numeric')
127
                                                ->end()
128
                                            ->end()
129
                                            ->scalarNode('weight')
130
                                                ->defaultValue(0)
131
                                                ->validate()
132
                                                ->ifTrue(function ($v) {
133
                                                    return !is_numeric($v);
134
                                                })
135
                                                    ->thenInvalid('Host weight must be numeric')
136
                                                ->end()
137
                                            ->end()
138
                                        ->end()
139
                                    ->end()
140
                                ->end()
141
                            ->end()
142
                        ->end() // memcached
143
                    ->end()
144
                ->end() // drivers
145
            ->end();
146
147
        return $treeBuilder;
148
    }
149
}
150