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

ServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 70
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B register() 0 25 2
A getPublished() 0 9 2
A shouldLogAnything() 0 4 2
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