SlowQueryDetected   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A via() 0 4 1
A toDiscord() 0 37 2
1
<?php
2
3
namespace Vormkracht10\SlowQuery\Notifications;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Database\Events\QueryExecuted;
7
use Illuminate\Notifications\Notification;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Facades\App;
10
use Illuminate\Support\Facades\Config;
11
use Illuminate\Support\Facades\Request;
12
use NotificationChannels\Discord\DiscordChannel;
13
use NotificationChannels\Discord\DiscordMessage;
14
15
class SlowQueryDetected extends Notification
16
{
17
    use Queueable;
18
19
    public function __construct(QueryExecuted $query)
20
    {
21
        $this->query = $query;
0 ignored issues
show
Bug introduced by
The property query does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
    }
23
24
    public function via()
25
    {
26
        return [DiscordChannel::class];
27
    }
28
29
    public function toDiscord()
30
    {
31
        $message = Arr::random([
32
            '☝🏻', '⚠️', '❗️', '🚨', '🤖',
33
        ]);
34
35
        $message .= ' Slow query has been detected on **' . Config::get('app.name') . '**';
36
        $additional = [];
37
38
        if (!App::runningInConsole()) {
39
            $url = Request::fullUrl();
40
41
            $additional = [
42
                'url' => $url,
43
            ];
44
        }
45
46
        return DiscordMessage::create($message, array_merge([
47
            'title' => Config::get('app.name'),
48
            'color' => '15105570',
49
            'fields' => [
50
                [
51
                    'name' => 'SQL',
52
                    'value' => $this->query->sql,
53
                    'inline' => false,
54
                ], [
55
                    'name' => 'Time run',
56
                    'value' => ($this->query->time / 1000) . ' seconds',
57
                    'inline' => false,
58
                ], [
59
                    'name' => 'URL',
60
                    'value' => $url ?? 'Console',
61
                    'inline' => false,
62
                ],
63
            ],
64
        ], $additional));
65
    }
66
}
67