Completed
Push — master ( 916ec1...b80e24 )
by Marcin
11:36
created

ServiceProvider::shouldLogAnything()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace Mnabialek\LaravelSqlLogger\Providers;
4
5
use Mnabialek\LaravelSqlLogger\Config;
6
use Mnabialek\LaravelSqlLogger\SqlLogger;
7
8
class ServiceProvider extends \Illuminate\Support\ServiceProvider
9
{
10
    /**
11
     * @var Config
12
     */
13
    protected $config;
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function __construct($app)
19
    {
20
        parent::__construct($app);
21
        $this->config = $this->app->make(Config::class);
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function register()
28
    {
29
        // merge config
30
        $this->mergeConfigFrom(__DIR__ . '/../../publish/config/sql_logger.php', 'sql_logger');
31
32
        // register files to be published
33
        $this->publishes($this->getPublished());
34
35
        // if no logging is enabled, we can stop here, nothing more should be done
36
        if (! $this->shouldLogAnything()) {
37
            return;
38
        }
39
40
        // create logger class
41
        $logger = new SqlLogger($this->app, $this->config);
42
43
        // listen to database queries
44
        $this->app['db']->listen(function (
45
            $query,
46
            $bindings = null,
47
            $time = null
48
        ) use ($logger) {
49
            $logger->log($query, $bindings, $time);
50
        });
51
    }
52
53
    /**
54
     * Get files to be published.
55
     *
56
     * @return array
57
     */
58
    protected function getPublished()
59
    {
60
        return [
61
            realpath(__DIR__ .
62
                '/../../publish/config/sql_logger.php') => (function_exists('config_path') ?
63
                    config_path('sql_logger.php') :
64
                    base_path('config/sql_logger.php')),
65
        ];
66
    }
67
68
    /**
69
     * Verify whether anything should be logged.
70
     *
71
     * @return bool
72
     */
73
    protected function shouldLogAnything()
74
    {
75
        return $this->config->logQueries() || $this->config->logSlowQueries();
76
    }
77
}
78