Completed
Push — master ( 067b5f...87ff8b )
by Kevin
02:23
created

ZenstruckBackupExtension::getConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

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