Primo Committ

This commit is contained in:
paoloar77
2024-05-07 12:17:25 +02:00
commit e73d0e5113
7204 changed files with 884387 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
<?php
namespace Spatie\Backup\Notifications;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Collection;
use Spatie\Backup\BackupDestination\BackupDestination;
use Spatie\Backup\Helpers\Format;
abstract class BaseNotification extends Notification
{
public function via(): array
{
$notificationChannels = config('backup.notifications.notifications.'.static::class);
return array_filter($notificationChannels);
}
public function applicationName(): string
{
return config('app.name') ?? config('app.url') ?? 'Laravel application';
}
public function backupName(): string
{
return $this->backupDestination()->backupName();
}
public function diskName(): string
{
return $this->backupDestination()->diskName();
}
protected function backupDestinationProperties(): Collection
{
$backupDestination = $this->backupDestination();
if (! $backupDestination) {
return collect();
}
$backupDestination->fresh();
$newestBackup = $backupDestination->newestBackup();
$oldestBackup = $backupDestination->oldestBackup();
return collect([
'Application name' => $this->applicationName(),
'Backup name' => $this->backupName(),
'Disk' => $backupDestination->diskName(),
'Newest backup size' => $newestBackup ? Format::humanReadableSize($newestBackup->size()) : 'No backups were made yet',
'Number of backups' => (string) $backupDestination->backups()->count(),
'Total storage used' => Format::humanReadableSize($backupDestination->backups()->size()),
'Newest backup date' => $newestBackup ? $newestBackup->date()->format('Y/m/d H:i:s') : 'No backups were made yet',
'Oldest backup date' => $oldestBackup ? $oldestBackup->date()->format('Y/m/d H:i:s') : 'No backups were made yet',
])->filter();
}
public function backupDestination(): ?BackupDestination
{
if (isset($this->event->backupDestination)) {
return $this->event->backupDestination;
}
if (isset($this->event->backupDestinationStatus)) {
return $this->event->backupDestinationStatus->backupDestination();
}
return null;
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace Spatie\Backup\Notifications;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Notifications\Notification;
use Spatie\Backup\Events\BackupHasFailed;
use Spatie\Backup\Events\BackupWasSuccessful;
use Spatie\Backup\Events\CleanupHasFailed;
use Spatie\Backup\Events\CleanupWasSuccessful;
use Spatie\Backup\Events\HealthyBackupWasFound;
use Spatie\Backup\Events\UnhealthyBackupWasFound;
use Spatie\Backup\Exceptions\NotificationCouldNotBeSent;
class EventHandler
{
/** @var \Illuminate\Contracts\Config\Repository */
protected $config;
public function __construct(Repository $config)
{
$this->config = $config;
}
public function subscribe(Dispatcher $events)
{
$events->listen($this->allBackupEventClasses(), function ($event) {
$notifiable = $this->determineNotifiable();
$notification = $this->determineNotification($event);
$notifiable->notify($notification);
});
}
protected function determineNotifiable()
{
$notifiableClass = $this->config->get('backup.notifications.notifiable');
return app($notifiableClass);
}
protected function determineNotification($event): Notification
{
$eventName = class_basename($event);
$notificationClass = collect($this->config->get('backup.notifications.notifications'))
->keys()
->first(function ($notificationClass) use ($eventName) {
$notificationName = class_basename($notificationClass);
return $notificationName === $eventName;
});
if (! $notificationClass) {
throw NotificationCouldNotBeSent::noNotificationClassForEvent($event);
}
return new $notificationClass($event);
}
protected function allBackupEventClasses(): array
{
return [
BackupHasFailed::class,
BackupWasSuccessful::class,
CleanupHasFailed::class,
CleanupWasSuccessful::class,
HealthyBackupWasFound::class,
UnhealthyBackupWasFound::class,
];
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Spatie\Backup\Notifications;
use Illuminate\Notifications\Notifiable as NotifiableTrait;
class Notifiable
{
use NotifiableTrait;
public function routeNotificationForMail()
{
return config('backup.notifications.mail.to');
}
public function routeNotificationForSlack()
{
return config('backup.notifications.slack.webhook_url');
}
public function getKey()
{
return 1;
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Spatie\Backup\Notifications\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackAttachment;
use Illuminate\Notifications\Messages\SlackMessage;
use Spatie\Backup\Events\BackupHasFailed as BackupHasFailedEvent;
use Spatie\Backup\Notifications\BaseNotification;
class BackupHasFailed extends BaseNotification
{
/** @var \Spatie\Backup\Events\BackupHasFailed */
protected $event;
public function __construct(BackupHasFailedEvent $event)
{
$this->event = $event;
}
public function toMail(): MailMessage
{
$mailMessage = (new MailMessage)
->error()
->from(config('backup.notifications.mail.from.address', config('mail.from.address')), config('backup.notifications.mail.from.name', config('mail.from.name')))
->subject(trans('backup::notifications.backup_failed_subject', ['application_name' => $this->applicationName()]))
->line(trans('backup::notifications.backup_failed_body', ['application_name' => $this->applicationName()]))
->line(trans('backup::notifications.exception_message', ['message' => $this->event->exception->getMessage()]))
->line(trans('backup::notifications.exception_trace', ['trace' => $this->event->exception->getTraceAsString()]));
$this->backupDestinationProperties()->each(function ($value, $name) use ($mailMessage) {
$mailMessage->line("{$name}: $value");
});
return $mailMessage;
}
public function toSlack(): SlackMessage
{
return (new SlackMessage)
->error()
->from(config('backup.notifications.slack.username'), config('backup.notifications.slack.icon'))
->to(config('backup.notifications.slack.channel'))
->content(trans('backup::notifications.backup_failed_subject', ['application_name' => $this->applicationName()]))
->attachment(function (SlackAttachment $attachment) {
$attachment
->title(trans('backup::notifications.exception_message_title'))
->content($this->event->exception->getMessage());
})
->attachment(function (SlackAttachment $attachment) {
$attachment
->title(trans('backup::notifications.exception_trace_title'))
->content($this->event->exception->getTraceAsString());
})
->attachment(function (SlackAttachment $attachment) {
$attachment->fields($this->backupDestinationProperties()->toArray());
});
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Spatie\Backup\Notifications\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackAttachment;
use Illuminate\Notifications\Messages\SlackMessage;
use Spatie\Backup\Events\BackupWasSuccessful as BackupWasSuccessfulEvent;
use Spatie\Backup\Notifications\BaseNotification;
class BackupWasSuccessful extends BaseNotification
{
/** @var \Spatie\Backup\Events\BackupWasSuccessful */
protected $event;
public function __construct(BackupWasSuccessfulEvent $event)
{
$this->event = $event;
}
public function toMail(): MailMessage
{
$mailMessage = (new MailMessage)
->from(config('backup.notifications.mail.from.address', config('mail.from.address')), config('backup.notifications.mail.from.name', config('mail.from.name')))
->subject(trans('backup::notifications.backup_successful_subject', ['application_name' => $this->applicationName()]))
->line(trans('backup::notifications.backup_successful_body', ['application_name' => $this->applicationName(), 'disk_name' => $this->diskName()]));
$this->backupDestinationProperties()->each(function ($value, $name) use ($mailMessage) {
$mailMessage->line("{$name}: $value");
});
return $mailMessage;
}
public function toSlack(): SlackMessage
{
return (new SlackMessage)
->success()
->from(config('backup.notifications.slack.username'), config('backup.notifications.slack.icon'))
->to(config('backup.notifications.slack.channel'))
->content(trans('backup::notifications.backup_successful_subject_title'))
->attachment(function (SlackAttachment $attachment) {
$attachment->fields($this->backupDestinationProperties()->toArray());
});
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Spatie\Backup\Notifications\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackAttachment;
use Illuminate\Notifications\Messages\SlackMessage;
use Spatie\Backup\Events\CleanupHasFailed as CleanupHasFailedEvent;
use Spatie\Backup\Notifications\BaseNotification;
class CleanupHasFailed extends BaseNotification
{
/** @var \Spatie\Backup\Events\CleanupHasFailed */
protected $event;
public function __construct(CleanupHasFailedEvent $event)
{
$this->event = $event;
}
public function toMail(): MailMessage
{
$mailMessage = (new MailMessage)
->error()
->from(config('backup.notifications.mail.from.address', config('mail.from.address')), config('backup.notifications.mail.from.name', config('mail.from.name')))
->subject(trans('backup::notifications.cleanup_failed_subject', ['application_name' => $this->applicationName()]))
->line(trans('backup::notifications.cleanup_failed_body', ['application_name' => $this->applicationName()]))
->line(trans('backup::notifications.exception_message', ['message' => $this->event->exception->getMessage()]))
->line(trans('backup::notifications.exception_trace', ['trace' => $this->event->exception->getTraceAsString()]));
$this->backupDestinationProperties()->each(function ($value, $name) use ($mailMessage) {
$mailMessage->line("{$name}: $value");
});
return $mailMessage;
}
public function toSlack(): SlackMessage
{
return (new SlackMessage)
->error()
->from(config('backup.notifications.slack.username'), config('backup.notifications.slack.icon'))
->to(config('backup.notifications.slack.channel'))
->content(trans('backup::notifications.cleanup_failed_subject', ['application_name' => $this->applicationName()]))
->attachment(function (SlackAttachment $attachment) {
$attachment
->title(trans('backup::notifications.exception_message_title'))
->content($this->event->exception->getMessage());
})
->attachment(function (SlackAttachment $attachment) {
$attachment
->title(trans('backup::notifications.exception_message_trace'))
->content($this->event->exception->getTraceAsString());
})
->attachment(function (SlackAttachment $attachment) {
$attachment->fields($this->backupDestinationProperties()->toArray());
});
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Spatie\Backup\Notifications\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackAttachment;
use Illuminate\Notifications\Messages\SlackMessage;
use Spatie\Backup\Events\CleanupWasSuccessful as CleanupWasSuccessfulEvent;
use Spatie\Backup\Notifications\BaseNotification;
class CleanupWasSuccessful extends BaseNotification
{
/** @var \Spatie\Backup\Events\CleanupWasSuccessful */
protected $event;
public function __construct(CleanupWasSuccessfulEvent $event)
{
$this->event = $event;
}
public function toMail(): MailMessage
{
$mailMessage = (new MailMessage)
->from(config('backup.notifications.mail.from.address', config('mail.from.address')), config('backup.notifications.mail.from.name', config('mail.from.name')))
->subject(trans('backup::notifications.cleanup_successful_subject', ['application_name' => $this->applicationName()]))
->line(trans('backup::notifications.cleanup_successful_body', ['application_name' => $this->applicationName(), 'disk_name' => $this->diskName()]));
$this->backupDestinationProperties()->each(function ($value, $name) use ($mailMessage) {
$mailMessage->line("{$name}: $value");
});
return $mailMessage;
}
public function toSlack(): SlackMessage
{
return (new SlackMessage)
->success()
->from(config('backup.notifications.slack.username'), config('backup.notifications.slack.icon'))
->to(config('backup.notifications.slack.channel'))
->content(trans('backup::notifications.cleanup_successful_subject_title'))
->attachment(function (SlackAttachment $attachment) {
$attachment->fields($this->backupDestinationProperties()->toArray());
});
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Spatie\Backup\Notifications\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackAttachment;
use Illuminate\Notifications\Messages\SlackMessage;
use Spatie\Backup\Events\HealthyBackupWasFound as HealthyBackupWasFoundEvent;
use Spatie\Backup\Notifications\BaseNotification;
class HealthyBackupWasFound extends BaseNotification
{
/** @var \Spatie\Backup\Events\HealthyBackupWasFound */
protected $event;
public function __construct(HealthyBackupWasFoundEvent $event)
{
$this->event = $event;
}
public function toMail(): MailMessage
{
$mailMessage = (new MailMessage)
->from(config('backup.notifications.mail.from.address', config('mail.from.address')), config('backup.notifications.mail.from.name', config('mail.from.name')))
->subject(trans('backup::notifications.healthy_backup_found_subject', ['application_name' => $this->applicationName(), 'disk_name' => $this->diskName()]))
->line(trans('backup::notifications.healthy_backup_found_body', ['application_name' => $this->applicationName()]));
$this->backupDestinationProperties()->each(function ($value, $name) use ($mailMessage) {
$mailMessage->line("{$name}: $value");
});
return $mailMessage;
}
public function toSlack(): SlackMessage
{
return (new SlackMessage)
->success()
->from(config('backup.notifications.slack.username'), config('backup.notifications.slack.icon'))
->to(config('backup.notifications.slack.channel'))
->content(trans('backup::notifications.healthy_backup_found_subject_title', ['application_name' => $this->applicationName()]))
->attachment(function (SlackAttachment $attachment) {
$attachment->fields($this->backupDestinationProperties()->toArray());
});
}
}

View File

@@ -0,0 +1,91 @@
<?php
namespace Spatie\Backup\Notifications\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackAttachment;
use Illuminate\Notifications\Messages\SlackMessage;
use Spatie\Backup\Events\UnhealthyBackupWasFound as UnhealthyBackupWasFoundEvent;
use Spatie\Backup\Notifications\BaseNotification;
use Spatie\Backup\Tasks\Monitor\HealthCheckFailure;
class UnhealthyBackupWasFound extends BaseNotification
{
/** @var \Spatie\Backup\Events\UnhealthyBackupWasFound */
protected $event;
public function __construct(UnhealthyBackupWasFoundEvent $event)
{
$this->event = $event;
}
public function toMail(): MailMessage
{
$mailMessage = (new MailMessage)
->error()
->from(config('backup.notifications.mail.from.address', config('mail.from.address')), config('backup.notifications.mail.from.name', config('mail.from.name')))
->subject(trans('backup::notifications.unhealthy_backup_found_subject', ['application_name' => $this->applicationName()]))
->line(trans('backup::notifications.unhealthy_backup_found_body', ['application_name' => $this->applicationName(), 'disk_name' => $this->diskName()]))
->line($this->problemDescription());
$this->backupDestinationProperties()->each(function ($value, $name) use ($mailMessage) {
$mailMessage->line("{$name}: $value");
});
if ($this->failure()->wasUnexpected()) {
$mailMessage
->line('Health check: '.$this->failure()->healthCheck()->name())
->line(trans('backup::notifications.exception_message', ['message' => $this->failure()->exception()->getMessage()]))
->line(trans('backup::notifications.exception_trace', ['trace' => $this->failure()->exception()->getTraceAsString()]));
}
return $mailMessage;
}
public function toSlack(): SlackMessage
{
$slackMessage = (new SlackMessage)
->error()
->from(config('backup.notifications.slack.username'), config('backup.notifications.slack.icon'))
->to(config('backup.notifications.slack.channel'))
->content(trans('backup::notifications.unhealthy_backup_found_subject_title', ['application_name' => $this->applicationName(), 'problem' => $this->problemDescription()]))
->attachment(function (SlackAttachment $attachment) {
$attachment->fields($this->backupDestinationProperties()->toArray());
});
if ($this->failure()->wasUnexpected()) {
$slackMessage
->attachment(function (SlackAttachment $attachment) {
$attachment
->title('Health check')
->content($this->failure()->healthCheck()->name());
})
->attachment(function (SlackAttachment $attachment) {
$attachment
->title(trans('backup::notifications.exception_message_title'))
->content($this->failure()->exception()->getMessage());
})
->attachment(function (SlackAttachment $attachment) {
$attachment
->title(trans('backup::notifications.exception_trace_title'))
->content($this->failure()->exception()->getTraceAsString());
});
}
return $slackMessage;
}
protected function problemDescription(): string
{
if ($this->failure()->wasUnexpected()) {
return trans('backup::notifications.unhealthy_backup_found_unknown');
}
return $this->failure()->exception()->getMessage();
}
protected function failure(): HealthCheckFailure
{
return $this->event->backupDestinationStatus->getHealthCheckFailure();
}
}