Change from notifiable to mailable

This commit is contained in:
Matthew Shillam 2018-10-22 17:36:31 +01:00
parent 1f0d84f3d5
commit 87f3be0cb1
9 changed files with 135 additions and 278 deletions

View File

@ -2,14 +2,15 @@
namespace App;
use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Mail\Mailable;
use App\Traits\Uuids;
// use Illuminate\Notifications\Notifiable;
class Client extends Model
{
use Uuids;
use Notifiable;
// use Notifiable;
/**
* Indicates if the IDs are auto-incrementing.
@ -24,13 +25,6 @@ class Client extends Model
*
* @var array
*/
// protected $fillable = [
// 'client_name',
// 'secret_key',
// 'access_key',
// 'setup_config'
// ];
protected $guarded = [ 'id' ];
}

View File

@ -1,19 +1,21 @@
<?php
namespace App\Http\Controllers\API\v1;
use App\Notifications\BackupError;
use App\Notifications\BackupSuccess;
use App\Notifications\BackupWarning;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
use App\Mail\BackupWarning;
use App\Mail\BackupSuccess;
use App\Mail\BackupError;
use Carbon\Carbon;
use App\Schedule;
use App\Backup;
use App\Client;
use Carbon\Carbon;
class BackupController extends Controller
{
/**
* Display a listing of the resource.
*
@ -36,8 +38,6 @@ class BackupController extends Controller
{
$data = parse_duplicati_message($request['message']);
$current_time = Carbon::now()->toDateTimeString();
// Sort out timestamps from $message
// dd($data);
$begin_time = parse_duplicati_time($data['BeginTime']);
$end_time = parse_duplicati_time($data['EndTime']);
@ -81,14 +81,23 @@ class BackupController extends Controller
// notify client by email.
$client = Client::where('id', '=', $client_id)->first();
if($backup->status == "Success"){
$client->notify(new BackupSuccess($backup));
if($backup->status == "Success")
{
Mail::to($client->email)
->cc(['matthew@shillam.me.uk','j.kendrick@dentalsupportuk.com'])
->send(new BackupSuccess($backup));
}
elseif($backup->status == "Warning"){
$client->notify(new BackupWarning($backup));
elseif($backup->status == "Warning")
{
Mail::to($client->email)
->cc(['matthew@shillam.me.uk','j.kendrick@dentalsupportuk.com'])
->send(new BackupWarning($backup));
}
elseif($backup->status == "Error"){
$client->notify(new BackupError($backup));
elseif($backup->status == "Error")
{
Mail::to($client->email)
->cc(['matthew@shillam.me.uk','j.kendrick@dentalsupportuk.com'])
->send(new BackupError($backup));
}
}

36
app/Mail/BackupError.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace App\Mail;
use App\Backup;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class BackupError extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Backup $backup)
{
$this->backup = $backup;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('reports@mydentalbackup.co.uk')
->subject('ERROR Your backup ( ' . $this->backup->schedule_name . ' ) has a warning!')
->markdown('mail.backup.warning', ['backup' => $this->backup]);
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Mail;
use App\Backup;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class BackupSuccess extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Backup $backup)
{
$this->backup = $backup;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('reports@mydentalbackup.co.uk')
->subject('SUCCESS Your backup ( ' . $this->backup->schedule_name . ' ) completed succesfully')
->markdown('mail.backup.success', ['backup' => $this->backup]);
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Mail;
use App\Backup;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class BackupWarning extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Backup $backup)
{
$this->backup = $backup;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('reports@mydentalbackup.co.uk')
->subject('SUCCESS Your backup ( ' . $this->backup->schedule_name . ' ) completed succesfully')
->markdown('mail.backup.success', ['backup' => $this->backup]);
}
}

View File

@ -1,68 +0,0 @@
<?php
namespace App\Notifications;
use App\Backup;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class BackupComplete extends Notification
{
use Queueable;
// protected Backup;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Backup $backup)
{
$this->backup = $backup;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The backup schedule ( ' . $this->backup->schedule_name . ' ) has completed succesfully!')
->action('Contact us ', url('/'))
->line($this->backup->schedule_name . ' has completed successfully! yay!')
->line('Size of added files: ' . readableBytes($this->backup->size_of_added_files))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

View File

@ -1,62 +0,0 @@
<?php
namespace App\Notifications;
use App\Backup;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class BackupError extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Backup $backup)
{
$this->backup = $backup;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('ERROR Your backup ( ' . $this->backup->schedule_name . ' ) has an error!')
->greeting('Error!')
->markdown('mail.backup.error', ['backup' => $this->backup]);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

View File

@ -1,62 +0,0 @@
<?php
namespace App\Notifications;
use App\Backup;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class BackupSuccess extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Backup $backup)
{
$this->backup = $backup;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('SUCCESS Your backup ( ' . $this->backup->schedule_name . ' ) completed succesfully')
->greeting('Success!')
->markdown('mail.backup.success', ['backup' => $this->backup]);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

View File

@ -1,62 +0,0 @@
<?php
namespace App\Notifications;
use App\Backup;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class BackupWarning extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Backup $backup)
{
$this->backup = $backup;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('ERROR Your backup ( ' . $this->backup->schedule_name . ' ) has a warning!')
->greeting('Error!')
->markdown('mail.backup.warning', ['backup' => $this->backup]);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}