Completed
Push — develop ( f5bb1e...33cc42 )
by Baptiste
02:31
created

InnmindNeo4jExtension::load()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 15
cts 15
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Neo4jBundle\DependencyInjection;
5
6
use Symfony\Component\{
7
    DependencyInjection\ContainerBuilder,
8
    Config\FileLocator,
9
    HttpKernel\DependencyInjection\Extension,
10
    DependencyInjection\Loader,
11
    DependencyInjection\Reference
12
};
13
14
class InnmindNeo4jExtension extends Extension
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 40
    public function load(array $configs, ContainerBuilder $container)
20
    {
21 40
        $configuration = new Configuration();
22 40
        $config = $this->processConfiguration($configuration, $configs);
23 40
        $loader = new Loader\YamlFileLoader(
24
            $container,
25 40
            new FileLocator(__DIR__ . '/../Resources/config')
26
        );
27 40
        $loader->load('services.yml');
28
29
        $this
30 40
            ->injectPersister(
31
                $container,
32 40
                $config['persister']
33
            )
34 40
            ->configureConnection(
35
                $container,
36 40
                $config['connection']
37
            )
38 40
            ->registerTypes(
39
                $container,
40 40
                $config['types']
41
            )
42 40
            ->injectMetadataConfiguration(
43
                $container,
44 40
                $config['metadata_configuration']
45
            );
46 40
    }
47
48
    /**
49
     * Inject the defined service in the unit of work
50
     *
51
     * @param ContainerBuilder $container
52
     * @param string $service
53
     *
54
     * @return self
55
     */
56 40
    private function injectPersister(
57
        ContainerBuilder $container,
58
        string $service
59
    ): self {
60
        $container
61 40
            ->getDefinition('innmind_neo4j.unit_of_work')
62 40
            ->replaceArgument(5, new Reference($service));
63
64 40
        return $this;
65
    }
66
67
    /**
68
     * Inject value objects to make the connection work
69
     *
70
     * @param ContainerBuilder $container
71
     * @param array $config
72
     *
73
     * @return self
74
     */
75 40
    private function configureConnection(
76
        ContainerBuilder $container,
77
        array $config
78
    ): self {
79 40
        $transactions = $container->getDefinition('innmind_neo4j.connection.transactions');
80 40
        $transport = $container->getDefinition('innmind_neo4j.connection.transport');
81 40
        $server = $container->getDefinition('innmind_neo4j.connection.server');
82 40
        $authentication = $container->getDefinition('innmind_neo4j.connection.authentication');
83
84
        $server
85 40
            ->replaceArgument(0, $config['scheme'])
86 40
            ->replaceArgument(1, $config['host'])
87 40
            ->replaceArgument(2, $config['port']);
88
        $authentication
89 40
            ->replaceArgument(0, $config['user'])
90 40
            ->replaceArgument(1, $config['password']);
91
        $transactions
92 40
            ->replaceArgument(2, $config['timeout']);
93
        $transport
94 40
            ->replaceArgument(4, $config['timeout']);
95
96 40
        return $this;
97
    }
98
99
    /**
100
     * Register the classes as added property types
101
     *
102
     * @param ContainerBuilder $container
103
     * @param array $types
104
     *
105
     * @return self
106
     */
107 40
    private function registerTypes(
108
        ContainerBuilder $container,
109
        array $types
110
    ): self {
111 40
        $definition = $container->getDefinition('innmind_neo4j.types');
112
113 40
        foreach ($types as $class) {
114 16
            $definition->addMethodCall('register', [$class]);
115
        }
116
117 40
        return $this;
118
    }
119
120
    /**
121
     * Inject the configuration object into the metadata builder
122
     *
123
     * @param ContainerBuilder $container
124
     * @param string $config
125
     *
126
     * @return self
127
     */
128 40
    private function injectMetadataConfiguration(
129
        ContainerBuilder $container,
130
        string $config
131
    ): self {
132
        $container
133 40
            ->getDefinition('innmind_neo4j.metadata_builder')
134 40
            ->replaceArgument(2, new Reference($config));
135
136 40
        return $this;
137
    }
138
}
139