Completed
Push — master ( fd92ed...e2f694 )
by Freek
10:21
created

SnapshotFactory::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 2
nc 2
nop 4
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;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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);
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);
86
87
        if (is_resource($file)) {
88
            fclose($file);
89
        }
90
91
        gc_collect_cycles();
92
93
        $directory->delete();
94
    }
95
}
96