DataGatewayServiceProvider::createPdo()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 1
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 4
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\RfcLinc\Application\Providers;
6
7
use PDO;
8
use PhpCfdi\RfcLinc\Application\Config;
9
use PhpCfdi\RfcLinc\DataGateway\Pdo\PdoFactory;
10
use Pimple\Container;
11
use Pimple\ServiceProviderInterface;
12
13
class DataGatewayServiceProvider implements ServiceProviderInterface
14
{
15
    public function register(Container $container)
16
    {
17 4
        $container['gateways'] = function (Container $container) {
18 4
            $config = $container['config'];
19 4
            return new PdoFactory($this->createPdo($config));
20
        };
21 8
    }
22
23 7
    public function createPdo(Config $config): PDO
24
    {
25 7
        if ('' === $config->dbDsn()) {
26 1
            throw new \RuntimeException('No database DSN is configured');
27
        }
28
29
        try {
30 6
            return new PDO($config->dbDsn(), $config->dbUsername(), $config->dbPassword());
31 1
        } catch (\Throwable $exception) {
32 1
            throw new \RuntimeException(sprintf(
33 1
                "Unable to create PDO using\nDSN: %s,\nUsername: '%s',\nPassword: %s.",
34 1
                $config->dbDsn(),
35 1
                $config->dbUsername(),
36 1
                ('' === $config->dbPassword()) ? 'empty' : 'not empty'
37 1
            ), 0, $exception);
38
        }
39
    }
40
}
41