Common::mapConfig()   F
last analyzed

Complexity

Conditions 19
Paths > 20000

Size

Total Lines 68

Duplication

Lines 10
Ratio 14.71 %

Importance

Changes 0
Metric Value
cc 19
nc 262144
nop 3
dl 10
loc 68
rs 0.3499
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Parser;
8
9
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\AbstractParser;
10
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\ContextualizerInterface;
11
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Suggestion\ConfigSuggestion;
12
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Suggestion\Collector\SuggestionCollectorAwareInterface;
13
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Suggestion\Collector\SuggestionCollectorInterface;
14
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
15
16
/**
17
 * Configuration parser handling all basic configuration (aka "common").
18
 */
19
class Common extends AbstractParser implements SuggestionCollectorAwareInterface
20
{
21
    /** @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Suggestion\Collector\SuggestionCollectorInterface */
22
    private $suggestionCollector;
23
24
    /**
25
     * Adds semantic configuration definition.
26
     *
27
     * @param \Symfony\Component\Config\Definition\Builder\NodeBuilder $nodeBuilder Node just under ezpublish.system.<siteaccess>
28
     */
29
    public function addSemanticConfig(NodeBuilder $nodeBuilder)
30
    {
31
        $nodeBuilder
32
            ->scalarNode('repository')->info('The repository to use. Choose among ezpublish.repositories.')->end()
33
            // @deprecated
34
            // Use ezpublish.repositories / repository settings instead.
35
            ->arrayNode('database')
36
                ->info('DEPRECATED. Use ezpublish.repositories / repository settings instead.')
37
                ->children()
38
                    ->enumNode('type')->values(['mysql', 'pgsql', 'sqlite'])->info('The database driver. Can be mysql, pgsql or sqlite.')->end()
39
                    ->scalarNode('server')->end()
40
                    ->scalarNode('port')->end()
41
                    ->scalarNode('user')->cannotBeEmpty()->end()
42
                    ->scalarNode('password')->end()
43
                    ->scalarNode('database_name')->cannotBeEmpty()->end()
44
                    ->scalarNode('charset')->defaultValue('utf8')->end()
45
                    ->scalarNode('socket')->end()
46
                    ->arrayNode('options')
47
                        ->info('Arbitrary options, supported by your DB driver ("driver-opts" in PDO)')
48
                        ->example(['foo' => 'bar', 'someOptionName' => ['one', 'two', 'three']])
49
                        ->useAttributeAsKey('key')
50
                        ->prototype('variable')->end()
51
                    ->end()
52
                    ->scalarNode('dsn')->info('Full database DSN. Will replace settings above.')->example('mysql://root:root@localhost:3306/ezdemo')->end()
53
                ->end()
54
            ->end()
55
            ->scalarNode('cache_service_name')
56
                ->example('cache.app')
57
                ->info('The cache pool service name to use for a siteaccess / siteaccess-group, *must* be present.')
58
            ->end()
59
            ->scalarNode('var_dir')
60
                ->cannotBeEmpty()
61
                ->example('var/ezdemo_site')
62
                ->info('The directory relative to web/ where files are stored. Default value is "var"')
63
            ->end()
64
            ->arrayNode('api_keys')
65
                ->info('Collection of API keys')
66
                ->addDefaultsIfNotSet()
67
                ->children()
68
                    ->scalarNode('google_maps')
69
                        ->setDeprecated('The child node "%node%" at path "%path%" is no longer used and deprecated.')
70
                        ->info('Google Maps API Key, required as of Google Maps v3 to make sure maps show up correctly.')
71
                    ->end()
72
                ->end()
73
            ->end()
74
            ->scalarNode('storage_dir')
75
                ->cannotBeEmpty()
76
                ->info("Directory where to place new files for storage, it's relative to var directory. Default value is 'storage'")
77
            ->end()
78
            ->scalarNode('binary_dir')
79
                ->cannotBeEmpty()
80
                ->info('Directory where binary files (from ezbinaryfile field type) are stored. Default value is "original"')
81
            ->end()
82
            // @deprecated since 5.3. Will be removed in 6.x.
83
            ->scalarNode('session_name')
84
                ->info('DEPRECATED. Use session.name instead.')
85
            ->end()
86
            ->arrayNode('session')
87
                ->info('Session options. Will override options defined in Symfony framework.session.*')
88
                ->children()
89
                    ->scalarNode('name')
90
                        ->info('The session name. If you want a session name per siteaccess, use "{siteaccess_hash}" token. Will override default session name from framework.session.name')
91
                        ->example(['session' => ['name' => 'eZSESSID{siteaccess_hash}']])
92
                    ->end()
93
                    ->scalarNode('cookie_lifetime')->end()
94
                    ->scalarNode('cookie_path')->end()
95
                    ->scalarNode('cookie_domain')->end()
96
                    ->booleanNode('cookie_secure')->end()
97
                    ->booleanNode('cookie_httponly')->end()
98
                ->end()
99
            ->end()
100
            ->scalarNode('pagelayout')
101
                ->info('The default layout to use')
102
                ->example('AppBundle::pagelayout.html.twig')
103
                ->setDeprecated('The "pagelayout" option is deprecated. Use "page_layout" instead.')
104
            ->end()
105
            ->scalarNode('page_layout')
106
                ->info('The default layout to use')
107
                ->example('AppBundle::page_layout.html.twig')
108
            ->end()
109
            ->scalarNode('index_page')
110
                ->info('The page that the index page will show. Default value is null.')
111
                ->example('/Getting-Started')
112
            ->end()
113
            ->scalarNode('default_page')
114
                ->info('The default page to show, e.g. after user login this will be used for default redirection. If provided, will override "default_target_path" from security.yml.')
115
                ->example('/Getting-Started')
116
            ->end()
117
            ->arrayNode('http_cache')
118
                ->info('Settings related to Http cache')
119
                ->children()
120
                    ->arrayNode('purge_servers')
121
                        ->info('Servers to use for Http PURGE (will NOT be used if ezpublish.http_cache.purge_type is "local").')
122
                        ->example(['http://localhost/', 'http://another.server/'])
123
                        ->requiresAtLeastOneElement()
124
                        ->prototype('scalar')->end()
125
                    ->end()
126
                ->end()
127
            ->end()
128
            ->scalarNode('anonymous_user_id')
129
                ->cannotBeEmpty()
130
                ->example('10')
131
                ->info('The ID of the user used for everyone who is not logged in.')
132
            ->end()
133
            ->arrayNode('user')
134
                ->children()
135
                    ->scalarNode('layout')
136
                        ->info('Layout template to use for user related actions. This is most likely the base pagelayout template of your site.')
137
                        ->example(['layout' => 'eZDemoBundle::pagelayout.html.twig'])
138
                    ->end()
139
                    ->scalarNode('login_template')
140
                        ->info('Template to use for login form. Defaults to EzPublishCoreBundle:security:login.html.twig')
141
                        ->example(['login_template' => 'AcmeTestBundle:User:login.html.twig'])
142
                    ->end()
143
                ->end()
144
            ->end();
145
    }
146
147
    public function preMap(array $config, ContextualizerInterface $contextualizer)
148
    {
149
        $contextualizer->mapConfigArray('session', $config);
150
    }
151
152
    public function mapConfig(array &$scopeSettings, $currentScope, ContextualizerInterface $contextualizer)
153
    {
154
        if (isset($scopeSettings['database'])) {
155
            $this->addDatabaseConfigSuggestion($currentScope, $scopeSettings['database']);
156
        }
157
        if (isset($scopeSettings['repository'])) {
158
            $contextualizer->setContextualParameter('repository', $currentScope, $scopeSettings['repository']);
159
        }
160
        if (isset($scopeSettings['cache_service_name'])) {
161
            $contextualizer->setContextualParameter('cache_service_name', $currentScope, $scopeSettings['cache_service_name']);
162
        }
163
        if (isset($scopeSettings['var_dir'])) {
164
            $contextualizer->setContextualParameter('var_dir', $currentScope, $scopeSettings['var_dir']);
165
        }
166
        if (isset($scopeSettings['storage_dir'])) {
167
            $contextualizer->setContextualParameter('storage_dir', $currentScope, $scopeSettings['storage_dir']);
168
        }
169
        if (isset($scopeSettings['binary_dir'])) {
170
            $contextualizer->setContextualParameter('binary_dir', $currentScope, $scopeSettings['binary_dir']);
171
        }
172
173
        $contextualizer->setContextualParameter('api_keys', $currentScope, $scopeSettings['api_keys']);
174
        foreach ($scopeSettings['api_keys'] as $key => $value) {
175
            $contextualizer->setContextualParameter('api_keys.' . $key, $currentScope, $value);
176
        }
177
178
        // session_name setting is deprecated in favor of session.name
179
        $container = $contextualizer->getContainer();
180
        $sessionOptions = $container->hasParameter("ezsettings.$currentScope.session") ? $container->getParameter("ezsettings.$currentScope.session") : [];
181
        if (isset($sessionOptions['name'])) {
182
            $contextualizer->setContextualParameter('session_name', $currentScope, $sessionOptions['name']);
183
        }
184
        // @deprecated session_name is deprecated, but if present, in addition to session.name, consider it instead (BC).
185
        if (isset($scopeSettings['session_name'])) {
186
            $sessionOptions['name'] = $scopeSettings['session_name'];
187
            $contextualizer->setContextualParameter('session_name', $currentScope, $scopeSettings['session_name']);
188
            $contextualizer->setContextualParameter('session', $currentScope, $sessionOptions);
189
        }
190
191
        if (isset($scopeSettings['http_cache']['purge_servers'])) {
192
            $contextualizer->setContextualParameter('http_cache.purge_servers', $currentScope, $scopeSettings['http_cache']['purge_servers']);
193
        }
194
        if (isset($scopeSettings['anonymous_user_id'])) {
195
            $contextualizer->setContextualParameter('anonymous_user_id', $currentScope, $scopeSettings['anonymous_user_id']);
196
        }
197
        if (isset($scopeSettings['user']['layout'])) {
198
            $contextualizer->setContextualParameter('security.base_layout', $currentScope, $scopeSettings['user']['layout']);
199
        }
200
        if (isset($scopeSettings['user']['login_template'])) {
201
            $contextualizer->setContextualParameter('security.login_template', $currentScope, $scopeSettings['user']['login_template']);
202
        }
203
        if (isset($scopeSettings['index_page'])) {
204
            $contextualizer->setContextualParameter('index_page', $currentScope, $scopeSettings['index_page']);
205
        }
206
        if (isset($scopeSettings['default_page'])) {
207
            $contextualizer->setContextualParameter('default_page', $currentScope, '/' . ltrim($scopeSettings['default_page'], '/'));
208
        }
209 View Code Duplication
        if (isset($scopeSettings['pagelayout'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
210
            // note: "pagelayout" is deprecated, deprecation message is set via Semantic Config Node Builder
211
            $contextualizer->setContextualParameter('pagelayout', $currentScope, $scopeSettings['pagelayout']);
212
            $contextualizer->setContextualParameter('page_layout', $currentScope, $scopeSettings['pagelayout']);
213
        }
214 View Code Duplication
        if (isset($scopeSettings['page_layout'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
            // note: "page_layout" as the new setting always takes precedence
216
            $contextualizer->setContextualParameter('pagelayout', $currentScope, $scopeSettings['page_layout']);
217
            $contextualizer->setContextualParameter('page_layout', $currentScope, $scopeSettings['page_layout']);
218
        }
219
    }
220
221
    /**
222
     * Injects SuggestionCollector.
223
     *
224
     * @param SuggestionCollectorInterface $suggestionCollector
225
     */
226
    public function setSuggestionCollector(SuggestionCollectorInterface $suggestionCollector)
227
    {
228
        $this->suggestionCollector = $suggestionCollector;
229
    }
230
231
    private function addDatabaseConfigSuggestion($sa, array $databaseConfig)
232
    {
233
        $suggestion = new ConfigSuggestion(
234
<<<EOT
235
Database configuration has changed for eZ Content repository.
236
Please define:
237
 - An entry in ezpublish.repositories
238
 - A Doctrine connection (You may check configuration reference for Doctrine "config:dump-reference doctrine" console command.)
239
 - A reference to configured repository in ezpublish.system.$sa.repository
240
EOT
241
        );
242
        $suggestion->setMandatory(true);
243
        $suggestionArray = [
244
            'driver' => 'pdo_mysql',
245
            'host' => 'localhost',
246
            'dbname' => 'my_database',
247
            'user' => 'my_user',
248
            'password' => 'some_password',
249
            'charset' => 'UTF8',
250
        ];
251
252
        if (!empty($databaseConfig)) {
253
            $suggestionArray['dbname'] = $databaseConfig['database_name'];
254
            $suggestionArray['host'] = $databaseConfig['server'];
255
            $driverMap = [
256
                'mysql' => 'pdo_mysql',
257
                'pgsql' => 'pdo_pgsql',
258
                'sqlite' => 'pdo_sqlite',
259
            ];
260
            if (isset($driverMap[$databaseConfig['type']])) {
261
                $suggestionArray['driver'] = $driverMap[$databaseConfig['type']];
262
            } else {
263
                $suggestionArray['driver'] = $databaseConfig['type'];
264
            }
265
            if (isset($databaseConfig['socket'])) {
266
                $suggestionArray['unix_socket'] = $databaseConfig['socket'];
267
            }
268
            $suggestionArray['options'] = $databaseConfig['options'];
269
            $suggestionArray['user'] = $databaseConfig['user'];
270
            $suggestionArray['password'] = $databaseConfig['password'];
271
        }
272
        $suggestion->setSuggestion(
273
            [
274
                'doctrine' => [
275
                    'dbal' => [
276
                        'connections' => [
277
                            'default' => $suggestionArray,
278
                        ],
279
                    ],
280
                ],
281
                'ezpublish' => [
282
                    'repositories' => [
283
                        'my_repository' => [
284
                            'storage' => [
285
                                'engine' => 'legacy',
286
                                'connection' => 'default',
287
                            ],
288
                            'search' => [
289
                                'engine' => 'legacy',
290
                                'connection' => 'default',
291
                            ],
292
                        ],
293
                    ],
294
                    'system' => [
295
                        $sa => [
296
                            'repository' => 'my_repository',
297
                        ],
298
                    ],
299
                ],
300
            ]
301
        );
302
303
        $this->suggestionCollector->addSuggestion($suggestion);
304
    }
305
}
306