Passed
Pull Request — master (#2)
by Alex
02:52
created

ConnectionFactory::doCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
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 6
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Service\Connection;
6
7
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationManagerInterface;
8
use Arp\LaminasDoctrine\Service\Connection\Exception\ConnectionFactoryException;
9
use Doctrine\Common\EventManager;
10
use Doctrine\DBAL\Configuration;
11
use Doctrine\DBAL\Connection;
12
use Doctrine\DBAL\DriverManager;
13
use Doctrine\DBAL\Exception;
14
15
/**
16
 * @author  Alex Patterson <[email protected]>
17
 * @package Arp\LaminasDoctrine\Service\Connection
18
 */
19
final class ConnectionFactory implements ConnectionFactoryInterface
20
{
21
    /**
22
     * @var ConfigurationManagerInterface
23
     */
24
    private ConfigurationManagerInterface $configurationManager;
25
26
    /**
27
     * @var \Closure
28
     */
29
    private \Closure $factoryWrapper;
30
31
    /**
32
     * @var array<mixed>
33
     */
34
    private array $defaultConfig;
35
36
    /**
37
     * @param ConfigurationManagerInterface $configurationManager
38
     * @param callable|null                 $factory
39
     * @param array<mixed>                  $defaultConfig
40
     *
41
     * @noinspection ProperNullCoalescingOperatorUsageInspection [$this, 'doCreate'] is of type callable
42
     */
43
    public function __construct(
44
        ConfigurationManagerInterface $configurationManager,
45
        ?callable $factory = null,
46
        array $defaultConfig = []
47
    ) {
48
        $this->configurationManager = $configurationManager;
49
        $this->factoryWrapper = \Closure::fromCallable($factory ?? [$this, 'doCreate']);
50
        $this->defaultConfig = $defaultConfig;
51
    }
52
53
    /**
54
     * Create a new connection from the provided $config
55
     *
56
     * @param array<mixed>              $config
57
     * @param Configuration|string|null $configuration
58
     * @param EventManager|null         $eventManager
59
     *
60
     * @return Connection
61
     *
62
     * @throws ConnectionFactoryException
63
     */
64
    public function create(array $config, $configuration = null, ?EventManager $eventManager = null): Connection
65
    {
66
        $config = array_replace_recursive($this->defaultConfig, $config);
67
68
        try {
69
            if (is_string($configuration)) {
70
                $configuration = $this->configurationManager->getConfiguration($configuration);
71
            }
72
73
            return call_user_func($this->factoryWrapper, $config, $configuration, $eventManager);
74
        } catch (\Exception $e) {
75
            throw new ConnectionFactoryException(
76
                sprintf('Failed to create new connection: %s', $e->getMessage()),
77
                $e->getCode(),
78
                $e
79
            );
80
        }
81
    }
82
83
    /**
84
     * Default factory creation callable
85
     *
86
     * @param array<mixed>       $config
87
     * @param Configuration|null $configuration
88
     * @param EventManager|null  $eventManager
89
     *
90
     * @return Connection
91
     *
92
     * @throws Exception
93
     * @noinspection PhpUnusedPrivateMethodInspection
94
     */
95
    private function doCreate(
96
        array $config,
97
        ?Configuration $configuration,
98
        ?EventManager $eventManager = null
99
    ): Connection {
100
        return DriverManager::getConnection($config, $configuration, $eventManager);
101
    }
102
}
103