Skip to content

Commit

Permalink
refactor some more config
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Jun 10, 2024
1 parent 556e526 commit d7b719c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 22 deletions.
6 changes: 2 additions & 4 deletions src/Config/BackupConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ protected function __construct(
public string $databaseDumpFilenameBase,
public string $databaseDumpFileExtension,
public DestinationConfig $destination,
public string $temporaryDirectory,
public ?string $temporaryDirectory,
public ?string $password,
public string $encryption,
public int $tries,
public int $retryDelay,
public ?NotificationsConfig $notifications,
public ?MonitoredBackupsConfig $monitoredBackups,
) {
}
Expand All @@ -40,12 +39,11 @@ public static function fromArray(array $data): self
databaseDumpFilenameBase: $data['database_dump_filename_base'],
databaseDumpFileExtension: $data['database_dump_file_extension'],
destination: DestinationConfig::fromArray($data['destination']),
temporaryDirectory: $data['temporary_directory'],
temporaryDirectory: $data['temporary_directory'] ?? null,
password: $data['password'],
encryption: $data['encryption'],
tries: $data['tries'],
retryDelay: $data['retry_delay'],
notifications: isset($data['notifications']) ? NotificationsConfig::fromArray($data['notifications']) : null,
monitoredBackups: $monitoredBackups ? MonitoredBackupsConfig::fromArray($monitoredBackups) : null,
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Config extends Data
{
protected function __construct(
public BackupConfig $backup,
public NotificationsConfig $notifications,
public MonitoredBackupsConfig $monitoredBackups,
public CleanupConfig $cleanup,
) {
Expand All @@ -26,6 +27,7 @@ public static function fromArray(array $data): self
{
return new self(
backup: BackupConfig::fromArray($data['backup']),
notifications: NotificationsConfig::fromArray($data['notifications']),
monitoredBackups: MonitoredBackupsConfig::fromArray($data['monitor_backups']),
cleanup: CleanupConfig::fromArray($data['cleanup']),
);
Expand Down
4 changes: 2 additions & 2 deletions src/Config/NotificationDiscordConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class NotificationDiscordConfig extends Data
{
protected function __construct(
public string $webhookUrl,
public string $channel,
public string $username,
public string $avatar_url,
) {
}
Expand All @@ -18,7 +18,7 @@ public static function fromArray(array $data): self
{
return new self(
webhookUrl: $data['webhook_url'],
channel: $data['channel'],
username: $data['username'],
avatar_url: $data['avatar_url'],
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Config/NotificationMailConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class NotificationMailConfig extends Data
{
/**
* @param array{address: string, name: string} $from
* @param array{address: string|null, name: string|null} $from
*/
protected function __construct(
public string $to,
Expand All @@ -23,11 +23,12 @@ protected function __construct(
*/
public static function fromArray(array $data): self
{
if (filter_var($data['to'], FILTER_VALIDATE_EMAIL)) {
if (! filter_var($data['to'], FILTER_VALIDATE_EMAIL)) {
throw InvalidConfig::invalidEmail($data['to']);
}

if (filter_var($data['from']['address'], FILTER_VALIDATE_EMAIL)) {
if ($data['from']['address']
&& ! filter_var($data['from']['address'], FILTER_VALIDATE_EMAIL)) {
throw InvalidConfig::invalidEmail($data['from']['address']);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Config/NotificationsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public static function fromArray(array $data): self
return new self(
notifications: $data['notifications'],
notifiable: $data['notifiable'],
mail: $data['mail'],
slack: $data['slack'],
discord: $data['discord'],
mail: NotificationMailConfig::fromArray($data['mail']),
slack: NotificationSlackConfig::fromArray($data['slack']),
discord: NotificationDiscordConfig::fromArray($data['discord']),
);
}
}
8 changes: 7 additions & 1 deletion src/Notifications/BaseNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@
use Illuminate\Notifications\Notification;
use Illuminate\Support\Collection;
use Spatie\Backup\BackupDestination\BackupDestination;
use Spatie\Backup\Config\Config;
use Spatie\Backup\Helpers\Format;

abstract class BaseNotification extends Notification
{
/** @return array<string, string> */
public function via(): array
{
$notificationChannels = config('backup.notifications.notifications.'.static::class);
$notificationChannels = $this->config()->notifications->notifications[static::class];

return array_filter($notificationChannels);
}

public function config(): Config
{
return app(Config::class);
}

public function applicationName(): string
{
$name = config('app.name') ?? config('app.url') ?? 'Laravel';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ public function __construct(

public function toMail(): MailMessage
{
$address = $this->config()->notifications->mail->from['address'] ?? config('mail.from.address');
$name = $this->config()->notifications->mail->from['name'] ?? config('mail.from.name');

$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')))
->from($address, $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());
Expand All @@ -44,8 +47,8 @@ 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'))
->from($this->config()->notifications->slack->username, $this->config()->notifications->slack->icon)
->to($this->config()->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());
Expand Down Expand Up @@ -77,7 +80,7 @@ public function toDiscord(): DiscordMessage
{
$discordMessage = (new DiscordMessage())
->error()
->from(config('backup.notifications.discord.username'), config('backup.notifications.discord.avatar_url'))
->from($this->config()->notifications->discord->username, $this->config()->notifications->discord->avatar_url)
->title(
trans('backup::notifications.unhealthy_backup_found_subject_title', [
'application_name' => $this->applicationName(),
Expand Down
12 changes: 7 additions & 5 deletions src/Tasks/Backup/BackupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function setBackupDestinations(Collection $backupDestinations): self
/** @throws Exception */
public function run(): void
{
$temporaryDirectoryPath = config('backup.backup.temporary_directory') ?? storage_path('app/backup-temp');
$temporaryDirectoryPath = $this->config->backup->temporaryDirectory ?? storage_path('app/backup-temp');

$this->temporaryDirectory = (new TemporaryDirectory($temporaryDirectoryPath))
->name('temp')
Expand Down Expand Up @@ -230,7 +230,7 @@ protected function createZipContainingEveryFileInManifest(Manifest $manifest): s
{
consoleOutput()->info("Zipping {$manifest->count()} files and directories...");

$pathToZip = $this->temporaryDirectory->path(config('backup.backup.destination.filename_prefix').$this->filename);
$pathToZip = $this->temporaryDirectory->path($this->config->backup->destination->filenamePrefix.$this->filename);

$zip = Zip::createForManifest($manifest, $pathToZip);

Expand Down Expand Up @@ -259,7 +259,7 @@ protected function dumpDatabases(): array

$dbType = mb_strtolower(basename(str_replace('\\', '/', $dbDumper::class)));

if (config('backup.backup.database_dump_filename_base') === 'connection') {
if ($this->config->backup->databaseDumpFilenameBase === 'connection') {
$dbName = $key;
} elseif ($dbDumper instanceof Sqlite) {
$dbName = $key.'-database';
Expand All @@ -268,12 +268,14 @@ protected function dumpDatabases(): array
}

$timeStamp = '';
if ($timeStampFormat = config('backup.backup.database_dump_file_timestamp_format')) {

if ($timeStampFormat = $this->config->backup->databaseDumpFileTimestampFormat) {
$timeStamp = '-'.Carbon::now()->format($timeStampFormat);
}

$fileName = "{$dbType}-{$dbName}{$timeStamp}.{$this->getExtension($dbDumper)}";

// @todo is this still relevant or undocumented?
if (config('backup.backup.gzip_database_dump')) {
$dbDumper->useCompressor(new GzipCompressor());
$fileName .= '.'.$dbDumper->getCompressorExtension();
Expand Down Expand Up @@ -334,7 +336,7 @@ protected function sendNotification(object|string $notification): void

protected function getExtension(DbDumper $dbDumper): string
{
if ($extension = config('backup.backup.database_dump_file_extension')) {
if ($extension = $this->config->backup->databaseDumpFileExtension) {
return $extension;
}

Expand Down

0 comments on commit d7b719c

Please sign in to comment.