Driver::connect()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 16
ccs 0
cts 8
cp 0
rs 10
cc 3
nc 4
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\DBAL\Logging;
6
7
use Doctrine\DBAL\Driver as DriverInterface;
8
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
9
use Ecodev\Felix\Log\Handler\DbHandler;
10
use SensitiveParameter;
11
12
final class Driver extends AbstractDriverMiddleware
13
{
14
    public function __construct(
15
        DriverInterface $driver,
16
        private readonly DbHandler $dbHandler,
17
        private readonly bool $logSql,
18
    ) {
19
        parent::__construct($driver);
20
    }
21
22
    public function connect(#[SensitiveParameter] array $params): DriverInterface\Connection
23
    {
24
        if ($this->logSql) {
25
            _log()->debug('Connecting to DB', $this->maskPassword($params));
26
        }
27
28
        // Don't bother to wrap the connection if we will never log SQL queries to file...
29
        $connection = parent::connect($params);
30
        if ($this->logSql) {
31
            $connection = new Connection($connection);
32
        }
33
34
        // ... but always notify that we are now connected. so we can log other things to DB
35
        $this->dbHandler->enable();
36
37
        return $connection;
38
    }
39
40
    /**
41
     * @param array<string,mixed> $params
42
     *
43
     * @return array<string,mixed>
44
     */
45
    private function maskPassword(#[SensitiveParameter] array $params): array
46
    {
47
        if (isset($params['password'])) {
48
            $params['password'] = '***REDACTED***';
49
        }
50
51
        return $params;
52
    }
53
}
54