|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Siravel\Notifications\Metrics; |
|
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 Tracking\Models\Metrics\LarametricsModel; |
|
11
|
|
|
|
|
12
|
|
|
class ModelChanged extends Notification implements ShouldQueue |
|
13
|
|
|
{ |
|
14
|
|
|
use Queueable; |
|
15
|
|
|
|
|
16
|
|
|
private $larametricsModel; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Create a new notification instance. |
|
20
|
|
|
* |
|
21
|
|
|
* @return void |
|
|
|
|
|
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(LarametricsModel $larametricsModel) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->larametricsModel = $larametricsModel; |
|
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) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
switch($notifiable->notify_by) { |
|
37
|
|
|
case 'email': |
|
38
|
|
|
return ['mail']; |
|
39
|
|
|
break; |
|
|
|
|
|
|
40
|
|
|
case 'slack': |
|
41
|
|
|
return ['slack']; |
|
42
|
|
|
break; |
|
|
|
|
|
|
43
|
|
|
case 'email_slack': |
|
44
|
|
|
return ['mail', 'slack']; |
|
45
|
|
|
break; |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$modelInfo = array( |
|
58
|
|
|
'id' => $this->larametricsModel->id, |
|
59
|
|
|
'model' => $this->larametricsModel->model, |
|
60
|
|
|
'method' => $this->larametricsModel->method, |
|
61
|
|
|
'original' => json_decode($this->larametricsModel->original, true), |
|
|
|
|
|
|
62
|
|
|
'changes' => json_decode($this->larametricsModel->changes, true) |
|
|
|
|
|
|
63
|
|
|
); |
|
64
|
|
|
$alertColor = '#ff9f00'; |
|
65
|
|
|
switch($this->larametricsModel->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('LARAMETRICS_MODEL_SUBJECT', '[Larametrics Alert] A model has been ' . $modelInfo['method'])) |
|
76
|
|
|
->from(env('LARAMETRICS_FROM_EMAIL', '[email protected]'), env('LARAMETRICS_FROM_NAME', 'Larametrics Alerts')) |
|
77
|
|
|
->view( |
|
78
|
|
|
'rica.larametrics::emails.model-changed', [ |
|
79
|
|
|
'modelInfo' => $modelInfo, |
|
80
|
|
|
'alertColor' => $alertColor |
|
81
|
|
|
] |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function toSlack($notifiable) |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
$modelInfo = array( |
|
88
|
|
|
'id' => $this->larametricsModel->id, |
|
89
|
|
|
'model' => $this->larametricsModel->model, |
|
90
|
|
|
'original' => json_decode($this->larametricsModel->original, true), |
|
|
|
|
|
|
91
|
|
|
'changes' => json_decode($this->larametricsModel->changes, true) |
|
|
|
|
|
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
switch($this->larametricsModel->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('rica.larametrics::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; |
|
|
|
|
|
|
110
|
|
View Code Duplication |
case 'created': |
|
|
|
|
|
|
111
|
|
|
return (new SlackMessage) |
|
112
|
|
|
->success() |
|
113
|
|
|
->attachment( |
|
114
|
|
|
function ($attachment) use ($modelInfo) { |
|
115
|
|
|
$attachment->title($modelInfo['model'] . ' #' . $modelInfo['original']['id'], route('rica.larametrics::models.show', $modelInfo['id'])) |
|
116
|
|
|
->content('A model on ' . url('/') . ' has been created.'); |
|
117
|
|
|
} |
|
118
|
|
|
); |
|
119
|
|
|
break; |
|
|
|
|
|
|
120
|
|
View Code Duplication |
case 'deleted': |
|
|
|
|
|
|
121
|
|
|
return (new SlackMessage) |
|
122
|
|
|
->error() |
|
123
|
|
|
->attachment( |
|
124
|
|
|
function ($attachment) use ($modelInfo) { |
|
125
|
|
|
$attachment->title($modelInfo['model'] . ' #' . $modelInfo['original']['id'], route('rica.larametrics::models.show', $modelInfo['id'])) |
|
126
|
|
|
->content('A model on ' . url('/') . ' has been deleted.'); |
|
127
|
|
|
} |
|
128
|
|
|
); |
|
129
|
|
|
break; |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
return [ |
|
142
|
|
|
// |
|
143
|
|
|
]; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
Adding a
@returnannotation 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.