Completed
Push — master ( 87ff8b...485185 )
by Kevin
02:27
created

ZenstruckBackupExtension::load()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 72
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 47
CRAP Score 7.0033

Importance

Changes 7
Bugs 4 Features 1
Metric Value
c 7
b 4
f 1
dl 0
loc 72
ccs 47
cts 49
cp 0.9592
rs 6.7427
cc 7
eloc 48
nc 64
nop 2
crap 7.0033

How to fix   Long Method   

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
namespace Zenstruck\BackupBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\DefinitionDecorator;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\DependencyInjection\Reference;
10
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
11
use Zenstruck\BackupBundle\DependencyInjection\Factory\Factory;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
class ZenstruckBackupExtension extends Extension
17
{
18 5
    public function load(array $configs, ContainerBuilder $container)
19
    {
20 5
        $configuration = $this->getConfiguration($configs, $container);
21 5
        $config = $this->processConfiguration($configuration, $configs);
22
23 2
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
24 2
        $loader->load('services.xml');
25 2
        $loader->load('destinations.xml');
26 2
        $loader->load('namers.xml');
27 2
        $loader->load('processors.xml');
28 2
        $loader->load('sources.xml');
29
30 2
        $abstractProfile = $container->getDefinition('zenstruck_backup.abstract_profile');
31
32 2
        if (method_exists($abstractProfile, 'setFactory')) {
33
            // 2.6+
34 2
            $abstractProfile->setFactory(array(new Reference('zenstruck_backup.profile_builder'), 'create'));
35 2
        } else {
36
            // <2.6
37
            $abstractProfile->setFactoryService('zenstruck_backup.profile_builder')
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Depend...on::setFactoryService() has been deprecated with message: since version 2.6, to be removed in 3.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The method Symfony\Component\Depend...ion::setFactoryMethod() has been deprecated with message: since version 2.6, to be removed in 3.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
38
                ->setFactoryMethod('create');
39
        }
40
41 2
        $container->setDefinition('zenstruck_backup.abstract_profile', $abstractProfile);
42
43 2
        foreach ($config['sources'] as $name => $source) {
44 1
            reset($source);
45
46
            $configuration
47 1
                ->getSourceFactory(key($source))
48 1
                ->create($container, $name, reset($source));
49 2
        }
50
51 2
        foreach ($config['namers'] as $name => $namer) {
52 1
            reset($namer);
53
54
            $configuration
55 1
                ->getNamerFactory(key($namer))
56 1
                ->create($container, $name, reset($namer));
57 2
        }
58
59 2
        foreach ($config['processors'] as $name => $processor) {
60 1
            reset($processor);
61
62
            $configuration
63 1
                ->getProcessorFactory(key($processor))
64 1
                ->create($container, $name, reset($processor));
65 2
        }
66
67 2
        foreach ($config['destinations'] as $name => $destination) {
68 1
            reset($destination);
69
70
            $configuration
71 1
                ->getDestinationFactory(key($destination))
72 1
                ->create($container, $name, reset($destination));
73 2
        }
74
75 2
        foreach ($config['profiles'] as $name => $profile) {
76 1
            $definition = $container->setDefinition(
77 1
                sprintf('zenstruck_backup.profile.%s', $name),
78 1
                new DefinitionDecorator('zenstruck_backup.abstract_profile'));
79
80
            $definition
81 1
                ->replaceArgument(0, $name)
82 1
                ->replaceArgument(1, $profile['scratch_dir'])
83 1
                ->replaceArgument(2, $profile['processor'])
84 1
                ->replaceArgument(3, $profile['namer'])
85 1
                ->replaceArgument(4, $profile['sources'])
86 1
                ->replaceArgument(5, $profile['destinations'])
87 1
                ->addTag('zenstruck_backup.profile');
88 2
        }
89 2
    }
90
91
    /**
92
     * @param array            $config
93
     * @param ContainerBuilder $container
94
     *
95
     * @return Configuration
96
     */
97 5
    public function getConfiguration(array $config, ContainerBuilder $container)
98
    {
99 5
        $tempContainer = new ContainerBuilder();
100
101 5
        $loader = new Loader\XmlFileLoader($tempContainer, new FileLocator(__DIR__.'/../Resources/config'));
102 5
        $loader->load('factories.xml');
103
104 5
        return new Configuration(
105 5
            $this->getServices('zenstruck_backup.namer_factory', $tempContainer),
106 5
            $this->getServices('zenstruck_backup.processor_factory', $tempContainer),
107 5
            $this->getServices('zenstruck_backup.source_factory', $tempContainer),
108 5
            $this->getServices('zenstruck_backup.destination_factory', $tempContainer)
109 5
        );
110
    }
111
112
    /**
113
     * @param string           $tag
114
     * @param ContainerBuilder $container
115
     *
116
     * @return Factory[]
117
     */
118 5
    private function getServices($tag, ContainerBuilder $container)
119
    {
120 5
        $services = array();
121
122 5
        foreach (array_keys($container->findTaggedServiceIds($tag)) as $id) {
123
            /** @var Factory $factory */
124 5
            $factory = $container->get($id);
125
126 5
            $services[$factory->getName()] = $factory;
127 5
        }
128
129 5
        return $services;
130
    }
131
}
132