Test Failed
Push — master ( 9a0773...1fd30e )
by Sébastien
07:48
created

DoctrineConnectionProviderAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 1
b 0
f 0
dl 0
loc 39
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConnection() 0 13 3
A getDefaultConnection() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
namespace Bdf\Prime\Console\ConnectionProvider;
4
5
use Bdf\Prime\ConnectionManager;
6
use Bdf\Prime\Exception\DBALException;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Connection as DoctrineConnection;
9
use Doctrine\DBAL\Tools\Console\ConnectionNotFound;
10
use Doctrine\DBAL\Tools\Console\ConnectionProvider;
11
12
/**
13
 * The prime adapter for doctrine connection provider used by doctrine in console command
14
 */
15
class DoctrineConnectionProviderAdapter implements ConnectionProvider
16
{
17
    /**
18
     * @var ConnectionManager
19
     */
20
    private $connectionManager;
21
22
    /**
23
     * @param ConnectionManager $connectionManager
24
     */
25
    public function __construct(ConnectionManager $connectionManager)
26
    {
27
        $this->connectionManager = $connectionManager;
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function getDefaultConnection(): Connection
34
    {
35
        return $this->getConnection($this->connectionManager->getDefaultConnection());
36
    }
37
38
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
39
     * {@inheritDoc}
40
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
41
    public function getConnection(string $name): Connection
42
    {
43
        try {
44
            $connection = $this->connectionManager->getConnection($name);
45
        } catch (DBALException $exception) {
46
            throw new ConnectionNotFound("The connection '$name' is not found.", 0, $exception);
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
47
        }
48
49
        if (!$connection instanceof DoctrineConnection) {
50
            throw new ConnectionNotFound("The connection '$name' is not a doctrine connection.");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
51
        }
52
53
        return $connection;
54
    }
55
}