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,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,
];
}
}