Driver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 12
c 2
b 0
f 0
dl 0
loc 40
ccs 0
cts 14
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A connect() 0 16 3
A maskPassword() 0 7 2
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