Completed
Push — master ( fbf93a...2fa69d )
by Mark
24:00 queued 19:19
created

SlowQueryServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 0
loc 40
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
A register() 0 3 1
A setupEvents() 0 15 2
A makeQuery() 0 4 1
A slowQueryCheck() 0 4 1
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
use Vormkracht10\SlowQuery\Query;
12
13
class SlowQueryServiceProvider extends ServiceProvider
14
{
15
    /**
16
     * Bootstrap the application services.
17
     */
18
    public function boot()
19
    {
20
        $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...
21
    }
22
23
    public function register()
24
    {
25
    }
26
27
    public function setupEvents($events)
28
    {
29
        $events->listen(QueryExecuted::class, function (QueryExecuted $query) {
30
            $query = $this->makeQuery($query->sql, $query->bindings, $query->time, $query->connectionName);
31
32
            if ($this->slowQueryCheck($query)) {
33
                event(QueryExecutedSlowly($query));
34
            }
35
        });
36
37
        $events->listen(QueryExecutedSlowly::class, function (QueryExecutedSlowly $event) {
38
            Notification::route('discord', '680703864172707841')
39
                ->notify(new SlowQueryDetected($event->query));
40
        });
41
    }
42
43
    public function makeQuery($sql, $bindings, $time, $connectionName)
44
    {
45
        return new Query($sql, $bindings, $time, $connectionName);
46
    }
47
48
    public function slowQueryCheck(Query $query)
49
    {
50
        return $query->time > Config::get('slow-query.slow_query_treshold_in_ms');
51
    }
52
}
53