Passed
Pull Request — master (#2)
by Alex
02:31
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\Exception\ConnectionFactoryException;
0 ignored issues
show
Bug introduced by
The type Arp\LaminasDoctrine\Serv...nectionFactoryException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Doctrine\Common\EventManager;
9
use Doctrine\DBAL\Configuration;
10
use Doctrine\DBAL\Connection;
11
use Doctrine\DBAL\Driver\PDO\MySQL\Driver;
12
use Doctrine\DBAL\DriverManager;
13
14
/**
15
 * @author  Alex Patterson <[email protected]>
16
 * @package Arp\LaminasDoctrine\Service\Connection
17
 */
18
final class ConnectionFactory implements ConnectionFactoryInterface
19
{
20
    /**
21
     * @var ConfigurationManager
22
     */
23
    private ConfigurationManager $configurationManager;
0 ignored issues
show
Bug introduced by
The type Arp\LaminasDoctrine\Serv...on\ConfigurationManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
25
    /**
26
     * @param ConfigurationManager $configurationManager
27
     */
28
    public function __construct(ConfigurationManager $configurationManager)
29
    {
30
        $this->configurationManager = $configurationManager;
31
    }
32
33
    /**
34
     * Create a new connection from the provided $config
35
     *
36
     * @param array                     $config
37
     * @param Configuration|string|null $configuration
38
     * @param EventManager|string|null  $eventManager
39
     *
40
     * @return Connection
41
     *
42
     * @throws ConnectionFactoryException
43
     */
44
    public function create(array $config, $configuration = null, $eventManager = null): Connection
45
    {
46
        $params = array_merge(
47
            [
48
                'driverClass'  => $config['driverClass'] ?? Driver::class,
49
                'wrapperClass' => $config['wrapperClass'] ?? null,
50
                'pdo'          => $config['pdo'] ?? null,
51
            ],
52
            $config['params'] ?? []
53
        );
54
55
        if (!empty($config['platform'])) {
56
            $platform = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $platform is dead and can be removed.
Loading history...
57
        }
58
59
        try {
60
            if (is_string($configuration)) {
61
                $configuration = $this->configurationManager->getConfiguration($configuration);
62
            }
63
64
            return DriverManager::getConnection($params, $configuration, $eventManager);
0 ignored issues
show
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

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