Skip to content

Commit

Permalink
refactor to Config class
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Jun 7, 2024
1 parent b8b5e50 commit 0e0c095
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php": "^8.1",
"php": "^8.2",
"ext-zip": "^1.14.0",
"illuminate/console": "^10.10.0|^11.0",
"illuminate/contracts": "^10.10.0|^11.0",
Expand Down
1 change: 1 addition & 0 deletions src/BackupDestination/BackupDestination.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Contracts\Filesystem\Filesystem;
use Spatie\Backup\Config\Config;
use Spatie\Backup\Exceptions\InvalidBackupDestination;

class BackupDestination
Expand Down
29 changes: 18 additions & 11 deletions src/Tasks/Backup/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Backup\Tasks\Backup;

use Illuminate\Support\Str;
use Spatie\Backup\Config\Config;
use Spatie\Backup\Helpers\Format;
use ZipArchive;

Expand All @@ -12,10 +13,23 @@ class Zip

protected int $fileCount = 0;

protected Config $config;

public function __construct(protected string $pathToZip)
{
$this->zipFile = new ZipArchive();
$this->config = app(Config::class);

$this->open();
}

public static function createForManifest(Manifest $manifest, string $pathToZip): self
{
$relativePath = config('backup.backup.source.files.relative_path') ?
rtrim((string) config('backup.backup.source.files.relative_path'), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR : false;
$config = app(Config::class);

$relativePath = $config->backup->source->files->relativePath
? rtrim($config->backup->source->files->relativePath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR
: false;

$zip = new static($pathToZip);

Expand Down Expand Up @@ -47,13 +61,6 @@ protected static function determineNameOfFileInZip(string $pathToFile, string $p
return $pathToFile;
}

public function __construct(protected string $pathToZip)
{
$this->zipFile = new ZipArchive();

$this->open();
}

public function path(): string
{
return $this->pathToZip;
Expand Down Expand Up @@ -93,8 +100,8 @@ public function add(string|iterable $files, ?string $nameInZip = null): self
$files = [$files];
}

$compressionMethod = config('backup.backup.destination.compression_method', null);
$compressionLevel = config('backup.backup.destination.compression_level', 9);
$compressionMethod = $this->config->backup->destination->compressionMethod;
$compressionLevel = $this->config->backup->destination->compressionLevel;

foreach ($files as $file) {
if (is_dir($file)) {
Expand Down
12 changes: 10 additions & 2 deletions src/Traits/Retryable.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,19 @@ protected function setTries(string $type): void
return;
}

$this->tries = (int) config('backup.'.$type.'.tries', 1);
$this->tries = match ($type) {
'backup' => $this->config->backup->tries,
'cleanup' => $this->config->cleanup->tries,
default => 1,
};
}

protected function getRetryDelay(string $type): int
{
return (int) config('backup.'.$type.'.retry_delay', 0);
return match ($type) {
'backup' => $this->config->backup->retryDelay,
'cleanup' => $this->config->cleanup->retryDelay,
default => 0,
};
}
}

0 comments on commit 0e0c095

Please sign in to comment.