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

ConnectionFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 15 3
A doCreate() 0 6 1
A __construct() 0 8 1
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
 * @deprecated
17
 * @author  Alex Patterson <[email protected]>
18
 * @package Arp\LaminasDoctrine\Service\Connection
19
 */
20
final class ConnectionFactory implements ConnectionFactoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Arp\LaminasDoctrine\Serv...nectionFactoryInterface 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

20
final class ConnectionFactory implements /** @scrutinizer ignore-deprecated */ ConnectionFactoryInterface

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...
21
{
22
    /**
23
     * @var ConfigurationManagerInterface
24
     */
25
    private ConfigurationManagerInterface $configurationManager;
26
27
    /**
28
     * @var \Closure
29
     */
30
    private \Closure $factoryWrapper;
31
32
    /**
33
     * @var array<mixed>
34
     */
35
    private array $defaultConfig;
36
37
    /**
38
     * @param ConfigurationManagerInterface $configurationManager
39
     * @param callable|null                 $factory
40
     * @param array<mixed>                  $defaultConfig
41
     *
42
     * @noinspection ProperNullCoalescingOperatorUsageInspection [$this, 'doCreate'] is of type callable
43
     */
44
    public function __construct(
45
        ConfigurationManagerInterface $configurationManager,
46
        ?callable $factory = null,
47
        array $defaultConfig = []
48
    ) {
49
        $this->configurationManager = $configurationManager;
50
        $this->factoryWrapper = \Closure::fromCallable($factory ?? [$this, 'doCreate']);
51
        $this->defaultConfig = $defaultConfig;
52
    }
53
54
    /**
55
     * Create a new connection from the provided $config
56
     *
57
     * @param array<mixed>              $config
58
     * @param Configuration|string|null $configuration
59
     * @param EventManager|null         $eventManager
60
     *
61
     * @return Connection
62
     *
63
     * @throws ConnectionFactoryException
64
     */
65
    public function create(array $config, $configuration = null, ?EventManager $eventManager = null): Connection
66
    {
67
        $config = array_replace_recursive($this->defaultConfig, $config);
68
69
        try {
70
            if (is_string($configuration)) {
71
                $configuration = $this->configurationManager->getConfiguration($configuration);
72
            }
73
74
            return call_user_func($this->factoryWrapper, $config, $configuration, $eventManager);
75
        } catch (\Exception $e) {
76
            throw new ConnectionFactoryException(
0 ignored issues
show
Deprecated Code introduced by
The class Arp\LaminasDoctrine\Serv...nectionFactoryException 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

76
            throw /** @scrutinizer ignore-deprecated */ new ConnectionFactoryException(
Loading history...
77
                sprintf('Failed to create new connection: %s', $e->getMessage()),
78
                $e->getCode(),
79
                $e
80
            );
81
        }
82
    }
83
84
    /**
85
     * Default factory creation callable
86
     *
87
     * @param array<mixed>       $config
88
     * @param Configuration|null $configuration
89
     * @param EventManager|null  $eventManager
90
     *
91
     * @return Connection
92
     *
93
     * @throws Exception
94
     * @noinspection PhpUnusedPrivateMethodInspection
95
     */
96
    private function doCreate(
97
        array $config,
98
        ?Configuration $configuration,
99
        ?EventManager $eventManager = null
100
    ): Connection {
101
        return DriverManager::getConnection($config, $configuration, $eventManager);
102
    }
103
}
104