SqlLogger::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 3
b 0
f 0
nc 1
nop 3
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Mnabialek\LaravelSqlLogger;
4
5
use Exception;
6
use Illuminate\Container\Container;
7
8
class SqlLogger
9
{
10
    /**
11
     * @var Container
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\Container\Container $app
36
     * @param Query $query
37
     * @param Writer $writer
38
     */
39
    public function __construct(Container $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:" . PHP_EOL . $e);
62
        }
63
    }
64
}
65