ConnectionManager::addConnectionConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
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\ConnectionConfigs;
8
use Arp\LaminasDoctrine\Service\Connection\Exception\ConnectionFactoryException;
9
use Arp\LaminasDoctrine\Service\Connection\Exception\ConnectionManagerException;
10
use Doctrine\DBAL\Connection;
11
12
final class ConnectionManager implements ConnectionManagerInterface
13
{
14
    /**
15
     * @var Connection[]
16
     */
17
    private array $connections = [];
18
19
    /**
20
     * @param ConnectionConfigs          $configs
21
     * @param ConnectionFactoryInterface $factory
22
     * @param Connection[]               $connections
23
     */
24
    public function __construct(
25
        private readonly ConnectionConfigs $configs,
26
        private readonly ConnectionFactoryInterface $factory,
27
        array $connections
28
    ) {
29
        $this->setConnections($connections);
30
    }
31
32
    public function hasConnection(string $name): bool
33
    {
34
        return isset($this->connections[$name]) || $this->configs->hasConnectionConfig($name);
35
    }
36
37
    /**
38
     * @throws ConnectionManagerException
39
     */
40
    public function getConnection(string $name): Connection
41
    {
42
        if (!isset($this->connections[$name]) && $this->configs->hasConnectionConfig($name)) {
43
            $this->connections[$name] = $this->create($name, $this->configs->getConnectionConfig($name));
44
        }
45
46
        if (isset($this->connections[$name])) {
47
            return $this->connections[$name];
48
        }
49
50
        throw new ConnectionManagerException(
51
            sprintf('Failed to establish connection \'%s\': Failed to find a the required configuration', $name)
52
        );
53
    }
54
55
    /**
56
     * @param array<mixed> $config
57
     *
58
     * @throws ConnectionManagerException
59
     */
60
    private function create(string $name, array $config): Connection
61
    {
62
        try {
63
            return $this->factory->create($config);
64
        } catch (ConnectionFactoryException $e) {
65
            throw new ConnectionManagerException(
66
                sprintf('Failed to establish connection \'%s\': %s', $name, $e->getMessage()),
67
                $e->getCode(),
68
                $e
69
            );
70
        }
71
    }
72
73
    /**
74
     * @param array<Connection|array<mixed>> $connections
75
     */
76
    public function setConnections(array $connections): void
77
    {
78
        $this->connections = [];
79
80
        foreach ($connections as $name => $connection) {
81
            if (is_array($connection)) {
82
                $this->addConnectionConfig($name, $connection);
83
            } else {
84
                $this->setConnection($name, $connection);
85
            }
86
        }
87
    }
88
89
    public function setConnection(string $name, Connection $connection): void
90
    {
91
        $this->connections[$name] = $connection;
92
    }
93
94
    /**
95
     * @param array<mixed> $config
96
     */
97
    public function addConnectionConfig(string $name, array $config): void
98
    {
99
        $this->configs->setConnectionConfig($name, $config);
100
    }
101
}
102