LogWritten::toMail()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 44

Duplication

Lines 5
Ratio 11.36 %

Importance

Changes 0
Metric Value
dl 5
loc 44
rs 8.5937
c 0
b 0
f 0
cc 6
nc 24
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 LogWritten extends Notification implements ShouldQueue
13
{
14
    use Queueable;
15
16
    private $laraLogsLog;
17
18
    private $requestInfo;
19
20
    private $notificationLevels;
21
22
    /**
23
     * Create a new notification instance.
24
     *
25
     * @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...
26
     */
27
    public function __construct(Finger $laraLogsLog)
28
    {
29
        $this->laraLogsLog = $laraLogsLog;
30
31
        $this->requestInfo = array(
32
            'id' => $laraLogsLog->id,
33
            'level' => $laraLogsLog->level,
34
            'message' => $laraLogsLog->message,
35
            'user_id' => $laraLogsLog->user_id ? $laraLogsLog->user_id : null,
36
            'email' => $laraLogsLog->email ? $laraLogsLog->email : null
37
        );
38
39
        $this->notificationLevels = array(
40
            'error' => [
41
                'emergency',
42
                'alert',
43
                'critical',
44
                'error'
45
            ],
46
            'warning' => [
47
                'warning',
48
                'notice'
49
            ],
50
            'success' => [
51
                'info',
52
                'debug'
53
            ]
54
        );
55
    }
56
57
    /**
58
     * Get the notification's delivery channels.
59
     *
60
     * @param  mixed $notifiable
61
     * @return array
62
     */
63 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...
64
    {
65
        switch($notifiable->notify_by) {
66
        case 'email':
67
            return ['mail'];
68
            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...
69
        case 'slack':
70
            return ['slack'];
71
            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...
72
        case 'email_slack':
73
            return ['mail', 'slack'];
74
            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...
75
        }
76
    }
77
78
    /**
79
     * Get the mail representation of the notification.
80
     *
81
     * @param  mixed $notifiable
82
     * @return \Illuminate\Notifications\Messages\MailMessage
83
     */
84
    public function toMail($notifiable)
85
    {
86
        $content = 'A log on ' . url('/') . ' has been written to.';
87
        if($notifiable->filter !== '*') {
88
            $content .= " You're being notified because the log contains `" . $notifiable->filter . "`";
89
        }
90
91
        $status = 'success';
92 View Code Duplication
        foreach($this->notificationLevels as $triggerLevel => $childLevels) {
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...
93
            if(in_array($this->requestInfo['level'], $childLevels)) {
94
                $status = $triggerLevel;
95
            }
96
        }
97
98
        $fields = array(
99
            'Level' => $this->requestInfo['level'],
100
            'Message' => $this->requestInfo['message']
101
        );
102
103
        if($this->requestInfo['user_id']) {
104
            $fields['User ID'] = $this->requestInfo['user_id'];
105
        }
106
107
        if($this->requestInfo['email']) {
108
            $fields['Associated Email'] = $this->requestInfo['email'];
109
        }
110
111
        $statusColors = array(
112
            'success' => '#00B945',
113
            'warning' => '#ff9f00',
114
            'error' => '#BC001A'
115
        );
116
117
        return (new MailMessage)
118
            ->subject(env('LARALogs_LOG_SUBJECT', '[LaraLogs Alert] The application log has been written to'))
119
            ->from(env('LARALogs_FROM_EMAIL', '[email protected]'), env('LARALogs_FROM_NAME', 'LaraLogs Alerts'))
120
            ->view(
121
                'laraLogs::emails.log-written', [
122
                'requestInfo' => $this->requestInfo,
123
                'content' => $content,
124
                'alertColor' => $statusColors[$status]
125
                ]
126
            );
127
    }
128
129
    public function toSlack($notifiable)
130
    {
131
        $content = 'A log on ' . url('/') . ' has been written to.';
132
        if($notifiable->filter !== '*') {
133
            $content .= " You're being notified because the log contains `" . $notifiable->filter . "`";
134
        }
135
136
        $status = 'success';
137 View Code Duplication
        foreach($this->notificationLevels as $triggerLevel => $childLevels) {
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...
138
            if(in_array($this->requestInfo['level'], $childLevels)) {
139
                $status = $triggerLevel;
140
            }
141
        }
142
143
        $fields = array(
144
            'Level' => $this->requestInfo['level'],
145
            'Message' => $this->requestInfo['message']
146
        );
147
148
        if($this->requestInfo['user_id']) {
149
            $fields['User ID'] = $this->requestInfo['user_id'];
150
        }
151
152
        if($this->requestInfo['email']) {
153
            $fields['Associated Email'] = $this->requestInfo['email'];
154
        }
155
156
        $requestInfo = $this->requestInfo;
157
158
        return (new SlackMessage)
159
            ->{ $status }()
160
            ->attachment(
161
                function ($attachment) use ($requestInfo, $fields, $content) {
162
                    $attachment->title('Log #' . $requestInfo['id'], route('laraLogs::logs.show', $requestInfo['id']))
163
                        ->content($content)
164
                        ->fields($fields);
165
                }
166
            );
167
    }
168
169
    /**
170
     * Get the array representation of the notification.
171
     *
172
     * @param  mixed $notifiable
173
     * @return array
174
     */
175
    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...
176
    {
177
        return [
178
            //
179
        ];
180
    }
181
}
182