Passed
Push — master ( 6bf3a3...2583ba )
by Janko
14:04
created

ConnectionFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 40%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 34
ccs 8
cts 20
cp 0.4
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createConnection() 0 25 3
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