Completed
Push — master ( 3c3d26...8e11a5 )
by Mahmoud
03:30
created

QueryDebuggerTask::run()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 9
nc 2
nop 2
1
<?php
2
3
namespace App\Containers\Debugger\Tasks;
4
5
use App;
6
use DB;
7
use Illuminate\Support\Facades\Config;
8
use Log;
9
10
/**
11
 * Class QueryDebuggerTask.
12
 *
13
 * @author  Mahmoud Zalt <[email protected]>
14
 */
15
class QueryDebuggerTask
16
{
17
18
    /**
19
     * Write the DB queries in the Log and Display them in the
20
     * terminal (in case you want to see them while executing the tests).
21
     *
22
     * @param bool|false $terminal
23
     */
24
    public function run($log = true, $terminal = false)
25
    {
26
        if (Config::get('debugger.queries.debug')) {
27
            DB::listen(function ($event) use ($terminal, $log) {
28
                $fullQuery = vsprintf(str_replace(['%', '?'], ['%%', '%s'], $event->sql), $event->bindings);
29
30
                $text = $event->connectionName . ' (' . $event->time . '): ' . $fullQuery;
31
32
                if ($terminal) {
33
                    dump($text);
34
                }
35
36
                if ($log) {
37
                    Log::info($text);
38
                }
39
            });
40
        }
41
    }
42
43
}
44