Completed
Push — master ( f20bc4...886a11 )
by Baptiste
02:52 queued 43s
created

InnmindNeo4jExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 1 Features 3
Metric Value
wmc 6
lcom 0
cbo 9
dl 0
loc 129
ccs 45
cts 45
cp 1
rs 10
c 9
b 1
f 3

5 Methods

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