Completed
Push — master ( 7a6bdd...324ce1 )
by Sébastien
09:34
created

ShardingConnectionFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 15
c 1
b 0
f 0
dl 0
loc 48
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 18 2
A __construct() 0 3 1
A support() 0 3 1
1
<?php
2
3
namespace Bdf\Prime\Connection\Factory;
4
5
use Bdf\Prime\Connection\ConnectionInterface;
6
use Bdf\Prime\Sharding\ShardingConnection;
7
use Doctrine\DBAL\Configuration;
8
9
/**
10
 * ShardingConnection
11
 */
12
class ShardingConnectionFactory implements ConnectionFactoryInterface
13
{
14
    /**
15
     * The delegated loader
16
     *
17
     * @var ConnectionFactoryInterface
18
     */
19
    private $connectionFactory;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
20
21
    /**
22
     * Set default configuration
23
     *
24
     * @param ConnectionFactoryInterface $connectionFactory
25
     */
26 123
    public function __construct(ConnectionFactoryInterface $connectionFactory)
27
    {
28 123
        $this->connectionFactory = $connectionFactory;
29 123
    }
30
31
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $connectionName should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $parameters should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
32
     * {@inheritDoc}
33
     */
34 73
    public function create(string $connectionName, array $parameters, Configuration $config): ConnectionInterface
35
    {
36 73
        $allParameters = $parameters['shards'];
37 73
        unset($parameters['shards']);
38
39 73
        $globalParameters = $parameters;
40 73
        unset($globalParameters['wrapperClass']);
41 73
        unset($globalParameters['distributionKey']);
42 73
        unset($globalParameters['shardChoser']);
43
44 73
        $parameters['shard_connections'] = [];
45 73
        foreach ($allParameters as $shardId => $shardParameters) {
46 73
            $parameters['shard_connections'][$shardId] = $this->connectionFactory->create($connectionName.'.'.$shardId, array_merge($globalParameters, $shardParameters), $config);
47
        }
48
49 73
        $parameters['wrapperClass'] = $parameters['wrapperClass'] ?? ShardingConnection::class;
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
50
51 73
        return $this->connectionFactory->create($connectionName, $parameters, $config);
52
    }
53
54
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $connectionName should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $parameters should have a doc-comment as per coding-style.
Loading history...
55
     * {@inheritDoc}
56
     */
57 77
    public function support(string $connectionName, array $parameters): bool
0 ignored issues
show
Coding Style introduced by
The method parameter $connectionName is never used
Loading history...
58
    {
59 77
        return isset($parameters['shards']);
60
    }
61
}
62