Completed
Pull Request — master (#13)
by Kevin
04:46 queued 02:56
created

ZenstruckBackupExtension   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 9
dl 0
loc 116
ccs 56
cts 58
cp 0.9655
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C load() 0 72 7
A getConfiguration() 0 14 1
A getServices() 0 13 2
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
        } else {
36
            // <2.6
37
            $abstractProfile->setFactoryService('zenstruck_backup.profile_builder')
0 ignored issues
show
Bug introduced by
The method setFactoryService() does not exist on Symfony\Component\DependencyInjection\Definition. Did you maybe mean setFactory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

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
        }
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
        }
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
        }
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
        }
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'));
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
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
        }
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
        );
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
        }
128
129 5
        return $services;
130
    }
131
}
132