Passed
Push — dev ( eeaa0f...91a85a )
by Janko
26:10
created

ConnectionFactory::createConnection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.667

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 0
dl 0
loc 25
ccs 6
cts 18
cp 0.3333
crap 5.667
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Orm\Transaction;
4
5
use Doctrine\DBAL\Configuration;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\DriverManager;
8
use Doctrine\DBAL\Logging\Middleware;
9
use Doctrine\DBAL\Tools\DsnParser;
10
use Noodlehaus\ConfigInterface;
11
use Stu\Component\Logging\Sql\SqlLogger;
12
use Stu\Module\Config\StuConfigInterface;
13
14
class ConnectionFactory implements ConnectionFactoryInterface
15
{
16 4
    public function __construct(
17
        private ConfigInterface $config,
18
        private StuConfigInterface $stuConfig,
19
        private SqlLogger $sqlLogger
20
    ) {
21 4
    }
22
23 4
    public function createConnection(): Connection
24
    {
25
        //use sqlite database
26 4
        if ($this->stuConfig->getDbSettings()->useSqlite()) {
27 4
            $dsnParser = new DsnParser(['sqlite' => 'pdo_sqlite']);
28 4
            $connectionParams = $dsnParser
29 4
                ->parse('sqlite:///:memory:');
30
31 4
            return DriverManager::getConnection($connectionParams);
32
        }
33
34
        $configuration = null;
35
        if ($this->stuConfig->getDebugSettings()->getSqlLoggingSettings()->isActive()) {
36
            $configuration = new Configuration();
37
            $configuration->setMiddlewares([new Middleware($this->sqlLogger)]);
38
        }
39
40
        return DriverManager::getConnection([
41
            'driver' => 'pdo_pgsql',
42
            'user' => $this->config->get('db.user'),
43
            'password' => $this->config->get('db.pass'),
44
            'dbname' => $this->config->get('db.database'),
45
            'host'  => $this->config->get('db.host'),
46
            'charset' => 'utf8',
47
        ], $configuration);
48
    }
49
}
50