Completed
Push — master ( 270485...f89d9f )
by Mahmoud
03:15
created

QueryDebuggerTrait::runQueryDebugger()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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