Passed
Push — master ( 33d5ac...775805 )
by Dmitriy
03:03
created

DatabaseCollector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 78
c 1
b 0
f 0
dl 0
loc 150
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A collectQueryEnd() 0 9 1
A collectTransactionRollback() 0 10 1
A collectTransactionCommit() 0 10 1
A collectTransactionStart() 0 14 1
A collectQueryStart() 0 18 1
A getCollected() 0 7 1
A getIndexData() 0 18 1
A collectQueryError() 0 9 1
A reset() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector\Database;
6
7
use Throwable;
8
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
9
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
10
use Yiisoft\Yii\Debug\Collector\IndexCollectorInterface;
11
12
class DatabaseCollector implements CollectorInterface, IndexCollectorInterface
13
{
14
    use CollectorTrait;
15
16
    private const ACTION_QUERY_START = 'query.start';
17
    private const ACTION_QUERY_END = 'query.end';
18
    private const ACTION_QUERY_ERROR = 'query.error';
19
20
    private const ACTION_TRANSACTION_START = 'transaction.start';
21
    private const ACTION_TRANSACTION_ROLLBACK = 'transaction.rollback';
22
    private const ACTION_TRANSACTION_COMMIT = 'transaction.commit';
23
24
    private const TRANSACTION_STATUS_COMMIT = 'commit';
25
    private const TRANSACTION_STATUS_ROLLBACK = 'rollback';
26
    private const TRANSACTION_STATUS_START = 'start';
27
28
    private const QUERY_STATUS_INITIALIZED = 'initialized';
29
    private const QUERY_STATUS_ERROR = 'error';
30
    private const QUERY_STATUS_SUCCESS = 'success';
31
32
    private array $queries = [];
33
    private array $transactions = [];
34
35
    private int $position = 0;
36
    private int $currentTransactionId = 0;
37
38
    public function collectQueryStart(
39
        string $id,
40
        string $sql,
41
        string $rawSql,
42
        array $params,
43
        string $line,
44
    ): void {
45
        $this->queries[$id] = [
46
            'position' => $this->position++,
47
            'transactionId' => $this->currentTransactionId,
48
            'sql' => $sql,
49
            'rawSql' => $rawSql,
50
            'params' => $params,
51
            'line' => $line,
52
            'status' => self::QUERY_STATUS_INITIALIZED,
53
            'actions' => [
54
                'action' => self::ACTION_QUERY_START,
55
                'time' => microtime(true),
56
            ],
57
        ];
58
    }
59
60
    public function collectQueryEnd(
61
        string $id,
62
        int $rowsNumber,
63
    ): void {
64
        $this->queries[$id]['rowsNumber'] = $rowsNumber;
65
        $this->queries[$id]['status'] = self::QUERY_STATUS_SUCCESS;
66
        $this->queries[$id]['actions'][] = [
67
            'action' => self::ACTION_QUERY_END,
68
            'time' => microtime(true),
69
        ];
70
    }
71
72
    public function collectQueryError(
73
        string $id,
74
        Throwable $exception,
75
    ): void {
76
        $this->queries[$id]['exception'] = $exception;
77
        $this->queries[$id]['status'] = self::QUERY_STATUS_ERROR;
78
        $this->queries[$id]['actions'][] = [
79
            'action' => self::ACTION_QUERY_ERROR,
80
            'time' => microtime(true),
81
        ];
82
    }
83
84
    public function collectTransactionStart(
85
        ?string $isolationLevel,
86
        string $line,
87
    ): void {
88
        $id = ++$this->currentTransactionId;
89
        $this->transactions[$id] = [
90
            'id' => $id,
91
            'position' => $this->position++,
92
            'status' => self::TRANSACTION_STATUS_START,
93
            'line' => $line,
94
            'level' => $isolationLevel,
95
            'actions' => [
96
                'action' => self::ACTION_TRANSACTION_START,
97
                'time' => microtime(true),
98
            ],
99
        ];
100
    }
101
102
    public function collectTransactionRollback(
103
        string $line,
104
    ) {
105
        $this->transactions[$this->currentTransactionId]['status'] = self::TRANSACTION_STATUS_ROLLBACK;
106
        $this->transactions[$this->currentTransactionId]['actions'] = [
107
            'action' => self::ACTION_TRANSACTION_ROLLBACK,
108
            'line' => $line,
109
            'time' => microtime(true),
110
        ];
111
        ++$this->currentTransactionId;
112
    }
113
114
    public function collectTransactionCommit(
115
        string $line,
116
    ) {
117
        $this->transactions[$this->currentTransactionId]['status'] = self::TRANSACTION_STATUS_COMMIT;
118
        $this->transactions[$this->currentTransactionId]['actions'] = [
119
            'action' => self::ACTION_TRANSACTION_COMMIT,
120
            'line' => $line,
121
            'time' => microtime(true),
122
        ];
123
        ++$this->currentTransactionId;
124
    }
125
126
    public function getCollected(): array
127
    {
128
        usort($this->queries, fn (array $a, array $b) => $a['position'] <=> $b['position']);
129
130
        return [
131
            'queries' => array_values($this->queries),
132
            'transactions' => array_values($this->transactions),
133
        ];
134
    }
135
136
    public function getIndexData(): array
137
    {
138
        return [
139
            'db' => [
140
                'queries' => [
141
                    'error' => count(
142
                        array_filter($this->queries, fn (array $query) => $query['status'] === self::QUERY_STATUS_ERROR)
143
                    ),
144
                    'total' => count($this->queries),
145
                ],
146
                'transactions' => [
147
                    'error' => count(
148
                        array_filter(
149
                            $this->transactions,
150
                            fn (array $query) => $query['status'] === self::TRANSACTION_STATUS_ROLLBACK
151
                        )
152
                    ),
153
                    'total' => count($this->transactions),
154
                ],
155
            ],
156
        ];
157
    }
158
159
    private function reset(): void
0 ignored issues
show
Unused Code introduced by
The method reset() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
160
    {
161
        $this->queries = [];
162
    }
163
}
164