Completed
Push — master ( 18c7f8...c29312 )
by Tobias
10:28
created

Neo4jExtension::load()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 16
cts 16
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 4
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Neo4j\Neo4jBundle\DependencyInjection;
6
7
use GraphAware\Bolt\Driver as BoltDriver;
8
use GraphAware\Neo4j\Client\Connection\Connection;
9
use GraphAware\Neo4j\OGM\EntityManager;
10
use GraphAware\Neo4j\Client\HttpDriver\Driver as HttpDriver;
11
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
12
use Symfony\Component\Config\FileLocator;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Definition;
15
use Symfony\Component\DependencyInjection\DefinitionDecorator;
16
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
17
use Symfony\Component\DependencyInjection\Reference;
18
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19
20
/**
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
class Neo4jExtension extends Extension
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 4
    public function load(array $configs, ContainerBuilder $container)
29
    {
30 4
        $configuration = $this->getConfiguration($configs, $container);
31 4
        $config = $this->processConfiguration($configuration, $configs);
1 ignored issue
show
Bug introduced by
It seems like $configuration defined by $this->getConfiguration($configs, $container) on line 30 can be null; however, Symfony\Component\Depend...:processConfiguration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
32
33 4
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
34 4
        $loader->load('services.xml');
35
36 4
        $this->handleConnections($config, $container);
37 4
        $clientServiceIds = $this->handleClients($config, $container);
38
39 4
        if ($this->validateEntityManagers($config)) {
40 4
            $loader->load('entity_manager.xml');
41 4
            $this->handleEntityMangers($config, $container, $clientServiceIds);
42 4
            $container->setAlias('neo4j.entity_manager', 'neo4j.entity_manager.default');
43
        }
44
45
        // add aliases for the default services
46 4
        $container->setAlias('neo4j.connection', 'neo4j.connection.default');
47 4
        $container->setAlias('neo4j.client', 'neo4j.client.default');
48
49
        // Configure toolbar
50 4
        if ($this->isConfigEnabled($container, $config['profiling'])) {
51 2
            $loader->load('data-collector.xml');
52
        }
53 4
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 4
    public function getConfiguration(array $config, ContainerBuilder $container): Configuration
59
    {
60 4
        return new Configuration($container->getParameter('kernel.debug'));
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 4
    public function getAlias(): string
67
    {
68 4
        return 'neo4j';
69
    }
70
71
    /**
72
     * @param array            $config
73
     * @param ContainerBuilder $container
74
     *
75
     * @return array with service ids
76
     */
77 4
    private function handleClients(array &$config, ContainerBuilder $container): array
78
    {
79 4
        if (empty($config['clients'])) {
80
            // Add default entity manager if none set.
81 4
            $config['clients']['default'] = ['connections' => ['default']];
82
        }
83
84 4
        $serviceIds = [];
85 4
        foreach ($config['clients'] as $name => $data) {
86 4
            $serviceIds[$name] = $serviceId = sprintf('neo4j.client.%s', $name);
87 4
            foreach ($data['connections'] as $connectionName) {
88 4
                if (empty($config['connections'][$connectionName])) {
89
                    throw new InvalidConfigurationException(sprintf(
90
                        'Client "%s" is configured to use connection named "%s" but there is no such connection',
91
                        $name,
92
                        $connectionName
93
                    ));
94
                }
95 4
                $connections[] = $connectionName;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$connections was never initialized. Although not strictly required by PHP, it is generally a good practice to add $connections = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
96
            }
97 4
            if (empty($connections)) {
98
                $connections[] = 'default';
0 ignored issues
show
Bug introduced by
The variable $connections does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
99
            }
100
101
            $container
102 4
                ->setDefinition($serviceId, new DefinitionDecorator('neo4j.client.abstract'))
103 4
                ->setArguments([$connections]);
104
        }
105
106 4
        return $serviceIds;
107
    }
108
109
    /**
110
     * @param array            $config
111
     * @param ContainerBuilder $container
112
     * @param array            $clientServiceIds
113
     *
114
     * @return array
115
     */
116 4
    private function handleEntityMangers(array &$config, ContainerBuilder $container, array $clientServiceIds): array
