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

ConnectionFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 19
dl 0
loc 52
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 26 4
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Service\Connection;
6
7
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationManager;
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\Driver\PDO\MySQL\Driver;
13
use Doctrine\DBAL\DriverManager;
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 ConfigurationManager
23
     */
24
    private ConfigurationManager $configurationManager;
25
26
    /**
27
     * @param ConfigurationManager $configurationManager
28
     */
29
    public function __construct(ConfigurationManager $configurationManager)
30
    {
31
        $this->configurationManager = $configurationManager;
32
    }
33
34
    /**
35
     * Create a new connection from the provided $config
36
     *
37
     * @param array                     $config
38
     * @param Configuration|string|null $configuration
39
     * @param EventManager|string|null  $eventManager
40
     *
41
     * @return Connection
42
     *
43
     * @throws ConnectionFactoryException
44
     */
45
    public function create(array $config, $configuration = null, $eventManager = null): Connection
46
    {
47
        $params = array_merge(
48
            [
49
                'driverClass'  => $config['driverClass'] ?? Driver::class,
50
                'wrapperClass' => $config['wrapperClass'] ?? null,
51
                'pdo'          => $config['pdo'] ?? null,
52
            ],
53
            $config['params'] ?? []
54
        );
55
56
        if (!empty($config['platform'])) {
57
            $platform = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $platform is dead and can be removed.
Loading history...
58
        }
59
60
        try {
61
            if (is_string($configuration)) {
62
                $configuration = $this->configurationManager->getConfiguration($configuration);
63
            }
64
65
            return DriverManager::getConnection($params, $configuration, $eventManager);
0 ignored issues
show
Bug introduced by
It seems like $configuration can also be of type string; however, parameter $config of Doctrine\DBAL\DriverManager::getConnection() does only seem to accept Doctrine\DBAL\Configuration|null, maybe add an additional type check? ( Ignorable by Annotation )

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

65
            return DriverManager::getConnection($params, /** @scrutinizer ignore-type */ $configuration, $eventManager);
Loading history...
Bug introduced by
It seems like $eventManager can also be of type string; however, parameter $eventManager of Doctrine\DBAL\DriverManager::getConnection() does only seem to accept Doctrine\Common\EventManager|null, maybe add an additional type check? ( Ignorable by Annotation )

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

65
            return DriverManager::getConnection($params, $configuration, /** @scrutinizer ignore-type */ $eventManager);
Loading history...
66
        } catch (\Throwable $e) {
67
            throw new ConnectionFactoryException(
68
                sprintf('Failed to create new connection: %s', $e->getMessage()),
69
                $e->getCode(),
70
                $e
71
            );
72
        }
73
    }
74
}
75