Completed
Push — develop ( 7f6ad2...eb155e )
by Baptiste
02:38
created

InnmindNeo4jExtension::configureConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 21
cp 0
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 2
crap 2
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
    public function load(array $configs, ContainerBuilder $container)
20
    {
21
        $configuration = new Configuration();
22
        $config = $this->processConfiguration($configuration, $configs);
23
        $loader = new Loader\YamlFileLoader(
24
            $container,
25
            new FileLocator(__DIR__ . '/../Resources/config')
26
        );
27
        $loader->load('services.yml');
28
29
        $this
30
            ->injectPersister(
31
                $container,
32
                $config['persister']
33
            )
34
            ->configureConnection(
35
                $container,
36
                $config['connection']
37
            )
38
            ->registerTypes(
39
                $container,
40
                $config['types']
41
            )
42
            ->injectMetadataConfiguration(
43
                $container,
44
                $config['metadata_configuration']
45
            );
46
    }
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
    private function injectPersister(
57
        ContainerBuilder $container,
58
        string $service
59
    ): self {
60
        $container
61
            ->getDefinition('innmind_neo4j.unit_of_work')
62
            ->replaceArgument(5, new Reference($service));
63
64
        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
    private function configureConnection(
76
        ContainerBuilder $container,
77
        array $config
78
    ): self {
79
        $transactions = $container->getDefinition('innmind_neo4j.connection.transactions');
80
        $transport = $container->getDefinition('innmind_neo4j.connection.transport');
81
        $server = $container->getDefinition('innmind_neo4j.connection.server');
82
        $authentication = $container->getDefinition('innmind_neo4j.connection.authentication');
83
84
        $server
85
            ->replaceArgument(0, $config['scheme'])
86
            ->replaceArgument(1, $config['host'])
87
            ->replaceArgument(2, $config['port']);
88
        $authentication
89
            ->replaceArgument(0, $config['user'])
90
            ->replaceArgument(1, $config['password']);
91
        $transactions
92
            ->replaceArgument(2, $config['timeout']);
93
        $transport
94
            ->replaceArgument(4, $config['timeout']);
95
96
        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
    private function registerTypes(
108
        ContainerBuilder $container,
109
        array $types
110
    ): self {
111
        $definition = $container->getDefinition('innmind_neo4j.types');
112
113
        foreach ($types as $class) {
114
            $definition->addMethodCall('register', [$class]);
115
        }
116
117
        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
    private function injectMetadataConfiguration(
129
        ContainerBuilder $container,
130
        string $config
131
    ): self {
132
        $container
133
            ->getDefinition('innmind_neo4j.metadata_builder')
134
            ->replaceArgument(2, new Reference($config));
135
136
        return $this;
137
    }
138
}
139