1 | <?php |
||||
2 | |||||
3 | namespace Spatie\DbSnapshots; |
||||
4 | |||||
5 | use Illuminate\Contracts\Filesystem\Factory; |
||||
6 | use Illuminate\Filesystem\FilesystemAdapter; |
||||
7 | use Spatie\DbDumper\Compressors\GzipCompressor; |
||||
8 | use Spatie\DbDumper\DbDumper; |
||||
9 | use Spatie\DbSnapshots\Events\CreatedSnapshot; |
||||
10 | use Spatie\DbSnapshots\Events\CreatingSnapshot; |
||||
11 | use Spatie\DbSnapshots\Exceptions\CannotCreateDisk; |
||||
12 | use Spatie\TemporaryDirectory\TemporaryDirectory; |
||||
13 | |||||
14 | class SnapshotFactory |
||||
15 | { |
||||
16 | protected DbDumperFactory $dumperFactory; |
||||
17 | |||||
18 | protected Factory $filesystemFactory; |
||||
19 | |||||
20 | public function __construct(DbDumperFactory $dumperFactory, Factory $filesystemFactory) |
||||
21 | { |
||||
22 | $this->dumperFactory = $dumperFactory; |
||||
23 | |||||
24 | $this->filesystemFactory = $filesystemFactory; |
||||
25 | } |
||||
26 | |||||
27 | public function create(string $snapshotName, string $diskName, string $connectionName, bool $compress = false): Snapshot |
||||
28 | { |
||||
29 | $disk = $this->getDisk($diskName); |
||||
30 | |||||
31 | $fileName = $snapshotName.'.sql'; |
||||
32 | $fileName = pathinfo($fileName, PATHINFO_BASENAME); |
||||
33 | |||||
34 | if ($compress) { |
||||
35 | $fileName .= '.gz'; |
||||
36 | } |
||||
37 | |||||
38 | event(new CreatingSnapshot( |
||||
39 | $fileName, |
||||
40 | $disk, |
||||
41 | $connectionName |
||||
42 | )); |
||||
43 | |||||
44 | $this->createDump($connectionName, $fileName, $disk, $compress); |
||||
45 | |||||
46 | $snapshot = new Snapshot($disk, $fileName); |
||||
47 | |||||
48 | event(new CreatedSnapshot($snapshot)); |
||||
49 | |||||
50 | return $snapshot; |
||||
51 | } |
||||
52 | |||||
53 | protected function getDisk(string $diskName): FilesystemAdapter |
||||
54 | { |
||||
55 | if (is_null(config("filesystems.disks.{$diskName}"))) { |
||||
56 | throw CannotCreateDisk::diskNotDefined($diskName); |
||||
57 | } |
||||
58 | |||||
59 | return $this->filesystemFactory->disk($diskName); |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
60 | } |
||||
61 | |||||
62 | protected function getDbDumper(string $connectionName): DbDumper |
||||
63 | { |
||||
64 | $factory = $this->dumperFactory; |
||||
65 | |||||
66 | return $factory::createForConnection($connectionName); |
||||
67 | } |
||||
68 | |||||
69 | protected function createDump(string $connectionName, string $fileName, FilesystemAdapter $disk, bool $compress = false) |
||||
70 | { |
||||
71 | $directory = (new TemporaryDirectory(config('db-snapshots.temporary_directory_path')))->create(); |
||||
72 | |||||
73 | $dumpPath = $directory->path($fileName); |
||||
74 | |||||
75 | $dbDumper = $this->getDbDumper($connectionName); |
||||
76 | |||||
77 | if ($compress) { |
||||
78 | $dbDumper->useCompressor(new GzipCompressor()); |
||||
79 | } |
||||
80 | |||||
81 | $dbDumper->dumpToFile($dumpPath); |
||||
82 | |||||
83 | $file = fopen($dumpPath, 'r'); |
||||
84 | |||||
85 | $disk->put($fileName, $file); |
||||
0 ignored issues
–
show
It seems like
$file can also be of type false ; however, parameter $contents of Illuminate\Filesystem\FilesystemAdapter::put() does only seem to accept resource|string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
86 | |||||
87 | if (is_resource($file)) { |
||||
88 | fclose($file); |
||||
89 | } |
||||
90 | |||||
91 | gc_collect_cycles(); |
||||
92 | |||||
93 | $directory->delete(); |
||||
94 | } |
||||
95 | } |
||||
96 |