Completed
Push — master ( cb2d71...0c0ad4 )
by Marcin
02:04
created

SqlLogger::saveLog()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 1
nop 3
1
<?php
2
3
namespace Mnabialek\LaravelSqlLogger;
4
5
use Exception;
6
use Illuminate\Contracts\Foundation\Application;
7
8
class SqlLogger
9
{
10
    /**
11
     * @var Application
12
     */
13
    private $app;
14
15
    /**
16
     * @var Query
17
     */
18
    private $query;
19
20
    /**
21
     * @var Writer
22
     */
23
    private $writer;
24
25
    /**
26
     * Number of executed queries.
27
     *
28
     * @var int
29
     */
30
    private $queryNumber = 0;
31
32
    /**
33
     * SqlLogger constructor.
34
     *
35
     * @param \Illuminate\Contracts\Foundation\Application $app
36
     * @param Query $query
37
     * @param Writer $writer
38
     */
39
    public function __construct(Application $app, Query $query, Writer $writer)
40
    {
41
        $this->app = $app;
42
        $this->query = $query;
43
        $this->writer = $writer;
44
    }
45
46
    /**
47
     * Log query.
48
     *
49
     * @param string|\Illuminate\Database\Events\QueryExecuted $query
50
     * @param array|null $bindings
51
     * @param float|null $time
52
     */
53
    public function log($query, array $bindings = null, $time = null)
54
    {
55
        ++$this->queryNumber;
56
57
        try {
58
            $sqlQuery = $this->query->get($this->queryNumber, $query, $bindings, $time);
59
            $this->writer->save($sqlQuery);
60
        } catch (Exception $e) {
61
            $this->app['log']->notice("Cannot log query nr {$this->queryNumber}. Exception:\n" . $e);
62
        }
63
    }
64
}
65