Passed
Pull Request — dev (#2303)
by Janko
05:56
created

ConnectionFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 47.37%

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 33
ccs 9
cts 19
cp 0.4737
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 2
    ) {}
21
22 183
    #[\Override]
23
    public function createConnection(): Connection
24
    {
25 183
        $configuration = null;
26 183
        if ($this->stuConfig->getDebugSettings()->getSqlLoggingSettings()->isActive()) {
27
            $configuration = new Configuration();
28
            $configuration->setMiddlewares([new Middleware($this->sqlLogger)]);
29
        }
30
31
        //use sqlite database
32 183
        if ($this->stuConfig->getDbSettings()->useSqlite()) {
33 183
            $dsnParser = new DsnParser(['sqlite' => 'pdo_sqlite']);
34 183
            $connectionParams = $dsnParser->parse($this->stuConfig->getDbSettings()->getSqliteDsn());
35
36 183
            return DriverManager::getConnection($connectionParams, $configuration);
37
        }
38
39
        return DriverManager::getConnection([
40
            'driver' => 'pdo_pgsql',
41
            'user' => $this->config->get('db.user'),
42
            'password' => $this->config->get('db.pass'),
43
            'dbname' => $this->config->get('db.database'),
44
            'host' => $this->config->get('db.host'),
45
            'charset' => 'utf8'
46
        ], $configuration);
47
    }
48
}
49