|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\PersonalDataExport\Jobs; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
|
6
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
|
7
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
|
8
|
|
|
use Illuminate\Queue\SerializesModels; |
|
9
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
|
10
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
11
|
|
|
use Illuminate\Support\Facades\Mail; |
|
12
|
|
|
use Illuminate\Support\Facades\Storage; |
|
13
|
|
|
use Spatie\PersonalDataExport\Events\PersonalDataExportCreated; |
|
14
|
|
|
use Spatie\PersonalDataExport\Events\PersonalDataSelected; |
|
15
|
|
|
use Spatie\PersonalDataExport\Exceptions\InvalidUser; |
|
16
|
|
|
use Spatie\PersonalDataExport\ExportsPersonalData; |
|
17
|
|
|
use Spatie\PersonalDataExport\PersonalDataSelection; |
|
18
|
|
|
use Spatie\PersonalDataExport\Zip; |
|
19
|
|
|
use Spatie\TemporaryDirectory\TemporaryDirectory; |
|
20
|
|
|
|
|
21
|
|
|
class CreatePersonalDataExportJob implements ShouldQueue |
|
22
|
|
|
{ |
|
23
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
|
24
|
|
|
|
|
25
|
|
|
/** @var \Spatie\PersonalDataExport\ExportsPersonalData */ |
|
26
|
|
|
protected $user; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(ExportsPersonalData $user) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->ensureValidUser($user); |
|
31
|
|
|
|
|
32
|
|
|
$this->user = $user; |
|
33
|
|
|
|
|
34
|
|
|
$this->queue = config('personal-data-export.job.queue'); |
|
35
|
|
|
|
|
36
|
|
|
$this->connection = config('personal-data-export.job.connection'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function handle() |
|
40
|
|
|
{ |
|
41
|
|
|
$temporaryDirectory = (new TemporaryDirectory())->create(); |
|
42
|
|
|
|
|
43
|
|
|
$personalDataSelection = $this->selectPersonalData($temporaryDirectory); |
|
44
|
|
|
|
|
45
|
|
|
event(new PersonalDataSelected($personalDataSelection, $this->user)); |
|
46
|
|
|
|
|
47
|
|
|
$zipFilename = $this->zipPersonalData($personalDataSelection, $this->getDisk(), $temporaryDirectory); |
|
48
|
|
|
|
|
49
|
|
|
$temporaryDirectory->delete(); |
|
50
|
|
|
|
|
51
|
|
|
event(new PersonalDataExportCreated($zipFilename, $this->user)); |
|
52
|
|
|
|
|
53
|
|
|
$this->mailZip($zipFilename); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function selectPersonalData(TemporaryDirectory $temporaryDirectory): PersonalDataSelection |
|
57
|
|
|
{ |
|
58
|
|
|
$personalData = (new PersonalDataSelection($temporaryDirectory))->forUser($this->user); |
|
59
|
|
|
|
|
60
|
|
|
$this->user->selectPersonalData($personalData); |
|
61
|
|
|
|
|
62
|
|
|
return $personalData; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function zipPersonalData( |
|
66
|
|
|
PersonalDataSelection $personalData, |
|
67
|
|
|
Filesystem $filesystem, |
|
68
|
|
|
TemporaryDirectory $temporaryDirectory |
|
69
|
|
|
): string { |
|
70
|
|
|
$zip = Zip::createForPersonalData($personalData, $temporaryDirectory); |
|
71
|
|
|
|
|
72
|
|
|
$zipFilename = pathinfo($zip->path(), PATHINFO_BASENAME); |
|
73
|
|
|
|
|
74
|
|
|
$filesystem->writeStream($zipFilename, fopen($zip->path(), 'r')); |
|
75
|
|
|
|
|
76
|
|
|
return $zipFilename; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getDisk(): Filesystem |
|
80
|
|
|
{ |
|
81
|
|
|
return Storage::disk(config('personal-data-export.disk')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function mailZip(string $zipFilename) |
|
85
|
|
|
{ |
|
86
|
|
|
$mailableClass = config('personal-data-export.mailable'); |
|
87
|
|
|
|
|
88
|
|
|
Mail::to($this->user)->send(new $mailableClass($zipFilename)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function ensureValidUser(ExportsPersonalData $user) |
|
92
|
|
|
{ |
|
93
|
|
|
if (is_null($user->email ?? null)) { |
|
|
|
|
|
|
94
|
|
|
throw InvalidUser::doesNotHaveAnEmailProperty($user); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: