ApplicationConfiguration::getConfigTreeBuilder()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 140
Code Lines 125

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 125
nc 1
nop 0
dl 0
loc 140
rs 8.2857
c 0
b 0
f 0

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
 * Created by PhpStorm.
4
 * User: nicolas
5
 * Date: 18/02/17
6
 * Time: 07:26
7
 */
8
9
namespace Devgiants\Configuration;
10
11
12
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Config...ion\Builder\TreeBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\Config\Definition\ConfigurationInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Config...\ConfigurationInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
class ApplicationConfiguration implements ConfigurationInterface
16
{
17
    const ROOT_NODE = 'configuration';
18
    const NODE_NAME = 'name';
19
    const NODE_DEFAULT_VALUE = 'default_value';
20
21
    const REMANENCE_NODE = [
22
        self::NODE_NAME => 'remanence',
23
        self::NODE_DEFAULT_VALUE => 5
24
    ];
25
26
	const LOG_NODE = [
27
		self::NODE_NAME => 'log_folder',
28
		self::NODE_DEFAULT_VALUE => '/tmp/websites-backup-logs/'
29
	];
30
31
    const SITES = 'sites';
32
    const BACKUP_STORAGES = 'backup_storages';
33
34
    const PRE_SAVE_COMMANDS = 'pre_save_commands';
35
    const POST_SAVE_COMMANDS = 'post_save_commands';
36
37
    const DATABASE = 'database';
38
    const NAME = 'name';
39
40
    const SERVER = 'server';
41
    const USER = 'user';
42
    const PASSWORD = "password";
43
44
    const DATABASE_SERVER = [
45
        self::NODE_NAME => self::SERVER,
46
        self::NODE_DEFAULT_VALUE => 'localhost'
47
    ];
48
49
50
    const FILES = 'files';
51
52
    const ROOT_DIR = 'root_dir';
53
    const FOLDERS_TO_INCLUDE = 'include';
54
    const FOLDERS_TO_EXCLUDE = 'exclude';
55
56
    const FTP = 'FTP';
57
    const SSH = 'SSH';
58
	const DROPBOX = 'Dropbox';
59
    const AUTHORIZED_STORAGE = [
60
        self::FTP,
61
        self::SSH,
62
	    self::DROPBOX
63
    ];
64
65
    const STORAGE_TYPE = 'type';
66
67
    /* FTP specific */
68
    const SSL = 'ssl';
69
    const PASSIVE = 'passive';
70
    const TRANSFER = 'transfer';
71
72
    /* Dropbox specific */
73
	const ACCESS_TOKEN = "access_token";
74
	const CLIENT_ID = "client_id";
75
	const CLIENT_SECRET = "client_secret";
76
77
78
    public function getConfigTreeBuilder()
79
    {
80
        $treeBuilder = new TreeBuilder();
81
        $rootNode = $treeBuilder->root(static::ROOT_NODE);
82
83
        // TODO total review to be done see https://github.com/devgiants/websites-backup/projects/1
84
        // add node definitions to the root of the tree
85
        $rootNode
86
            ->children()
87
                ->scalarNode(static::REMANENCE_NODE[static::NODE_NAME])
88
                    ->defaultValue(static::REMANENCE_NODE[static::NODE_DEFAULT_VALUE])
89
                    ->info('Contains the backup folders max value to keep on defined storages')
90
                ->end()
91
		        ->scalarNode(static::LOG_NODE[static::NODE_NAME])
92
			        ->defaultValue(static::LOG_NODE[static::NODE_DEFAULT_VALUE])
93
			        ->info('Contains the backup log folder')
94
		        ->end()
95
                ->arrayNode(static::SITES)
96
                    ->isRequired()
97
                    ->requiresAtLeastOneElement()
98
                    ->prototype('array')
99
                        ->children()
100
                            ->arrayNode(static::PRE_SAVE_COMMANDS)
101
                                ->requiresAtLeastOneElement()
102
                                ->prototype('scalar')->end()
103
                            ->end()
104
                            ->arrayNode(static::DATABASE)
105
                                ->children()
106
                                    ->scalarNode(static::DATABASE_SERVER[static::NODE_NAME])
107
                                        ->defaultValue(static::DATABASE_SERVER[static::NODE_DEFAULT_VALUE])
108
                                        ->info('Contains the MySQL database server IP or domain name. default is localhost')
109
                                    ->end()
110
                                    ->scalarNode(static::USER)
111
                                        ->info('Contains the MySQL user to connect to database with')
112
                                        ->isRequired()
113
                                        ->cannotBeEmpty()
114
                                    ->end()
115
                                    ->scalarNode(static::PASSWORD)
116
                                        ->info('Contains password for MySQL user')
117
                                        ->isRequired()
118
                                        ->cannotBeEmpty()
119
                                    ->end()
120
                                    ->scalarNode(static::NAME)
121
                                        ->info('Contains database name')
122
                                        ->isRequired()
123
                                        ->cannotBeEmpty()
124
                                    ->end()
125
                                ->end()
126
                            ->end()
127
                            ->arrayNode(static::FILES)
128
                                ->children()
129
                                    ->scalarNode(static::ROOT_DIR)
130
                                        ->info('Contains the root directory for backup')
131
                                        ->isRequired()
132
                                        ->cannotBeEmpty()
133
                                    ->end()
134
                                    ->arrayNode(static::FOLDERS_TO_INCLUDE)
135
                                        ->requiresAtLeastOneElement()
136
                                        ->prototype('scalar')->end()
137
                                    ->end()
138
                                    ->arrayNode(static::FOLDERS_TO_EXCLUDE)
139
                                        ->requiresAtLeastOneElement()
140
                                        ->prototype('scalar')->end()
141
                                    ->end()
142
                                ->end()
143
                            ->end()
144
	                        // TODO make checks to ensure storage is defined
145
					        ->arrayNode(static::BACKUP_STORAGES)
146
						        ->requiresAtLeastOneElement()
147
						        ->prototype('scalar')->end()
148
					        ->end()
149
                            ->arrayNode(static::POST_SAVE_COMMANDS)
150
                                ->requiresAtLeastOneElement()
151
                                ->prototype('scalar')->end()
152
                            ->end()
153
                        ->end()
154
                    ->end()
155
                ->end()
156
                ->arrayNode(static::BACKUP_STORAGES)
157
                    ->isRequired()
158
                    ->requiresAtLeastOneElement()
159
                    ->prototype('array')
160
                        ->children()
161
                            // TODO add checks if true to contextualize options with storage type
162
                            ->enumNode(static::STORAGE_TYPE)
163
                                ->info('Contains the storage type for backup')
164
                                ->values(static::AUTHORIZED_STORAGE)
165
                            ->end()
166
                            ->booleanNode(static::SSL)
167
                                ->info('For FTP connections, decides if connection is STP or regular FTP. Default regular.')
168
                                ->defaultFalse()
169
                            ->end()
170
                            ->booleanNode(static::PASSIVE)
171
                                ->info('For FTP connections, decides if transfer mode is passive or not. Default passive')
172
                                ->defaultTrue()
173
                            ->end()
174
                            ->integerNode(static::TRANSFER)
175
                                ->info('For FTP connections, decides transfer type. Default FTP_BINARY')
176
                                ->defaultValue(FTP_BINARY)
177
                            ->end()
178
                            ->scalarNode(static::SERVER)
179
                                ->info('Contains the server to connect with')
180
//                                ->isRequired()
181
                                ->cannotBeEmpty()
182
                            ->end()
183
                            ->scalarNode(static::USER)
184
                                ->info('Contains the user to connect server with')
185
//                                ->isRequired()
186
                                ->cannotBeEmpty()
187
                            ->end()
188
                            ->scalarNode(static::PASSWORD)
189
                                ->info('Contains the password to connect server with')
190
//                                ->isRequired()
191
                                ->cannotBeEmpty()
192
                            ->end()
193
                            ->scalarNode(static::ROOT_DIR)
194
                                ->info('Contains the root dir on remote server to start with')
195
                                ->isRequired()
196
                                ->cannotBeEmpty()
197
                            ->end()
198
					        ->scalarNode(static::CLIENT_ID)
199
						        ->info('The Dropbox app client id')
200
//						        ->isRequired()
201
						        ->cannotBeEmpty()
202
					        ->end()
203
					        ->scalarNode(static::CLIENT_SECRET)
204
						        ->info('The Dropbox app client secret')
205
//						        ->isRequired()
206
						        ->cannotBeEmpty()
207
					        ->end()
208
					        ->scalarNode(static::ACCESS_TOKEN)
209
						        ->info('The Dropbox app access token')
210
//						        ->isRequired()
211
						        ->cannotBeEmpty()
212
					        ->end()
213
                        ->end()
214
                ->end()
215
        ;
216
217
        return $treeBuilder;
218
    }
219
}