Completed
Pull Request — develop (#50)
by A.
03:09
created

parseCookieStorageConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace OpenConext\ProfileBundle\DependencyInjection;
20
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\Config\FileLocator;
23
use Symfony\Component\DependencyInjection\Definition;
24
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
25
use Symfony\Component\DependencyInjection\Loader;
26
use \DateTime;
27
28
class OpenConextProfileExtension extends Extension
29
{
30
    public function load(array $configs, ContainerBuilder $container)
31
    {
32
        $configuration = new Configuration();
33
        $config        = $this->processConfiguration($configuration, $configs);
34
35
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
36
        $loader->load('controllers.yml');
37
        $loader->load('services.yml');
38
39
        $this->parseEngineBlockEntityIdConfiguration($config['engine_block_entity_id'], $container);
40
41
        $this->parseAttributeSupportMailConfiguration($config['attribute_support'], $container);
42
43
        $this->parseDefaultLocaleConfiguration($config['default_locale'], $container);
44
        $this->parseAvailableLocaleConfiguration($config['locales'], $container);
45
        $this->parseLocaleCookieStorageConfiguration(
46
            $config['locale_cookie_domain'],
47
            $config['locale_cookie_key'],
48
            $config['locale_cookie_expires_in'],
49
            $config['locale_cookie_secure'],
50
            $config['locale_cookie_http_only'],
51
            $container
52
        );
53
    }
54
55
    private function parseEngineBlockEntityIdConfiguration($engineBlockEntityId, ContainerBuilder $container)
56
    {
57
        $container
58
            ->getDefinition('profile.engine_block_entity_id')
59
            ->replaceArgument(0, $engineBlockEntityId);
60
    }
61
62
    private function parseAttributeSupportMailConfiguration(array $attributeSupportConfig, ContainerBuilder $container)
63
    {
64
        $container
65
            ->getDefinition('profile.attribute_support.email_from')
66
            ->replaceArgument(0, $attributeSupportConfig['email_from']);
67
        $container
68
            ->getDefinition('profile.attribute_support.email_to')
69
            ->replaceArgument(0, $attributeSupportConfig['email_to']);
70
    }
71
72
    private function parseDefaultLocaleConfiguration($defaultLocaleConfig, ContainerBuilder $container)
73
    {
74
        $container
75
            ->getDefinition('profile.default_locale')
76
            ->replaceArgument(0, $defaultLocaleConfig);
77
    }
78
79
    private function parseAvailableLocaleConfiguration(array $availableLocaleConfig, ContainerBuilder $container)
80
    {
81
        $availableLocales = array_map(function ($availableLocale) {
82
            return new Definition('OpenConext\Profile\Value\Locale', [$availableLocale]);
83
        }, $availableLocaleConfig);
84
85
        $container
86
            ->getDefinition('profile.available_locales')
87
            ->replaceArgument(0, $availableLocales);
88
    }
89
90
    private function parseLocaleCookieStorageConfiguration(
91
        $localeCookieDomain,
92
        $localeCookieKey,
93
        $localeCookieExpiresIn,
94
        $localeCookieSecure,
95
        $localeCookieHttpOnly,
96
        ContainerBuilder $container
97
    ) {
98
99
        if ($localeCookieExpiresIn !== null) {
100
            $localeCookieExpirationDateDefinition = new Definition(DateTime::class);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $localeCookieExpirationDateDefinition exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
101
            $localeCookieExpirationDateDefinition->addMethodCall('modify', [$localeCookieExpiresIn]);
102
        } else {
103
            $localeCookieExpirationDateDefinition = null;
104
        }
105
106
        $container
107
            ->getDefinition('profile.storage.locale_cookie')
108
            ->replaceArgument(0, $localeCookieDomain)
109
            ->replaceArgument(1, $localeCookieKey)
110
            ->replaceArgument(2, $localeCookieExpirationDateDefinition)
111
            ->replaceArgument(3, $localeCookieSecure)
112
            ->replaceArgument(4, $localeCookieHttpOnly);
113
    }
114
}
115