Passed
Push — master ( f7ef7a...65c26d )
by Dmitriy
02:46
created

DatabaseCollector::getSummary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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