Completed
Branch master (b23e60)
by Kamil
35:19
created

resolveConfigurationSources()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 5.3846
c 0
b 0
f 0
nc 7
cc 8
eloc 19
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ThemeBundle\DependencyInjection;
13
14
use Sylius\Bundle\ThemeBundle\Configuration\ConfigurationSourceFactoryInterface;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\Config\Loader\LoaderInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
20
use Symfony\Component\DependencyInjection\Extension\Extension;
21
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
22
use Symfony\Component\DependencyInjection\Reference;
23
24
/**
25
 * @author Kamil Kokot <[email protected]>
26
 */
27
final class SyliusThemeExtension extends Extension implements PrependExtensionInterface
28
{
29
    /**
30
     * @var ConfigurationSourceFactoryInterface[]
31
     */
32
    private $configurationSourceFactories = [];
33
34
    /**
35
     * @internal
36
     *
37
     * {@inheritdoc}
38
     */
39
    public function load(array $config, ContainerBuilder $container)
40
    {
41
        $config = $this->processConfiguration($this->getConfiguration($config, $container), $config);
42
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/services'));
43
        $loader->load('services.xml');
44
45
        if ($config['assets']['enabled']) {
46
            $loader->load('support/assets.xml');
47
        }
48
49
        if ($config['templating']['enabled']) {
50
            $loader->load('support/templating.xml');
51
        }
52
53
        if ($config['translations']['enabled']) {
54
            $loader->load('support/translations.xml');
55
        }
56
57
        $this->resolveConfigurationSources($container, $config);
58
59
        $container->setAlias('sylius.context.theme', $config['context']);
60
    }
61
62
    /**
63
     * @internal
64
     *
65
     * {@inheritdoc}
66
     */
67
    public function prepend(ContainerBuilder $container)
68
    {
69
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/services'));
70
71
        $this->prependSyliusSettings($container, $loader);
72
    }
73
74
    /**
75
     * @api
76
     *
77
     * @param ConfigurationSourceFactoryInterface $configurationSourceFactory
78
     */
79
    public function addConfigurationSourceFactory(ConfigurationSourceFactoryInterface $configurationSourceFactory)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $configurationSourceFactory exceeds the maximum configured length of 20.

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

Loading history...
80
    {
81
        $this->configurationSourceFactories[$configurationSourceFactory->getName()] = $configurationSourceFactory;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getConfiguration(array $config, ContainerBuilder $container)
88
    {
89
        $configuration = new Configuration($this->configurationSourceFactories);
90
91
        $container->addObjectResource($configuration);
92
93
        return $configuration;
94
    }
95
96
    /**
97
     * @param ContainerBuilder $container
98
     * @param LoaderInterface $loader
99
     */
100
    private function prependSyliusSettings(ContainerBuilder $container, LoaderInterface $loader)
101
    {
102
        if (!$container->hasExtension('sylius_settings')) {
103
            return;
104
        }
105
106
        $loader->load('integration/settings.xml');
107
    }
108
109
    /**
110
     * @param ContainerBuilder $container
111
     * @param array $config
112
     *
113
     * @return mixed
114
     */
115
    private function resolveConfigurationSources(ContainerBuilder $container, array $config)
116
    {
117
        $configurationProviders = [];
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $configurationProviders exceeds the maximum configured length of 20.

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

Loading history...
118
        foreach ($this->configurationSourceFactories as $configurationSourceFactory) {
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $configurationSourceFactory exceeds the maximum configured length of 20.

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

Loading history...
119
            $sourceName = $configurationSourceFactory->getName();
120
            if (isset($config['sources'][$sourceName]) && $config['sources'][$sourceName]['enabled']) {
121
                $sourceConfig = $config['sources'][$sourceName];
122
123
                $configurationProvider = $configurationSourceFactory->initializeSource($container, $sourceConfig);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $configurationProvider exceeds the maximum configured length of 20.

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

Loading history...
124
125
                if (!$configurationProvider instanceof Reference && !$configurationProvider instanceof Definition) {
126
                    throw new \InvalidArgumentException(sprintf(
127
                        'Source factory "%s" was expected to return an instance of "%s" or "%s", "%s" found',
128
                        $configurationSourceFactory->getName(),
129
                        Reference::class,
130
                        Definition::class,
131
                        is_object($configurationProvider) ? get_class($configurationProvider) : gettype($configurationProvider)
132
                    ));
133
                }
134
135
                $configurationProviders[] = $configurationProvider;
136
            }
137
        }
138
139
        $compositeConfigurationProvider = $container->getDefinition('sylius.theme.configuration.provider');
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $compositeConfigurationProvider exceeds the maximum configured length of 20.

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

Loading history...
140
        $compositeConfigurationProvider->replaceArgument(0, $configurationProviders);
141
142
        foreach ($this->configurationSourceFactories as $configurationSourceFactory) {
143
            $container->addObjectResource($configurationSourceFactory);
144
        }
145
    }
146
}
147