ModelChanged::toSlack()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 47

Duplication

Lines 20
Ratio 42.55 %

Importance

Changes 0
Metric Value
dl 20
loc 47
rs 8.8452
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
3
namespace Audit\Notifications\Logs;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Notifications\Notification;
7
use Illuminate\Contracts\Queue\ShouldQueue;
8
use Illuminate\Notifications\Messages\MailMessage;
9
use Illuminate\Notifications\Messages\SlackMessage;
10
use Audit\Models\Logs\Finger;
11
12
class ModelChanged extends Notification implements ShouldQueue
13
{
14
    use Queueable;
15
16
    private $finger;
17
18
    /**
19
     * Create a new notification instance.
20
     *
21
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
22
     */
23
    public function __construct(Finger $finger)
24
    {
25
        $this->finger = $finger;
26
    }
27
28
    /**
29
     * Get the notification's delivery channels.
30
     *
31
     * @param  mixed $notifiable
32
     * @return array
33
     */
34 View Code Duplication
    public function via($notifiable)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        switch($notifiable->notify_by) {
37
        case 'email':
38
            return ['mail'];
39
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
40
        case 'slack':
41
            return ['slack'];
42
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
43
        case 'email_slack':
44
            return ['mail', 'slack'];
45
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
46
        }
47
    }
48
49
    /**
50
     * Get the mail representation of the notification.
51
     *
52
     * @param  mixed $notifiable
53
     * @return \Illuminate\Notifications\Messages\MailMessage
54
     */
55
    public function toMail($notifiable)
0 ignored issues
show
Unused Code introduced by
The parameter $notifiable is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57
        $modelInfo = array(
58
            'id' => $this->finger->id,
59
            'model' => $this->finger->model,
60
            'method' => $this->finger->method,
61
            'original' => json_decode($this->finger->original, true),
62
            'changes' => json_decode($this->finger->changes, true)
63
        );
64
        $alertColor = '#ff9f00';
65
        switch($this->finger->method) {
66
        case 'created':
67
            $alertColor = '#00B945';
68
            break;
69
        case 'deleted':
70
            $alertColor = '#BC001A';
71
            break;
72
        }
73
74
        return (new MailMessage)
75
            ->subject(env('LARALogs_MODEL_SUBJECT', '[LaraLogs Alert] A model has been ' . $modelInfo['method']))
76
            ->from(env('LARALogs_FROM_EMAIL', '[email protected]'), env('LARALogs_FROM_NAME', 'LaraLogs Alerts'))
77
            ->view(
78
                'laraLogs::emails.model-changed', [
79
                'modelInfo' => $modelInfo,
80
                'alertColor' => $alertColor
81
                ]
82
            );
83
    }
84
85
    public function toSlack($notifiable)
0 ignored issues
show
Unused Code introduced by
The parameter $notifiable is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87
        $modelInfo = array(
88
            'id' => $this->finger->id,
89
            'model' => $this->finger->model,
90
            'original' => json_decode($this->finger->original, true),
91
            'changes' => json_decode($this->finger->changes, true)
92
        );
93
        
94
        switch($this->finger->method) {
95
        case 'updated':
96
            return (new SlackMessage)
97
                ->warning()
98
                ->attachment(
99
                    function ($attachment) use ($modelInfo) {
100
                        $columnsChanged = '';
101
                        foreach(array_keys($modelInfo['changes']) as $changedColumn) {
102
                            $columnsChanged .= '    • ' . $changedColumn . "\r\n";
103
                        }
104
                        $attachment->title($modelInfo['model'] . ' #' . $modelInfo['original']['id'], route('laraLogs::models.show', $modelInfo['id']))
105
                            ->content('A model on ' . url('/') . ' has been updated. The following columns have changed:' . "\r\n" . $columnsChanged)
106
                            ->markdown(['text']);
107
                    }
108
                );
109
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
110 View Code Duplication
        case 'created':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
            return (new SlackMessage)
112
                ->success()
113
                ->attachment(
114
                    function ($attachment) use ($modelInfo) {
115
                        $attachment->title($modelInfo['model'] . ' #' . $modelInfo['original']['id'], route('laraLogs::models.show', $modelInfo['id']))
116
                            ->content('A model on ' . url('/') . ' has been created.');
117
                    }
118
                );
119
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
120 View Code Duplication
        case 'deleted':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
            return (new SlackMessage)
122
                ->error()
123
                ->attachment(
124
                    function ($attachment) use ($modelInfo) {
125
                        $attachment->title($modelInfo['model'] . ' #' . $modelInfo['original']['id'], route('laraLogs::models.show', $modelInfo['id']))
126
                            ->content('A model on ' . url('/') . ' has been deleted.');
127
                    }
128
                );
129
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
130
        }
131
    }
132
133
    /**
134
     * Get the array representation of the notification.
135
     *
136
     * @param  mixed $notifiable
137
     * @return array
138
     */
139
    public function toArray($notifiable)
0 ignored issues
show
Unused Code introduced by
The parameter $notifiable is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
140
    {
141
        return [
142
            //
143
        ];
144
    }
145
}
146