Passed
Pull Request — master (#1975)
by Janko
19:23 queued 09:07
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 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 25
ccs 6
cts 18
cp 0.3333
crap 5.667
rs 9.7
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 2
    public function __construct(
17
        private ConfigInterface $config,
18
        private StuConfigInterface $stuConfig,
19
        private SqlLogger $sqlLogger
20
    ) {
21 2
    }
22
23 2
    public function createConnection(): Connection
24
    {
25
        //use sqlite database
26 2
        if ($this->stuConfig->getDbSettings()->useSqlite()) {
27 2
            $dsnParser = new DsnParser(['sqlite' => 'pdo_sqlite']);
28 2
            $connectionParams = $dsnParser
29 2
                ->parse('sqlite:///:memory:');
30
31 2
            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