Passed
Pull Request — master (#1975)
by Janko
19:23 queued 09:07
created

ConnectionFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 1
rs 10
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