Passed
Push — master ( a25875...d379e9 )
by Dominik
03:08
created

DoctrineDbalConnectionRegistry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\DoctrineDbServiceProvider\Registry;
6
7
use Doctrine\Common\Persistence\ConnectionRegistry;
8
use Doctrine\DBAL\Connection;
9
use Pimple\Container;
10
11
final class DoctrineDbalConnectionRegistry implements ConnectionRegistry
12
{
13
    /**
14
     * @var Container
15
     */
16
    private $container;
17
18
    /**
19
     * @var Container
20
     */
21
    private $connections;
22
23
    /**
24
     * @var string
25
     */
26
    private $defaultConnectionName;
27
28 5
    public function __construct(Container $container)
29
    {
30 5
        $this->container = $container;
31 5
    }
32
33 3
    public function getDefaultConnectionName(): string
34
    {
35 3
        $this->loadConnections();
36
37 3
        return $this->defaultConnectionName;
38
    }
39
40
    /**
41
     * @param string|null $name
42
     *
43
     * @throws \InvalidArgumentException
44
     *
45
     * @return Connection
46
     */
47 2 View Code Duplication
    public function getConnection($name = null): Connection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49 2
        $this->loadConnections();
50
51 2
        $name = $name ?? $this->getDefaultConnectionName();
52
53 2
        if (!isset($this->connections[$name])) {
54 1
            throw new \InvalidArgumentException(sprintf('Missing connection with name "%s".', $name));
55
        }
56
57 1
        return $this->connections[$name];
58
    }
59
60
    /**
61
     * @return array<string, Connection>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
62
     */
63 1 View Code Duplication
    public function getConnections(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65 1
        $this->loadConnections();
66
67 1
        $connections = [];
68
        /** @var string $name */
69 1
        foreach ($this->connections->keys() as $name) {
70
            /** @var Connection $connection */
71 1
            $connection = $this->connections[$name];
72 1
            $connections[$name] = $connection;
73
        }
74
75 1
        return $connections;
76
    }
77
78
    /**
79
     * @return array<string>
80
     */
81 1
    public function getConnectionNames(): array
82
    {
83 1
        $this->loadConnections();
84
85 1
        return $this->connections->keys();
86
    }
87
88 5 View Code Duplication
    private function loadConnections(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90 5
        if (null === $this->connections) {
91 5
            $this->connections = $this->container['doctrine.dbal.dbs'];
92 5
            $this->defaultConnectionName = $this->container['doctrine.dbal.dbs.default'];
93
        }
94 5
    }
95
}
96