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\LaraLogsRequest; |
11
|
|
|
|
12
|
|
|
class RouteRequested extends Notification implements ShouldQueue |
13
|
|
|
{ |
14
|
|
|
use Queueable; |
15
|
|
|
|
16
|
|
|
private $laraLogsRequest; |
17
|
|
|
|
18
|
|
|
private $requestInfo; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Create a new notification instance. |
22
|
|
|
* |
23
|
|
|
* @return void |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
public function __construct(LaraLogsRequest $laraLogsRequest) |
26
|
|
|
{ |
27
|
|
|
$this->laraLogsRequest = $laraLogsRequest; |
28
|
|
|
|
29
|
|
|
$this->requestInfo = array( |
30
|
|
|
'id' => $laraLogsRequest->id, |
31
|
|
|
'method' => $laraLogsRequest->method, |
32
|
|
|
'uri' => $laraLogsRequest->uri, |
33
|
|
|
'ip' => $laraLogsRequest->ip, |
34
|
|
|
'execution_time' => floor(($laraLogsRequest->end_time - $laraLogsRequest->start_time) * 1000) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the notification's delivery channels. |
40
|
|
|
* |
41
|
|
|
* @param mixed $notifiable |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function via($notifiable) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
switch($notifiable->notify_by) { |
47
|
|
|
case 'email': |
48
|
|
|
return ['mail']; |
49
|
|
|
break; |
|
|
|
|
50
|
|
|
case 'slack': |
51
|
|
|
return ['slack']; |
52
|
|
|
break; |
|
|
|
|
53
|
|
|
case 'email_slack': |
54
|
|
|
return ['mail', 'slack']; |
55
|
|
|
break; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the mail representation of the notification. |
61
|
|
|
* |
62
|
|
|
* @param mixed $notifiable |
63
|
|
|
* @return \Illuminate\Notifications\Messages\MailMessage |
64
|
|
|
*/ |
65
|
|
|
public function toMail($notifiable) |
66
|
|
|
{ |
67
|
|
|
$content = 'A route on ' . url('/') . ' was requested.'; |
68
|
|
View Code Duplication |
if($notifiable->filter !== '*' && !is_numeric($notifiable->filter)) { |
|
|
|
|
69
|
|
|
$content .= " You're being notified because the route contains `" . $notifiable->filter . "`"; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if(is_numeric($notifiable->filter)) { |
73
|
|
|
$content .= " You're being notified because the execution time exceeded your limit of " . $notifiable->filter . "ms."; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$alertColor = '#00B945'; |
77
|
|
View Code Duplication |
if(is_numeric($notifiable->filter) && $this->requestInfo['execution_time'] > intval($notifiable->filter)) { |
|
|
|
|
78
|
|
|
$alertColor = '#BC001A'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return (new MailMessage) |
82
|
|
|
->subject(env('LARALogs_ROUTE_SUBJECT', '[LaraLogs Alert] A route has been requested')) |
83
|
|
|
->from(env('LARALogs_FROM_EMAIL', '[email protected]'), env('LARALogs_FROM_NAME', 'LaraLogs Alerts')) |
84
|
|
|
->view( |
85
|
|
|
'laraLogs::emails.route-requested', [ |
86
|
|
|
'requestInfo' => $this->requestInfo, |
87
|
|
|
'content' => $content, |
88
|
|
|
'alertColor' => $alertColor |
89
|
|
|
] |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function toSlack($notifiable) |
94
|
|
|
{ |
95
|
|
|
$content = 'A route on ' . url('/') . ' was requested.'; |
96
|
|
View Code Duplication |
if($notifiable->filter !== '*' && !is_numeric($notifiable->filter)) { |
|
|
|
|
97
|
|
|
$content .= " You're being notified because the route contains `" . $notifiable->filter . "`"; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if(is_numeric($notifiable->filter)) { |
101
|
|
|
$content .= " You're being notified because the execution time exceeded your limit of " . $notifiable->filter . "ms."; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$status = 'success'; |
105
|
|
View Code Duplication |
if(is_numeric($notifiable->filter) && $this->requestInfo['execution_time'] > intval($notifiable->filter)) { |
|
|
|
|
106
|
|
|
$status = 'error'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$requestInfo = $this->requestInfo; |
110
|
|
|
|
111
|
|
|
return (new SlackMessage) |
112
|
|
|
->{ $status }() |
113
|
|
|
->attachment( |
114
|
|
|
function ($attachment) use ($requestInfo, $content) { |
115
|
|
|
$attachment->title('Request #' . $requestInfo['id'], route('laraLogs::requests.show', $requestInfo['id'])) |
116
|
|
|
->content($content) |
117
|
|
|
->fields( |
118
|
|
|
[ |
119
|
|
|
'Method' => $requestInfo['method'], |
120
|
|
|
'From IP' => $requestInfo['ip'], |
121
|
|
|
'Requested URI' => $requestInfo['uri'], |
122
|
|
|
'Execution Time' => $requestInfo['execution_time'] . 'ms' |
123
|
|
|
] |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the array representation of the notification. |
131
|
|
|
* |
132
|
|
|
* @param mixed $notifiable |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function toArray($notifiable) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
return [ |
138
|
|
|
// |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.