Passed
Pull Request — master (#2)
by Alex
08:14
created

ConnectionManager::setConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Service\Connection;
6
7
use Arp\LaminasDoctrine\Config\DoctrineConfig;
8
use Arp\LaminasDoctrine\Service\Connection\Exception\ConnectionFactoryException;
9
use Arp\LaminasDoctrine\Service\Connection\Exception\ConnectionManagerException;
10
use Doctrine\DBAL\Connection;
11
12
/**
13
 * @deprecated
14
 * @author  Alex Patterson <[email protected]>
15
 * @package Arp\LaminasDoctrine\Service\Connection
16
 */
17
final class ConnectionManager implements ConnectionManagerInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Arp\LaminasDoctrine\Serv...nectionManagerInterface has been deprecated. ( Ignorable by Annotation )

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

17
final class ConnectionManager implements /** @scrutinizer ignore-deprecated */ ConnectionManagerInterface

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
18
{
19
    /**
20
     * @var DoctrineConfig
21
     */
22
    private DoctrineConfig $config;
23
24
    /**
25
     * @var Connection[]
26
     */
27
    private array $connections = [];
28
29
    /**
30
     * @var ConnectionFactoryInterface
31
     */
32
    private ConnectionFactoryInterface $factory;
33
34
    /**
35
     * @param DoctrineConfig             $config
36
     * @param ConnectionFactoryInterface $factory
37
     * @param Connection[]               $connections
38
     */
39
    public function __construct(
40
        DoctrineConfig $config,
41
        ConnectionFactoryInterface $factory,
42
        array $connections
43
    ) {
44
        $this->config = $config;
45
        $this->factory = $factory;
46
47
        $this->setConnections($connections);
48
    }
49
50
    /**
51
     * @param string $name
52
     *
53
     * @return bool
54
     */
55
    public function hasConnection(string $name): bool
56
    {
57
        return isset($this->connections[$name]) || $this->config->hasConnectionConfig($name);
58
    }
59
60
    /**
61
     * @param string $name
62
     *
63
     * @return Connection
64
     *
65
     * @throws ConnectionManagerException
66
     */
67
    public function getConnection(string $name): Connection
68
    {
69
        if (!isset($this->connections[$name]) && $this->config->hasConnectionConfig($name)) {
70
            $this->connections[$name] = $this->create($name, $this->config->getConnectionConfig($name));
71
        }
72
73
        if (isset($this->connections[$name])) {
74
            return $this->connections[$name];
75
        }
76
77
        throw new ConnectionManagerException(
0 ignored issues
show
Deprecated Code introduced by
The class Arp\LaminasDoctrine\Serv...nectionManagerException has been deprecated. ( Ignorable by Annotation )

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

77
        throw /** @scrutinizer ignore-deprecated */ new ConnectionManagerException(
Loading history...
78
            sprintf('Failed to establish connection \'%s\': Failed to find a the required configuration', $name)
79
        );
80
    }
81
82
    /**
83
     * @param string       $name
84
     * @param array<mixed> $config
85
     *
86
     * @return Connection
87
     *
88
     * @throws ConnectionManagerException
89
     */
90
    private function create(string $name, array $config): Connection
91
    {
92
        try {
93
            return $this->factory->create($config);
94
        } catch (ConnectionFactoryException $e) {
95
            throw new ConnectionManagerException(
0 ignored issues
show
Deprecated Code introduced by
The class Arp\LaminasDoctrine\Serv...nectionManagerException has been deprecated. ( Ignorable by Annotation )

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

95
            throw /** @scrutinizer ignore-deprecated */ new ConnectionManagerException(
Loading history...
96
                sprintf('Failed to establish connection \'%s\': %s', $name, $e->getMessage()),
97
                $e->getCode(),
98
                $e
99
            );
100
        }
101
    }
102
103
    /**
104
     * @param array<Connection|array> $connections
105
     */
106
    public function setConnections(array $connections): void
107
    {
108
        $this->connections = [];
109
110
        foreach ($connections as $name => $connection) {
111
            if (is_array($connection)) {
112
                $this->addConnectionConfig($name, $connection);
113
            } else {
114
                $this->setConnection($name, $connection);
115
            }
116
        }
117
    }
118
119
    /**
120
     * @param string     $name
121
     * @param Connection $connection
122
     */
123
    public function setConnection(string $name, Connection $connection): void
124
    {
125
        $this->connections[$name] = $connection;
126
    }
127
128
    /**
129
     * @param string       $name
130
     * @param array<mixed> $config
131
     */
132
    public function addConnectionConfig(string $name, array $config): void
133
    {
134
        $this->config->setConnectionConfig($name, $config);
135
    }
136
}
137