Completed
Push — develop ( c8b131...c09436 )
by Baptiste
04:23
created

InnmindNeo4jExtension::configureGenerators()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 15
loc 15
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
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
final class InnmindNeo4jExtension extends Extension
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 19
    public function load(array $configs, ContainerBuilder $container)
20
    {
21 19
        $configuration = new Configuration();
22 19
        $config = $this->processConfiguration($configuration, $configs);
23 19
        $loader = new Loader\YamlFileLoader(
24
            $container,
25 19
            new FileLocator(__DIR__ . '/../Resources/config')
26
        );
27 19
        $loader->load('services.yml');
28
29
        $this
30 19
            ->injectPersister(
31
                $container,
32 19
                $config['persister']
33
            )
34 19
            ->configureConnection(
35
                $container,
36 19
                $config['connection']
37
            )
38 19
            ->registerTypes(
39
                $container,
40 19
                $config['types']
41
            )
42 19
            ->injectMetadataConfiguration(
43
                $container,
44 19
                $config['metadata_configuration']
45
            );
46 19
    }
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 19
    private function injectPersister(
57
        ContainerBuilder $container,
58
        string $service
59
    ): self {
60
        $container
61 19
            ->getDefinition('innmind_neo4j.unit_of_work')
62 19
            ->replaceArgument(5, new Reference($service));
63
64 19
        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 19
    private function configureConnection(
76
        ContainerBuilder $container,
77
        array $config
78
    ): self {
79 19
        $transactions = $container->getDefinition('innmind_neo4j.dbal.connection.transactions');
0 ignored issues
show
Unused Code introduced by
$transactions is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
80 19
        $transport = $container->getDefinition('innmind_neo4j.dbal.connection.transport');
0 ignored issues
show
Unused Code introduced by
$transport is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81 19
        $server = $container->getDefinition('innmind_neo4j.dbal.connection.server');
82 19
        $authentication = $container->getDefinition('innmind_neo4j.dbal.connection.authentication');
83
84
        $server
85 19
            ->replaceArgument(0, $config['scheme'])
86 19
            ->replaceArgument(1, $config['host'])
87 19
            ->replaceArgument(2, $config['port']);
88
        $authentication
89 19
            ->replaceArgument(0, $config['user'])
90 19
            ->replaceArgument(1, $config['password']);
91
92 19
        return $this;
93
    }
94
95
    /**
96
     * Register the classes as added property types
97
     *
98
     * @param ContainerBuilder $container
99
     * @param array $types
100
     *
101
     * @return self
102
     */
103 19
    private function registerTypes(
104
        ContainerBuilder $container,
105
        array $types
106
    ): self {
107 19
        $definition = $container->getDefinition('innmind_neo4j.types');
108
109 19
        foreach ($types as $class) {
110 9
            $definition->addArgument($class);
111
        }
112
113 19
        return $this;
114
    }
115
116
    /**
117
     * Inject the configuration object into the metadata builder
118
     *
119
     * @param ContainerBuilder $container
120
     * @param string $config
121
     *
122
     * @return self
123
     */
124 19
    private function injectMetadataConfiguration(
125
        ContainerBuilder $container,
126
        string $config
127
    ): self {
128
        $container
129 19
            ->getDefinition('innmind_neo4j.metadata_builder')
130 19
            ->replaceArgument(2, new Reference($config));
131
132 19
        return $this;
133
    }
134
}
135