Test Failed
Pull Request — master (#31)
by Sébastien
07:15
created

ConnectionManager::config()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A ConnectionManager::getConnectionNames() 0 3 1
1
<?php
2
3
namespace Bdf\Prime;
4
5
use Bdf\Prime\Connection\ConnectionInterface;
6
use Bdf\Prime\Connection\ConnectionRegistry;
7
use Bdf\Prime\Connection\SubConnectionManagerInterface;
8
use Bdf\Prime\Exception\DBALException;
9
use LogicException;
10
11
/**
12
 * ConnectionManager
13
 * 
14
 * doctrine dbal connection registry
0 ignored issues
show
Coding Style introduced by
Doc comment long description must start with a capital letter
Loading history...
introduced by
Doc comment long description must end with a full stop
Loading history...
15
 */
16
class ConnectionManager implements ConnectionRegistryInterface
17
{
18
    /**
19
     * The connection registry
20
     *
21
     * @var ConnectionRegistryInterface
22
     */
23
    private $registry;
24
25
    /**
26
     * Connections list
27
     *
28
     * @var ConnectionInterface[]
29
     */
30
    private $connections = [];
31
32
    /**
33
     * Default connection to use
34
     * 
35
     * @var string 
0 ignored issues
show
introduced by
Expected "string" but found "string " for @var tag in member variable comment
Loading history...
36
     */
37
    private $defaultConnection;
38
39
40
    /**
41
     * Set default configuration
42
     *
43
     * @param ConnectionRegistryInterface $registry
44
     */
45 200
    public function __construct(ConnectionRegistryInterface $registry = null)
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
46
    {
47 200
        $this->registry = $registry ?: new ConnectionRegistry();
0 ignored issues
show
Coding Style introduced by
The value of a comparison must not be assigned to a variable
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
48 200
    }
49
50
    /**
51
     * Add database connection
52
     *
53
     * @param ConnectionInterface $connection Unique name for the connection
54
     * @param boolean             $default    Use this connection as the default? The first connection added is automatically set as the default, even if this flag is false.
0 ignored issues
show
introduced by
Expected "bool" but found "boolean" for parameter type
Loading history...
55
     *
56
     * @throws LogicException if connection exists
0 ignored issues
show
introduced by
@throws comment must be on the next line
Loading history...
introduced by
@throws tag comment must start with a capital letter
Loading history...
introduced by
@throws tag comment must end with a full stop
Loading history...
57
     */
58 167
    public function addConnection(ConnectionInterface $connection, bool $default = false)
59
    {
60
        // Connection name must be unique
61 167
        if (isset($this->connections[$connection->getName()])) {
62 14
            throw new LogicException('Connection for "'.$connection->getName().'" already exists. Connection name must be unique.');
63
        }
64 14
65
        // Set as default connection?
66 14
        if (true === $default || null === $this->defaultConnection) {
67
            $this->defaultConnection = $connection->getName();
68
        }
69
70 167
        $this->connections[$connection->getName()] = $connection;
71 1
    }
72
73
    /**
74
     * Remove a connection by its name
75 167
     *
76 151
     * @param string $name
77
     */
78
    public function removeConnection(string $name)
79 167
    {
80 167
        if (!isset($this->connections[$name])) {
81
            return;
82
        }
83
        
84
        $this->connections[$name]->close();
0 ignored issues
show
Bug introduced by
The method close() does not exist on Bdf\Prime\Connection\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Connection\ConnectionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        $this->connections[$name]->/** @scrutinizer ignore-call */ 
85
                                   close();
Loading history...
85
        unset($this->connections[$name]);
86
    }
87 40
88
    /**
89 40
     * Get connection by name
90 26
     *
91
     * @param string $name Unique name of the connection to be returned
92
     * 
93 14
     * @return ConnectionInterface
94 14
     * 
95 14
     * @throws DBALException
96
     *
97
     * @deprecated Since 1.1 use getConnection
0 ignored issues
show
introduced by
Each @deprecated tag must have a @see tag immediately following it.
Loading history...
introduced by
The text '@deprecated Since 1.1 use getConnection' does not match the standard format: @deprecated in deprecation-version and is removed from removal-version. extra-info.
Loading history...
98
     */
99
    public function connection(string $name = null): ConnectionInterface
100
    {
101
        return $this->getConnection($name);
102
    }
103
104
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
105
     * {@inheritDoc}
106
     */
107
    public function getConnection(string $name = null): ConnectionInterface
108
    {
109
        if ($name === null) {
110
            $name = $this->defaultConnection;
111
        }
112
113
        // Connection name must be unique
114
        if (!isset($this->connections[$name]) && !$this->loadSubConnection($name)) {
115
            $this->addConnection($this->registry->getConnection($name));
116 1118
        }
117
118 1118
        return $this->connections[$name];
119 6
    }
120
121
    /**
122
     * Associate configuration to connection
123 1118
     *
124 162
     * @param string $connectionName
125
     * @param string|array $parameters
126
     */
127 1117
    public function declareConnection(string $connectionName, $parameters)
128
    {
129
        if ($this->registry instanceof ConnectionRegistry) {
130
            $this->registry->declareConnection($connectionName, $parameters);
131
        }
132
    }
133
134
    /**
135
     * Get all connections
136 41
     *
137
     * @return ConnectionInterface[] Array of connection objects
138 41
     */
139 41
    public function connections()
140
    {
141 41
        return $this->connections;
142
    }
143
144
    /**
145
     * {@inheritDoc}
146
     */
147
    public function getConnectionNames(): array
148
    {
149
        return array_unique(array_merge($this->getCurrentConnectionNames(), $this->registry->getConnectionNames()));
150 167
    }
151
152 167
    /**
153
     * Gets the name of connections in progress
154 167
     */
155 167
    public function getCurrentConnectionNames(): array
156
    {
157
        return array_keys($this->connections);
158
    }
159
160
    /**
161
     * Set the default connection name
162
     *
163
     * @param string $name
164
     */
165
    public function setDefaultConnection($name)
166 1149
    {
167
        $this->defaultConnection = $name;
168 1149
    }
169
170
    /**
171
     * Get the default connection name
172
     *
173
     * @return string
174 3
     */
175
    public function getDefaultConnection()
176 3
    {
177
        return $this->defaultConnection;
178
    }
179
180
    /**
181
     * Try to load a sub connection
182
     *
183
     * This method allows connection as "name.otherName".
184
     * Works only if connection "name" is a SubConnectionManagerInterface.
185 1
     *
186
     * @param string $connectionName
187 1
     * 
188
     * @return bool  The connection has been loaded
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
189
     */
190
    private function loadSubConnection($connectionName)
0 ignored issues
show
Coding Style introduced by
Private method name "ConnectionManager::loadSubConnection" must be prefixed with an underscore
Loading history...
191
    {
192
        $names = explode('.', $connectionName, 2);
193 3
194
        if (!isset($names[1])) {
195 3
            return false;
196
        }
197
198
        $connection = $this->getConnection($names[0]);
199
200
        if ($connection instanceof SubConnectionManagerInterface) {
201
            //TODO doit on concerver une reference sur la sous connection ?
0 ignored issues
show
Coding Style introduced by
No space found before comment text; expected "// TODO doit on concerver une reference sur la sous connection ?" but found "//TODO doit on concerver une reference sur la sous connection ?"
Loading history...
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
202
            $this->connections[$connectionName] = $connection->getConnection($names[1]);
203 1
            return true;
204
        }
205 1
206 1
        return false;
207
    }
208
}
209