Skip to content

Commit c1f6737

Browse files
chore(zipper)!: remove open method in favor of read and write methods
1 parent 41c96b9 commit c1f6737

File tree

8 files changed

+35
-31
lines changed

8 files changed

+35
-31
lines changed

src/Backuper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function backup(): BackupDto
3737

3838
$temp_zip_path = config('backup.temp_path') . '/temp.zip';
3939

40-
$zipper = Zipper::open($temp_zip_path);
40+
$zipper = Zipper::write($temp_zip_path);
4141

4242
Pipeline::via('backup')
4343
->send($zipper)

src/Restorer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private function unzip(string $path): string
142142
{
143143
$target = config('backup.temp_path') . DIRECTORY_SEPARATOR . 'unzipping';
144144

145-
Zipper::open($path, true)
145+
Zipper::read($path)
146146
->extractTo($target, config('backup.password'))
147147
->close();
148148

src/StateManager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
public function __construct(
2525
private Repository $cache,
2626
private Filesystem $filesystem,
27-
) {}
27+
) {
28+
}
2829

2930
public function getState(): State
3031
{
@@ -38,7 +39,7 @@ public function getState(): State
3839

3940
if (
4041
!in_array($state, [State::BackupInProgress, State::RestoreInProgress]) &&
41-
$this->cache->has(self::JOB_QUEUED_KEY)
42+
$this->cache->has(self::JOB_QUEUED_KEY)
4243
) {
4344
$state = State::Queued;
4445
}

src/Support/Zipper.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\Finder\SplFileInfo;
1010
use ZipArchive;
1111

12+
// @mago-ignore maintainability/too-many-methods
1213
final class Zipper
1314
{
1415
private ZipArchive $zip;
@@ -26,11 +27,14 @@ public function __construct(string $path, int $flags = ZipArchive::CREATE | ZipA
2627
/**
2728
* Create a new instance of the Zipper.
2829
*/
29-
public static function open(string $path, bool $readOnly = false): self
30+
public static function write(string $path): self
3031
{
31-
$flags = $readOnly ? ZipArchive::RDONLY : (ZipArchive::CREATE | ZipArchive::OVERWRITE);
32+
return new static($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
33+
}
3234

33-
return new static($path, $flags);
35+
public static function read(string $path): self
36+
{
37+
return new static($path, ZipArchive::RDONLY);
3438
}
3539

3640
/**
@@ -115,12 +119,7 @@ public function getArchive(): ZipArchive
115119
*/
116120
public function addMeta(string $key, array|string $meta): self
117121
{
118-
if (is_array($meta)) {
119-
$current = $this->meta[$key] ?? [];
120-
$this->meta[$key] = array_merge($current, $meta);
121-
} else {
122-
$this->meta[$key] = $meta;
123-
}
122+
$this->meta[$key] = is_array($meta) ? array_merge($this->meta[$key] ?? [], $meta) : $meta;
124123

125124
$this->zip->setArchiveComment(comment: json_encode($this->meta));
126125

tests/Feature/RestoreFromPathTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
$emptyArchive = storage_path(config('backup.temp_path') . '/empty.zip');
6565

6666
// The zip file cant be empty, but when extracting it can if the password is wrong.
67-
Zipper::open($emptyArchive)
67+
Zipper::write($emptyArchive)
6868
->addFromString('empty.txt', 'empty')
6969
->encrypt('not-the-password-we-decrypt-with')
7070
->close();

tests/Unit/BackuperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
$backup = Backuper::backup();
2828

2929
$unzipped = config('backup.temp_path') . '/unzipped';
30-
Zipper::open(Storage::disk(config('backup.destination.disk'))->path($backup->path), true)->extractTo(
30+
Zipper::read(Storage::disk(config('backup.destination.disk'))->path($backup->path))->extractTo(
3131
$unzipped,
3232
config('backup.password'),
3333
);

tests/Unit/PipeTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
test('backup pipes can pass zipper instance', function (string $pipe): void {
1616
$temp_zip = config('backup.temp_path') . '/backup.zip';
1717

18-
$zipper = Zipper::open($temp_zip);
18+
$zipper = Zipper::write($temp_zip);
1919
expect(app()
2020
->make($pipe)
2121
->backup($zipper, fn(Zipper $z): Zipper => $z))->toBeInstanceOf(Zipper::class);
@@ -58,7 +58,7 @@
5858

5959
File::deleteDirectory(Stache::store('users')->directory());
6060

61-
$zipper = Zipper::open(config('backup.temp_path') . '/backup.zip');
61+
$zipper = Zipper::write(config('backup.temp_path') . '/backup.zip');
6262

6363
$pipe->backup(zip: $zipper, next: $callable);
6464

@@ -79,7 +79,7 @@
7979
File::copyDirectory(config('backup.content_path'), config('backup.content_path') . '_backup');
8080
File::deleteDirectory(config('backup.content_path'));
8181

82-
$zipper = Zipper::open(config('backup.temp_path') . '/backup.zip');
82+
$zipper = Zipper::write(config('backup.temp_path') . '/backup.zip');
8383

8484
$pipe->backup(zip: $zipper, next: $callable);
8585

tests/Unit/ZipperTest.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
it('can zip file from string', function (): void {
2222
$target = storage_path('test.zip');
2323

24-
Zipper::open($target)
24+
Zipper::write($target)
2525
->addFromString('test.txt', 'test')
2626
->close();
2727

@@ -33,7 +33,7 @@
3333
it('can zip file from path', function (): void {
3434
$target = storage_path('test.zip');
3535

36-
Zipper::open($target)
36+
Zipper::write($target)
3737
->addFile(__FILE__)
3838
->close();
3939

@@ -45,7 +45,7 @@
4545
it('can zip directory', function (): void {
4646
$path = storage_path('test.zip');
4747

48-
Zipper::open($path)->addDirectory(config('backup.content_path'), 'example');
48+
Zipper::write($path)->addDirectory(config('backup.content_path'), 'example');
4949

5050
expect($path)->toBeString();
5151
expect(file_exists($path))->toBeTrue();
@@ -54,13 +54,13 @@
5454
it('can unzip file', function (): void {
5555
$target = storage_path('test.zip');
5656

57-
Zipper::open($target)
57+
Zipper::write($target)
5858
->addFromString('test.txt', 'test')
5959
->close();
6060

6161
$unzip = storage_path('unzip');
6262

63-
Zipper::open($target, true)
63+
Zipper::read($target)
6464
->extractTo($unzip)
6565
->close();
6666

@@ -70,10 +70,12 @@
7070
it('can unzip file to directory', function (): void {
7171
$target = storage_path('test.zip');
7272

73-
Zipper::open($target)->addFromString('test.txt', 'test');
73+
Zipper::write($target)
74+
->addFromString('test.txt', 'test')
75+
->close();
7476

7577
$unzip = storage_path('test');
76-
Zipper::open($target, true)
78+
Zipper::read($target)
7779
->extractTo($unzip)
7880
->close();
7981

@@ -88,10 +90,12 @@
8890

8991
$target = storage_path('test.zip');
9092

91-
Zipper::open($target)->addDirectory(config('backup.content_path'), 'example');
93+
Zipper::write($target)
94+
->addDirectory(config('backup.content_path'), 'example')
95+
->close();
9296

9397
$unzip = storage_path('unzipdir');
94-
Zipper::open($target, true)
98+
Zipper::read($target)
9599
->extractTo($unzip)
96100
->close();
97101

@@ -108,7 +112,7 @@
108112
// @mago-ignore security/no-literal-password
109113
$password = 'password';
110114

111-
Zipper::open($target)
115+
Zipper::write($target)
112116
->addFromString('test.txt', 'test')
113117
->encrypt($password)
114118
->close();
@@ -118,7 +122,7 @@
118122

119123
$unzip = storage_path('unzip');
120124

121-
Zipper::open($target, true)
125+
Zipper::read($target)
122126
->extractTo($unzip, $password)
123127
->close();
124128

@@ -129,12 +133,12 @@
129133
it('can write meta to zip', function (): void {
130134
$target = storage_path('test.zip');
131135

132-
Zipper::open($target)
136+
Zipper::write($target)
133137
->addFromString('test.txt', 'test')
134138
->addMeta('test', 'test')
135139
->close();
136140

137-
$zip = Zipper::open($target, true);
141+
$zip = Zipper::read($target);
138142

139143
expect($zip->getMeta())->toHaveKey('test');
140144
expect($zip->getMeta())

0 commit comments

Comments
 (0)