Test Failed
Pull Request — master (#4)
by Dmytro
04:37
created

DebugTrait   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 296
Duplicated Lines 0 %

Test Coverage

Coverage 43.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 31
eloc 79
c 1
b 0
f 0
dl 0
loc 296
ccs 35
cts 81
cp 0.4321
rs 9.92

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getLastQuery() 0 3 1
A getIgnoreErrors() 0 3 1
A flushIgnoreErrors() 0 5 1
A setConnectionTime() 0 5 1
A setIgnoreErrors() 0 9 2
A addIgnoreErrors() 0 5 1
A setDebug() 0 5 1
A isDebug() 0 3 1
A getAllExecutedQuery() 0 3 1
A renderConnectionTime() 0 7 1
A getConnectionTime() 0 3 2
A setLastQuery() 0 5 1
A renderExecutedQuery() 0 24 5
A checkLastError() 0 12 4
A flushExecutedQuery() 0 7 1
A getQueriesTime() 0 3 1
A addQueriesTime() 0 5 1
A collectQuery() 0 31 5
1
<?php namespace AgelxNash\Modx\Evo\Database\Traits;
2
3
use AgelxNash\Modx\Evo\Database\Exceptions;
4
5
trait DebugTrait
6
{
7
    /**
8
     * @var bool
9
     */
10
    protected $debug = false;
11
12
    /**
13
     * @var array
14
     */
15
    protected $queryCollection = [];
16
17
    /**
18
     * @var int
19
     */
20
    protected $queriesTime = 0;
21
22
    /**
23
     * @var string
24
     */
25
    protected $lastQuery = '';
26
27
    /**
28
     * @var int
29
     */
30
    protected $connectionTime = 0;
31
32
    /**
33
     * @var array
34
     */
35
    protected $ignoreErrors = [
36
        '42S22', // SQLSTATE: 42S22 (ER_BAD_FIELD_ERROR) Unknown column '%s' in '%s'
37
        '42S21', // SQLSTATE: 42S21 (ER_DUP_FIELDNAME) Duplicate column name '%s'
38
        '42000', // SQLSTATE: 42000 (ER_DUP_KEYNAME) Duplicate key name '%s'
39
        '23000', // SQLSTATE: 23000 (ER_DUP_ENTRY) Duplicate entry '%s' for key %d
40
        '42000' // SQLSTATE: 42000 (ER_CANT_DROP_FIELD_OR_KEY) Can't DROP '%s'; check that column/key exists
41
    ];
42
43
    /**
44
     * @var string
45
     */
46
    protected $timeFormat = '%2.5f';
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    abstract public function getDriver();
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    abstract public function getLastError();
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    abstract public function getLastErrorNo();
62
63
    /**
64
     * {@inheritDoc}
65
     */
66
    abstract public function getRecordCount($result);
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    abstract public function getAffectedRows();
72
73
    /**
74
     * {@inheritDoc}
75
     */
76 2
    public function isDebug()
77
    {
78 2
        return $this->debug;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84 2
    public function setDebug($flag)
85
    {
86 2
        $this->debug = $flag;
87
88 2
        return $this;
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function checkLastError($query = null)
95
    {
96
        if ($this->getDriver()->isConnected() === false || $this->getLastErrorNo() === '') {
97
            return false;
98
        }
99
100
        if (\in_array($this->getLastErrorNo(), $this->getIgnoreErrors(), true)) {
101
            return true;
102
        }
103
104
        throw (new Exceptions\QueryException($this->getLastError()))
105
            ->setQuery($query);
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111
    public function collectQuery($result, $sql, $time)
112
    {
113
        $debug = debug_backtrace();
114
        array_shift($debug);
115
        array_shift($debug);
116
        $path = [];
117
        foreach ($debug as $line) {
118
            $path[] = ($line['class'] ? ($line['class'] . '::') : null) . $line['function'];
119
            /*$path[] = [
120
                'method' => ($line['class'] ? ($line['class'] . '::') : null) . $line['function'],
121
                'file' => ($line['file'] ? ($line['file'] . ':') : null) . ($line['line'] ?? 0)
122
            ];*/
123
        }
124
        $path = implode(' > ', array_reverse($path));
125
126
        $data = [
127
            'sql' => $sql,
128
            'time' => $time,
129
            'rows' => (stripos($sql, 'SELECT') === 0 && $this->getDriver()->isResult($result)) ?
130
                $this->getRecordCount($result) : $this->getAffectedRows(),
131
            'path' => $path,
132
            //'event' => $modx->event->name,
133
            //'element' => [
134
            //      'name' => $modx->event->activePlugin ?? ($modx->currentSnippet ?? null),
135
            //      'type' => $modx->event->activePlugin ? 'Plugin' : ($modx->currentSnippet ? 'Snippet' : 'Source code')
136
            // ]
137
        ];
138
139
        $this->queryCollection[] = $data;
140
141
        return $data;
142
    }
143
144
    /**
145
     * {@inheritDoc}
146
     */
147
    public function setLastQuery($query)
148
    {
149
        $this->lastQuery = (string)$query;
150
151
        return $query;
152
    }
153
154
    /**
155
     * {@inheritDoc}
156
     */
157 1
    public function getLastQuery()
158
    {
159 1
        return (string)$this->lastQuery;
160
    }
161
162
    /**
163
     * {@inheritDoc}
164
     */
165 2
    public function getAllExecutedQuery()
166
    {
167 2
        return $this->queryCollection;
168
    }
169
170
    /**
171
     * {@inheritDoc}
172
     */
173
    public function flushExecutedQuery()
174
    {
175
        $this->queryCollection = [];
176
        $this->queriesTime = 0;
177
        $this->lastQuery = $this->setLastQuery('');
178
179
        return true;
180
    }
181
182
    /**
183
     * {@inheritDoc}
184
     */
185 1
    public function setConnectionTime($value)
186
    {
187 1
        $this->connectionTime = $value;
188
189 1
        return $value;
190
    }
191
192
    /**
193
     * {@inheritDoc}
194
     */
195 1
    public function getConnectionTime($format = false)
196
    {
197 1
        return $format ? sprintf($this->timeFormat, $this->connectionTime) : $this->connectionTime;
198
    }
199
200
    /**
201
     * {@inheritDoc}
202
     */
203
    public function getQueriesTime()
204
    {
205
        return $this->queriesTime;
206
    }
207
208
    /**
209
     * {@inheritDoc}
210
     */
211
    public function addQueriesTime($time)
212
    {
213
        $this->queriesTime += $time;
214
215
        return $time;
216
    }
217
218
    /**
219
     * @return string
220
     */
221 1
    public function renderConnectionTime()
222
    {
223
        return '<fieldset style="text-align:left">' .
224
            '<legend>Database connection</legend>' .
225 1
            'Database connection was created in ' . $this->getConnectionTime(true) . ' s.' .
226 1
            '</fieldset>' .
227 1
            '<br />';
228
    }
229
230
    /**
231
     * @return array
232
     */
233 1
    public function getIgnoreErrors()
234
    {
235 1
        return $this->ignoreErrors;
236
    }
237
238
    /**
239
     * @param string $error
240
     * @return string
241
     */
242 1
    public function addIgnoreErrors($error)
243
    {
244 1
        $this->ignoreErrors[] = $error;
245
246 1
        return $error;
247
    }
248
249
    /**
250
     * @return bool
251
     */
252 1
    public function flushIgnoreErrors()
253
    {
254 1
        $this->ignoreErrors = [];
255
256 1
        return true;
257
    }
258
259
    /**
260
     * @param array $errors
261
     * @return array
262
     */
263 1
    public function setIgnoreErrors(array $errors)
264
    {
265 1
        $this->flushIgnoreErrors();
266
267 1
        foreach ($errors as $error) {
268 1
            $this->addIgnoreErrors($error);
269
        }
270
271 1
        return $this->getIgnoreErrors();
272
    }
273
274
    /**
275
     * @return string
276
     */
277 1
    public function renderExecutedQuery()
278
    {
279 1
        $out = '';
280
281 1
        foreach ($this->getAllExecutedQuery() as $i => $query) {
282
            $out .= '<fieldset style="text-align:left">';
283
            $out .= '<legend>Query ' . $i . ' - ' . sprintf($this->timeFormat, $query['time']) . '</legend>';
284
            $out .= $query['sql'] . '<br><br>';
285
            if (! empty($query['element'])) {
286
                $out .= $query['element']['type'] . '  => ' . $query['element']['name'] . '<br>';
287
            }
288
            if (! empty($query['event'])) {
289
                $out .= 'Current Event  => ' . $query['event'] . '<br>';
290
            }
291
            $out .= 'Affected Rows => ' . $query['rows'] . '<br>';
292
            if (! empty($query['path'])) {
293
                $out .= 'Functions Path => ' . $query['path'] . '<br>';
294
            }
295
            /*$out .= 'Functions Path => ' . $query['path']['method'] . '<br>';
296
            $out .= empty($query['path']['file']) ?: $query['path']['file'] . '<br />';*/
297
            $out .= '</fieldset><br />';
298
        }
299
300 1
        return $out;
301
    }
302
}
303