Passed
Push — master ( 9c8ace...d62147 )
by Alexander
13:22
created

StdoutQueryLogger::isPostgresSystemQuery()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 10
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Logger;
6
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\LoggerTrait;
9
use Psr\Log\LogLevel;
10
11
/**
12
 * Temporary LoggerInterface class
13
 * Slightly adapted for SQL queries
14
 * @package Yiisoft\Yii\Cycle\Logger
15
 * @deprecated In the future StdoutLogger will be removed (when we will have debug-tools)
16
 */
17
class StdoutQueryLogger implements LoggerInterface
18
{
19
    use LoggerTrait;
20
21
    private bool $display;
22
23
    private int $countWrites;
24
    private int $countReads;
25
    private array $buffer = [];
26
    private $fp;
27
28
    public function __construct()
29
    {
30
        $this->display = true;
31
        $this->countWrites = 0;
32
        $this->countReads = 0;
33
        $this->fp = fopen('php://stdout', 'w');
34
    }
35
36
    public function countWriteQueries(): int
37
    {
38
        return $this->countWrites;
39
    }
40
41
    public function countReadQueries(): int
42
    {
43
        return $this->countReads;
44
    }
45
46
    public function log($level, $message, array $context = []): void
47
    {
48
        if (!empty($context['elapsed'])) {
49
            $sql = strtolower($message);
50
            if (
51
                strpos($sql, 'insert') === 0 ||
52
                strpos($sql, 'update') === 0 ||
53
                strpos($sql, 'delete') === 0
54
            ) {
55
                $this->countWrites++;
56
            } elseif (!$this->isPostgresSystemQuery($sql)) {
57
                ++$this->countReads;
58
            }
59
        }
60
61
        if ($level === LogLevel::ERROR) {
62
            $this->print(" ! \033[31m" . $message . "\033[0m");
63
        } elseif ($level === LogLevel::ALERT) {
64
            $this->print(" ! \033[35m" . $message . "\033[0m");
65
        } elseif (strpos($message, 'SHOW') === 0) {
66
            $this->print(" > \033[34m" . $message . "\033[0m");
67
        } else {
68
            if ($this->isPostgresSystemQuery($message)) {
69
                $this->print(" > \033[90m" . $message . "\033[0m");
70
71
                return;
72
            }
73
74
            if (strpos($message, 'SELECT') === 0) {
75
                $this->print(" > \033[32m" . $message . "\033[0m");
76
            } elseif (strpos($message, 'INSERT') === 0) {
77
                $this->print(" > \033[36m" . $message . "\033[0m");
78
            } else {
79
                $this->print(" > \033[33m" . $message . "\033[0m");
80
            }
81
        }
82
    }
83
84
    private function print(string $str): void
85
    {
86
        $this->buffer[] = $str;
87
        if (!$this->display) {
88
            return;
89
        }
90
        fwrite($this->fp, "{$str}\n");
0 ignored issues
show
Bug introduced by
It seems like $this->fp can also be of type boolean; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        fwrite(/** @scrutinizer ignore-type */ $this->fp, "{$str}\n");
Loading history...
91
    }
92
93
    public function display(): void
94
    {
95
        $this->display = true;
96
    }
97
98
    public function hide(): void
99
    {
100
        $this->display = false;
101
    }
102
103
    public function getBuffer(): array
104
    {
105
        return $this->buffer;
106
    }
107
108
    public function cleanBuffer(): void
109
    {
110
        $this->buffer = [];
111
    }
112
113
    protected function isPostgresSystemQuery(string $query): bool
114
    {
115
        $query = strtolower($query);
116
        if (
117
            strpos($query, 'tc.constraint_name') ||
118
            strpos($query, 'pg_indexes') ||
119
            strpos($query, 'tc.constraint_name') ||
120
            strpos($query, 'pg_constraint') ||
121
            strpos($query, 'information_schema') ||
122
            strpos($query, 'pg_class')
123
        ) {
124
            return true;
125
        }
126
127
        return false;
128
    }
129
}
130