1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\PersonalDataExport; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Spatie\TemporaryDirectory\TemporaryDirectory; |
7
|
|
|
use ZipArchive; |
8
|
|
|
|
9
|
|
|
class Zip |
10
|
|
|
{ |
11
|
|
|
/** @var \ZipArchive */ |
12
|
|
|
protected $zipFile; |
13
|
|
|
|
14
|
|
|
/** @var int */ |
15
|
|
|
protected $fileCount = 0; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
protected $pathToZip; |
19
|
|
|
|
20
|
|
|
public static function createForPersonalData( |
21
|
|
|
PersonalDataSelection $personalDataSelection, |
22
|
|
|
TemporaryDirectory $temporaryDirectory): self |
23
|
|
|
{ |
24
|
|
|
$zipFilenameParts = [ |
25
|
|
|
$personalDataSelection->user->getKey(), |
26
|
|
|
now()->timestamp, |
27
|
|
|
Str::random(64), |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
$zipFilename = implode('_', $zipFilenameParts).'.zip'; |
31
|
|
|
|
32
|
|
|
$pathToZip = $temporaryDirectory->path($zipFilename); |
33
|
|
|
|
34
|
|
|
return (new static($pathToZip)) |
35
|
|
|
->add($personalDataSelection->files(), $temporaryDirectory->path()) |
36
|
|
|
->close(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function __construct(string $pathToZip) |
40
|
|
|
{ |
41
|
|
|
$this->zipFile = new ZipArchive(); |
42
|
|
|
|
43
|
|
|
$this->pathToZip = $pathToZip; |
44
|
|
|
|
45
|
|
|
$this->open(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function path(): string |
49
|
|
|
{ |
50
|
|
|
return $this->pathToZip; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function size(): int |
54
|
|
|
{ |
55
|
|
|
if ($this->fileCount === 0) { |
56
|
|
|
return 0; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return filesize($this->pathToZip); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function open(): self |
63
|
|
|
{ |
64
|
|
|
$this->zipFile->open($this->pathToZip, ZipArchive::CREATE); |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string|array $files |
71
|
|
|
* @param string $rootPath |
72
|
|
|
* |
73
|
|
|
* @return \Spatie\PersonalDataExport\Zip |
74
|
|
|
*/ |
75
|
|
|
public function add($files, $rootPath): self |
76
|
|
|
{ |
77
|
|
|
foreach ($files as $file) { |
|
|
|
|
78
|
|
|
if (file_exists($file)) { |
79
|
|
|
$nameInZip = Str::after($file, $rootPath.'/'); |
80
|
|
|
|
81
|
|
|
$this->zipFile->addFile($file, ltrim($nameInZip, DIRECTORY_SEPARATOR)); |
82
|
|
|
} |
83
|
|
|
$this->fileCount++; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function close(): self |
90
|
|
|
{ |
91
|
|
|
$this->zipFile->close(); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.