Issues (5)

src/DependencyInjection/InnmindNeo4jExtension.php (2 issues)

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
final class InnmindNeo4jExtension extends Extension
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 48
    public function load(array $configs, ContainerBuilder $container)
20
    {
21 48
        $configuration = new Configuration();
22 48
        $config = $this->processConfiguration($configuration, $configs);
23 48
        $loader = new Loader\YamlFileLoader(
24 48
            $container,
25 48
            new FileLocator(__DIR__ . '/../Resources/config')
26
        );
27 48
        $loader->load('services.yml');
28
29
        $this
30 48
            ->injectPersister($container, $config['persister'])
31 48
            ->configureConnection($container, $config['connection'])
32 48
            ->registerTypes($container, $config['types'])
33 48
            ->injectMetadataConfiguration(
34 48
                $container,
35 48
                $config['metadata_configuration']
36
            )
37 48
            ->injectClock($container, $config['clock'])
38 48
            ->aliasEventBus($container, $config['event_bus'])
39 48
            ->aliasDbalConnection($container, $config['dbal_connection']);
40 48
    }
41
42
    /**
43
     * Inject the defined service in the unit of work
44
     *
45
     * @param ContainerBuilder $container
46
     * @param string $service
47
     *
48
     * @return self
49
     */
50 48
    private function injectPersister(
51
        ContainerBuilder $container,
52
        string $service
53
    ): self {
54
        $container
55 48
            ->getDefinition('innmind_neo4j.unit_of_work')
56 48
            ->replaceArgument(5, new Reference($service));
57
58 48
        return $this;
59
    }
60
61
    /**
62
     * Inject value objects to make the connection work
63
     *
64
     * @param ContainerBuilder $container
65
     * @param array $config
66
     *
67
     * @return self
68
     */
69 48
    private function configureConnection(
70
        ContainerBuilder $container,
71
        array $config
72
    ): self {
73 48
        $transactions = $container->getDefinition('innmind_neo4j.dbal.connection.transactions');
0 ignored issues
show
The assignment to $transactions is dead and can be removed.
Loading history...
74 48
        $transport = $container->getDefinition('innmind_neo4j.dbal.connection.transport');
0 ignored issues
show
The assignment to $transport is dead and can be removed.
Loading history...
75 48
        $server = $container->getDefinition('innmind_neo4j.dbal.connection.server');
76 48
        $authentication = $container->getDefinition('innmind_neo4j.dbal.connection.authentication');
77
78
        $server
79 48
            ->replaceArgument(0, $config['scheme'])
80 48
            ->replaceArgument(1, $config['host'])
81 48
            ->replaceArgument(2, $config['port']);
82
        $authentication
83 48
            ->replaceArgument(0, $config['user'])
84 48
            ->replaceArgument(1, $config['password']);
85
86 48
        return $this;
87
    }
88
89
    /**
90
     * Register the classes as added property types
91
     *
92
     * @param ContainerBuilder $container
93
     * @param array $types
94
     *
95
     * @return self
96
     */
97 48
    private function registerTypes(
98
        ContainerBuilder $container,
99
        array $types
100
    ): self {
101 48
        $definition = $container->getDefinition('innmind_neo4j.types');
102
103 48
        foreach ($types as $class) {
104 28
            $definition->addArgument($class);
105
        }
106
107 48
        return $this;
108
    }
109
110
    /**
111
     * Inject the configuration object into the metadata builder
112
     *
113
     * @param ContainerBuilder $container
114
     * @param string $config
115
     *
116
     * @return self
117
     */
118 48
    private function injectMetadataConfiguration(
119
        ContainerBuilder $container,
120
        string $config
121
    ): self {
122
        $container
123 48
            ->getDefinition('innmind_neo4j.metadata_builder')
124 48
            ->replaceArgument(2, new Reference($config));
125
126 48
        return $this;
127
    }
128
129
    /**
130
     * Inject the clock in the transactions service
131
     *
132
     * @param ContainerBuilder $container
133
     * @param string $clock
134
     *
135
     * @return self
136
     */
137 48
    private function injectClock(
138
        ContainerBuilder $container,
139
        string $clock
140
    ): self {
141
        $container
142 48
            ->getDefinition('innmind_neo4j.dbal.connection.transactions')
143 48
            ->replaceArgument(1, new Reference($clock));
144
145 48
        return $this;
146
    }
147
148
    /**
149
     * Create the alias for the event bus
150
     *
151
     * @param ContainerBuilder $container
152
     * @param string $eventBus
153
     *
154
     * @return self
155
     */
156 48
    private function aliasEventBus(
157
        ContainerBuilder $container,
158
        string $eventBus
159
    ): self {
160 48
        $container->setAlias('innmind_neo4j.event_bus', $eventBus);
161
162 48
        return $this;
163
    }
164
165
    /**
166
     * Create the alias for dbal connection
167
     *
168
     * @param ContainerBuilder $container
169
     * @param string $connection
170
     *
171
     * @return self
172
     */
173 48
    private function aliasDbalConnection(
174
        ContainerBuilder $container,
175
        string $connection
176
    ): self {
177 48
        $container->setAlias('innmind_neo4j.dbal.connection', $connection);
178
179 48
        return $this;
180
    }
181
}
182