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

ConnectionFactory::createConnection()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.8315

Importance

Changes 0
Metric Value
cc 3
eloc 17
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 25
ccs 7
cts 17
cp 0.4118
crap 4.8315
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 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