Connection::rollBack()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
nc 1
nop 0
dl 0
loc 5
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of orm
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Orm\Infrastructure\Logging;
13
14
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
15
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
16
use Doctrine\DBAL\Driver\Result;
17
use Doctrine\DBAL\Driver\Statement as DriverStatement;
18
use Psr\Log\LoggerInterface;
19
20
/**
21
 * Connection
22
 *
23
 * @package Slick\Orm\Infrastructure\Logging
24
 */
25
final class Connection extends AbstractConnectionMiddleware
26
{
27
    private float $startTransaction = 0.0;
28
29
    public function __construct(ConnectionInterface $connection, private readonly LoggerInterface $logger)
30
    {
31
        parent::__construct($connection);
32
    }
33
34
    public function __destruct()
35
    {
36
        $this->logger->info('Disconnecting');
37
    }
38
39
    public function prepare(string $sql): DriverStatement
40
    {
41
        return new Statement(
42
            parent::prepare($sql),
43
            $this->logger,
44
            $sql,
45
        );
46
    }
47
48
    public function query(string $sql): Result
49
    {
50
        $start = microtime(true);
51
        $result = parent::query($sql);
52
        $duration = microtime(true) - $start;
53
        $this->logger->debug('Executing query: {sql}', [
54
            'sql' => $sql,
55
            'duration' => $duration
56
        ]);
57
58
        return $result;
59
    }
60
61
    public function exec(string $sql): int|string
62
    {
63
        $start = microtime(true);
64
        $exec = parent::exec($sql);
65
        $duration = microtime(true) - $start;
66
        $this->logger->debug('Executing statement: {sql}', ['sql' => $sql, 'duration' => $duration]);
67
68
        return $exec;
69
    }
70
71
    public function beginTransaction(): void
72
    {
73
        $this->logger->debug('Beginning transaction');
74
        $this->startTransaction = microtime(true);
75
        parent::beginTransaction();
76
    }
77
78
    public function commit(): void
79
    {
80
        parent::commit();
81
        $duration = microtime(true) - $this->startTransaction;
82
        $this->logger->debug('Committing transaction', ['duration' => $duration]);
83
    }
84
85
    public function rollBack(): void
86
    {
87
        $this->logger->debug('Rolling back transaction');
88
89
        parent::rollBack();
90
    }
91
}
92