117
    {
118 4
        $serviceIds = [];
119 4
        foreach ($config['entity_managers'] as $name => $data) {
120 4
            $serviceIds[] = $serviceId = sprintf('neo4j.entity_manager.%s', $name);
121 4
            $clientName = $data['client'];
122 4
            if (empty($clientServiceIds[$clientName])) {
123
                throw new InvalidConfigurationException(sprintf(
124
                    'EntityManager "%s" is configured to use client named "%s" but there is no such client',
125
                    $name,
126
                    $clientName
127
                ));
128
            }
129
            $container
130 4
                ->setDefinition($serviceId, new DefinitionDecorator('neo4j.entity_manager.abstract'))
131 4
                ->setArguments([
132 4
                    $container->getDefinition($clientServiceIds[$clientName]),
133 4
                    empty($data['cache_dir']) ? $container->getParameter('kernel.cache_dir').'/neo4j' : $data['cache_dir'],
134
                ]);
135
        }
136
137 4
        return $serviceIds;
138
    }
139
140
    /**
141
     * @param array            $config
142
     * @param ContainerBuilder $container
143
     *
144
     * @return array with service ids
145
     */
146 4
    private function handleConnections(array &$config, ContainerBuilder $container): array
147
    {
148 4
        $serviceIds = [];
149 4
        $firstName = null;
150 4
        foreach ($config['connections'] as $name => $data) {
151 4
            if ($firstName === null || $name === 'default') {
152 4
                $firstName = $name;
153
            }
154 4
            $def = new Definition(Connection::class);
155 4
            $def->addArgument($name);
156 4
            $def->addArgument($this->getUrl($data));
157 4
            $serviceIds[$name] = $serviceId = 'neo4j.connection.'.$name;
158 4
            $container->setDefinition($serviceId, $def);
159
        }
160
161
        // Make sure we got a 'default'
162 4
        if ($firstName !== 'default') {
163
            $config['connections']['default'] = $config['connections'][$firstName];
164
        }
165
166
        // Add connections to connection manager
167 4
        $connectionManager = $container->getDefinition('neo4j.connection_manager');
168 4
        foreach ($serviceIds as $name => $serviceId) {
169 4
            $connectionManager->addMethodCall('registerExistingConnection', [$name, new Reference($serviceId)]);
170
        }
171 4
        $connectionManager->addMethodCall('setMaster', [$firstName]);
172
173 4
        return $serviceIds;
174
    }
175
176
    /**
177
     * Get URL form config.
178
     *
179
     * @param array $config
180
     *
181
     * @return string
182
     */
183 4
    private function getUrl(array $config): string
184
    {
185 4
        return sprintf(
186 4
            '%s://%s:%s@%s:%d',
187 4
            $config['schema'],
188 4
            $config['username'],
189 4
            $config['password'],
190 4
            $config['host'],
191 4
            $this->getPort($config)
192
        );
193
    }
194
195
    /**
196
     * Return the correct default port if not manually set.
197
     *
198
     * @param array $config
199
     *
200
     * @return int
201
     */
202 4
    private function getPort(array $config)
203
    {
204 4
        if (isset($config['port'])) {
205 3
            return $config['port'];
206
        }
207
208 1
        return 'http' == $config['schema'] ? HttpDriver::DEFAULT_HTTP_PORT : BoltDriver::DEFAULT_TCP_PORT;
209
    }
210
211
    /**
212
     * Make sure the EntityManager is installed if we have configured it.
213
     *
214
     * @param array &$config
215
     *
216
     * @return bool true if "graphaware/neo4j-php-ogm" is installed
217
     *
218
     * @thorws \LogicException if EntityManagers os not installed but they are configured.
219
     */
220 4
    private function validateEntityManagers(array &$config): bool
221
    {
222 4
        $dependenciesInstalled = class_exists(EntityManager::class);
223 4
        $entityManagersConfigured = !empty($config['entity_managers']);
224
225 4
        if ($dependenciesInstalled && !$entityManagersConfigured) {
226
            // Add default entity manager if none set.
227 4
            $config['entity_managers']['default'] = ['client' => 'default'];
228
        } elseif (!$dependenciesInstalled && $entityManagersConfigured) {
229
            throw new \LogicException(
230
                'You need to install "graphaware/neo4j-php-ogm" to be able to use the EntityManager'
231
            );
232
        }
233
234 4
        return $dependenciesInstalled;
235
    }
236
}
237