DBALConnectionFactory::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Persistence\DBAL\Container;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\DriverManager;
9
use InvalidArgumentException;
10
use Psr\Container\ContainerInterface;
11
12
class DBALConnectionFactory
13
{
14
    public const DEFAULT_CONNECTION_NAME = 'default';
15
    public const MISSING_CONFIG_MESSAGE = 'Missing config for dbl connection inside the container.';
16
17 3
    public function __invoke(
18
        ContainerInterface $container,
19
        string $connectionName = self::DEFAULT_CONNECTION_NAME
20
    ): Connection {
21 3
        $connectionParams = $container->get('config')['dbal']['connections'][$connectionName] ?? null;
22
23 3
        if (false === is_array($connectionParams)) {
24 1
            throw new InvalidArgumentException(self::MISSING_CONFIG_MESSAGE);
25
        }
26
27 2
        return DriverManager::getConnection($connectionParams);
0 ignored issues
show
Bug introduced by
It seems like $connectionParams can also be of type null; however, parameter $params of Doctrine\DBAL\DriverManager::getConnection() does only seem to accept array, 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

27
        return DriverManager::getConnection(/** @scrutinizer ignore-type */ $connectionParams);
Loading history...
28
    }
29
}
30