Passed
Branch ci_setup (6efa95)
by Mattia
12:10 queued 13s
created

PatchManagerExtension::handleData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 3
dl 0
loc 14
ccs 0
cts 10
cp 0
crap 6
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Cypress\PatchManager\Bundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Loader;
8
use Symfony\Component\DependencyInjection\Reference;
9
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
10
11
class PatchManagerExtension extends Extension
12
{
13
    /**
14
     * Loads a specific configuration.
15
     *
16
     * @param array $configs An array of configuration values
17
     * @param ContainerBuilder $container A ContainerBuilder instance
18
     *
19
     * @throws \InvalidArgumentException When provided tag is not defined in this extension
20
     *
21
     * @api
22
     */
23
    public function load(array $configs, ContainerBuilder $container): void
24
    {
25
        $configuration = new Configuration();
26
        $config = $this->processConfiguration($configuration, $configs);
27
28
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
29
        $loaderHandlers = new Loader\XmlFileLoader(
30
            $container,
31
            new FileLocator(__DIR__.'/../Resources/config/handlers')
32
        );
33
        $loader->load('services.xml');
34
        $this->handleGenericConfig($config, $container, $loaderHandlers);
35
    }
36
37
    /**
38
     * @param array $config
39
     * @param ContainerBuilder $container
40
     * @param Loader\XmlFileLoader $loaderHandlers
41
     */
42
    private function handleGenericConfig(
43
        array $config,
44
        ContainerBuilder $container,
45
        Loader\XmlFileLoader $loaderHandlers
46
    ): void {
47
        if ($config['dispatch_events']) {
48
            $patchManagerDefinition = $container->getDefinition('patch_manager');
49
            $patchManagerDefinition->addMethodCall(
50
                'setEventDispatcherInterface',
51
                [new Reference('event_dispatcher')]
52
            );
53
        }
54
        if (!is_null($config['alias'])) {
55
            $container->setAlias($config['alias'], 'patch_manager');
56
        }
57
        if (array_key_exists('data', $config['handlers'])) {
58
            $this->handleData($config, $loaderHandlers, $container);
59
        }
60
        if (array_key_exists('state_machine', $config['handlers'])) {
61
            $this->handleStateMachine($loaderHandlers, $container);
62
        }
63
        $container->setParameter('patch_manager.strict_mode', $config['strict_mode']);
64
    }
65
66
    /**
67
     * @param array $config
68
     * @param Loader\XmlFileLoader $loaderHandlers
69
     * @param ContainerBuilder $container
70
     */
71
    private function handleData(array $config, Loader\XmlFileLoader $loaderHandlers, ContainerBuilder $container): void
72
    {
73
        if ($config['handlers']['data']['doctrine']) {
74
            $loaderHandlers->load('data_doctrine.xml');
75
            $doctrineEMName = sprintf('doctrine.orm.%s_entity_manager', $config['handlers']['data']['entity_manager']);
76
            $dataDoctrineDefinition = $container->getDefinition('patch_manager.handler.data');
77
            $dataDoctrineDefinition->addArgument(
78
                new Reference($doctrineEMName)
79
            );
80
        } else {
81
            $loaderHandlers->load('data.xml');
82
        }
83
        $dataHandlerDefinition = $container->getDefinition('patch_manager.handler.data');
84
        $dataHandlerDefinition->addTag('patch_manager.handler');
85
    }
86
87
    /**
88
     * @param Loader\XmlFileLoader $loaderHandlers
89
     * @param ContainerBuilder $container
90
     */
91
    private function handleStateMachine(Loader\XmlFileLoader $loaderHandlers, ContainerBuilder $container): void
92
    {
93
        if (!interface_exists('Finite\Factory\FactoryInterface')) {
94
            $msg = 'If you want to use the patch manager with "op": "sm" you should install ';
95
            $msg .= 'the finite library. See https://github.com/yohang/Finite';
96
97
            throw new \RuntimeException($msg);
98
        }
99
        $loaderHandlers->load('state_machine.xml');
100
        $smHandlerDefinition = $container->getDefinition('patch_manager.handler.state_machine');
101
        $smHandlerDefinition->addTag('patch_manager.handler');
102
    }
103
}
104