Completed
Push — symfony3-wololo-packages ( e06be3...2be468 )
by Kamil
19:36 queued 02:48
created

SyliusThemeExtension   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 8
dl 0
loc 120
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A prepend() 0 6 1
A addConfigurationSourceFactory() 0 4 1
A getConfiguration() 0 8 1
C resolveConfigurationSources() 0 31 8
B load() 0 22 4
A prependTwig() 0 8 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\Extension;
20
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
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);
0 ignored issues
show
Bug introduced by
It seems like $this->getConfiguration($config, $container) can be null; however, processConfiguration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
42
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
43
        $loader->load('services.xml');
44
45
        if ($config['assets']['enabled']) {
46
            $loader->load('services/integrations/assets.xml');
47
        }
48
49
        if ($config['templating']['enabled']) {
50
            $loader->load('services/integrations/templating.xml');
51
        }
52
53
        if ($config['translations']['enabled']) {
54
            $loader->load('services/integrations/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'));
70
71
        $this->prependTwig($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 prependTwig(ContainerBuilder $container, LoaderInterface $loader)
101
    {
102
        if (!$container->hasExtension('twig')) {
103
            return;
104
        }
105
106
        $loader->load('services/integrations/twig.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