1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\PersonalDataExport; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
use Spatie\PersonalDataExport\Exceptions\CouldNotAddToPersonalDataSelection; |
8
|
|
|
use Spatie\TemporaryDirectory\TemporaryDirectory; |
9
|
|
|
|
10
|
|
|
class PersonalDataSelection |
11
|
|
|
{ |
12
|
|
|
/** @var \Spatie\TemporaryDirectory\TemporaryDirectory */ |
13
|
|
|
protected $temporaryDirectory; |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
protected $files = []; |
17
|
|
|
|
18
|
|
|
/** @var \Illuminate\Database\Eloquent\Model */ |
19
|
|
|
public $user; |
20
|
|
|
|
21
|
|
|
public function __construct(TemporaryDirectory $temporaryDirectory) |
22
|
|
|
{ |
23
|
|
|
$this->temporaryDirectory = $temporaryDirectory; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function files(): array |
27
|
|
|
{ |
28
|
|
|
return $this->files; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function forUser(ExportsPersonalData $user) |
32
|
|
|
{ |
33
|
|
|
$this->user = $user; |
|
|
|
|
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $nameInDownload |
40
|
|
|
* @param array|string $content |
41
|
|
|
* |
42
|
|
|
* @return \Spatie\PersonalDataExport\PersonalDataSelection |
43
|
|
|
*/ |
44
|
|
|
public function add(string $nameInDownload, $content) |
45
|
|
|
{ |
46
|
|
|
if (! is_string($content)) { |
47
|
|
|
$content = json_encode($content, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$path = $this->temporaryDirectory->path($nameInDownload); |
51
|
|
|
|
52
|
|
|
$this->ensureDoesNotOverwriteExistingFile($path); |
53
|
|
|
|
54
|
|
|
$this->files[] = $path; |
55
|
|
|
|
56
|
|
|
file_put_contents($path, $content); |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function addFile(string $pathToFile, string $diskName = null) |
62
|
|
|
{ |
63
|
|
|
return is_null($diskName) |
64
|
|
|
? $this->copyLocalFile($pathToFile) |
65
|
|
|
: $this->copyFileFromDisk($pathToFile, $diskName); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function copyLocalFile(string $pathToFile) |
69
|
|
|
{ |
70
|
|
|
$fileName = pathinfo($pathToFile, PATHINFO_BASENAME); |
71
|
|
|
|
72
|
|
|
$destination = $this->temporaryDirectory->path($fileName); |
73
|
|
|
|
74
|
|
|
$this->ensureDoesNotOverwriteExistingFile($destination); |
75
|
|
|
|
76
|
|
|
(new Filesystem())->copy($pathToFile, $destination); |
77
|
|
|
|
78
|
|
|
$this->files[] = $destination; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function copyFileFromDisk(string $pathOnDisk, string $diskName) |
84
|
|
|
{ |
85
|
|
|
$stream = Storage::disk($diskName)->readStream($pathOnDisk); |
86
|
|
|
|
87
|
|
|
$pathInTemporaryDirectory = $this->temporaryDirectory->path($pathOnDisk); |
88
|
|
|
|
89
|
|
|
$this->ensureDoesNotOverwriteExistingFile($pathInTemporaryDirectory); |
90
|
|
|
|
91
|
|
|
file_put_contents($pathInTemporaryDirectory, stream_get_contents($stream), FILE_APPEND); |
92
|
|
|
|
93
|
|
|
$this->files[] = $pathInTemporaryDirectory; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function ensureDoesNotOverwriteExistingFile(string $path) |
99
|
|
|
{ |
100
|
|
|
if (file_exists($path)) { |
101
|
|
|
throw CouldNotAddToPersonalDataSelection::fileAlreadyAddedToPersonalDataSelection($path); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.