Completed
Push — qa-cumulative-ezp-31278-31279-... ( f57b9c...05fe70 )
by
unknown
14:35
created

IO::postMap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Parser;
10
11
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\AbstractParser;
12
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ComplexSettings\ComplexSettingParserInterface;
13
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\ContextualizerInterface;
14
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
17
class IO extends AbstractParser
18
{
19
    /** @var ComplexSettingParserInterface */
20
    private $complexSettingParser;
21
22
    public function __construct(ComplexSettingParserInterface $complexSettingParser)
23
    {
24
        $this->complexSettingParser = $complexSettingParser;
25
    }
26
27
    public function addSemanticConfig(NodeBuilder $nodeBuilder)
28
    {
29
        $nodeBuilder
30
            ->arrayNode('io')
31
                ->info('Binary storage options')
32
                ->children()
33
                    ->scalarNode('metadata_handler')
34
                        ->info('Handler uses to manipulate IO files metadata')
35
                        ->example('default')
36
                    ->end()
37
                    ->scalarNode('binarydata_handler')
38
                        ->info('Handler uses to manipulate IO files binarydata')
39
                        ->example('default')
40
                    ->end()
41
                    ->scalarNode('url_prefix')
42
                        ->info('Prefix added to binary files uris. A host can also be added')
43
                        ->example('$var_dir$/$storage_dir$, http://static.example.com/')
44
                    ->end()
45
                    ->arrayNode('permissions')
46
                        ->info('Permissions applied by the Local flysystem adapter when creating content files and directories in storage.')
47
                        ->children()
48
                            ->scalarNode('files')
49
                                ->defaultValue('0644')
50
                            ->end()
51
                            ->scalarNode('directories')
52
                                ->defaultValue('0755')
53
                            ->end()
54
                        ->end()
55
                    ->end()
56
                ->end()
57
            ->end();
58
    }
59
60
    public function mapConfig(array &$scopeSettings, $currentScope, ContextualizerInterface $contextualizer)
61
    {
62
        if (!isset($scopeSettings['io'])) {
63
            return;
64
        }
65
66
        $settings = $scopeSettings['io'];
67
        if (isset($settings['metadata_handler'])) {
68
            $contextualizer->setContextualParameter('io.metadata_handler', $currentScope, $settings['metadata_handler']);
69
        }
70
        if (isset($settings['binarydata_handler'])) {
71
            $contextualizer->setContextualParameter('io.binarydata_handler', $currentScope, $settings['binarydata_handler']);
72
        }
73
        if (isset($settings['url_prefix'])) {
74
            $contextualizer->setContextualParameter('io.url_prefix', $currentScope, $settings['url_prefix']);
75
        }
76
        if (isset($settings['permissions'])) {
77
            if (isset($settings['permissions']['files'])) {
78
                $contextualizer->setContextualParameter('io.permissions.files', $currentScope, $settings['permissions']['files']);
79
            }
80
            if (isset($settings['permissions']['directories'])) {
81
                $contextualizer->setContextualParameter('io.permissions.directories', $currentScope, $settings['permissions']['directories']);
82
            }
83
        }
84
    }
85
86
    /**
87
     * Post process configuration to add io_root_dir and io_prefix.
88
     */
89
    public function postMap(array $config, ContextualizerInterface $contextualizer)
90
    {
91
        $container = $contextualizer->getContainer();
92
93
        // complex parameters dependencies
94
        foreach (array_merge($config['siteaccess']['list'], array_keys($config['siteaccess']['groups'])) as $scope) {
95
            $this->addComplexParametersDependencies('io.url_prefix', $scope, $container);
0 ignored issues
show
Compatibility introduced by
$container of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ction\ContainerBuilder>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
96
            $this->addComplexParametersDependencies('io.legacy_url_prefix', $scope, $container);
0 ignored issues
show
Compatibility introduced by
$container of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ction\ContainerBuilder>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
97
            $this->addComplexParametersDependencies('io.root_dir', $scope, $container);
0 ignored issues
show
Compatibility introduced by
$container of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ction\ContainerBuilder>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
98
        }
99
    }
100
101
    /**
102
     * Applies dependencies of complex $parameter in $scope.
103
     */
104
    private function addComplexParametersDependencies($parameter, $scope, ContainerBuilder $container)
105
    {
106
        // The complex setting exists in this scope, we don't need to do anything
107
        if ($container->hasParameter("ezsettings.$scope.$parameter")) {
108
            return;
109
        }
110
        $parameterValue = $container->getParameter("ezsettings.default.$parameter");
111
112
        // not complex in this scope
113
        if (!$this->complexSettingParser->containsDynamicSettings($parameterValue)) {
114
            return;
115
        }
116
117
        // if one of the complex parameters dependencies is set in the current scope,
118
        // we set the complex parameter in the current scope as well.
119
        foreach ($this->complexSettingParser->parseComplexSetting($parameterValue) as $dynamicParameter) {
120
            $dynamicParameterParts = $this->complexSettingParser->parseDynamicSetting($dynamicParameter);
121
            if ($dynamicParameterParts['scope'] === $scope) {
122
                continue;
123
            }
124
            $dynamicParameterId = sprintf(
125
                '%s.%s.%s',
126
                $dynamicParameterParts['namespace'] ?: 'ezsettings',
127
                $scope,
128
                $dynamicParameterParts['param']
129
            );
130
            if ($container->hasParameter($dynamicParameterId)) {
131
                $container->setParameter("ezsettings.$scope.$parameter", $parameterValue);
132
                break;
133
            }
134
        }
135
    }
136
}
137