SlowQueryServiceProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 41
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A slowQueryCheck() 0 4 1
A boot() 0 8 1
A register() 0 4 1
A setupEvents() 0 16 2
1
<?php
2
3
namespace Vormkracht10\SlowQuery;
4
5
use Illuminate\Database\Events\QueryExecuted;
6
use Illuminate\Support\Facades\Config;
7
use Illuminate\Support\Facades\Notification;
8
use Illuminate\Support\ServiceProvider;
9
use Vormkracht10\SlowQuery\Events\QueryExecutedSlowly;
10
use Vormkracht10\SlowQuery\Notifications\SlowQueryDetected;
11
12
class SlowQueryServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * Bootstrap the application services.
16
     */
17
    public function boot()
18
    {
19
        $this->publishes([
20
            __DIR__ . '/../config/slow-query.php' => config_path('slow-query.php'),
21
        ], 'config');
22
23
        $this->setupEvents($this->app->events);
0 ignored issues
show
Bug introduced by
Accessing events on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
24
    }
25
26
    public function register()
27
    {
28
        $this->mergeConfigFrom(__DIR__ . '/../config/slow-query.php', 'slow-query');
29
    }
30
31
    public function setupEvents($events)
32
    {
33
        $events->listen(QueryExecuted::class, function (QueryExecuted $query) {
34
            if ($this->slowQueryCheck($query)) {
35
                event(new QueryExecutedSlowly($query));
36
            }
37
        });
38
39
        $events->listen(QueryExecutedSlowly::class, function (QueryExecutedSlowly $event) {
40
            $provider = Config::get('slow-query.notifications.default');
41
            $channel = Config::get('slow-query.notifications.' . $provider . '.channel');
42
43
            Notification::route($provider, $channel)
44
                ->notify(new SlowQueryDetected($event->query));
45
        });
46
    }
47
48
    public function slowQueryCheck(QueryExecuted $query)
49
    {
50
        return $query->time >= Config::get('slow-query.treshold_in_ms');
51
    }
52
}
53