DataGatewayServiceProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 25
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createPdo() 0 15 4
A register() 0 5 1
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