Passed
Push — master ( 3f1f2e...c46818 )
by
unknown
08:52
created

Snapshot   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 9
eloc 30
c 6
b 2
f 0
dl 0
loc 73
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 19 3
A __construct() 0 14 2
A size() 0 3 1
A delete() 0 7 1
A dropAllCurrentTables() 0 7 1
A createdAt() 0 3 1
1
<?php
2
3
namespace Spatie\DbSnapshots;
4
5
use Carbon\Carbon;
6
use Illuminate\Filesystem\FilesystemAdapter as Disk;
7
use Illuminate\Support\Facades\DB;
8
use Spatie\DbSnapshots\Events\DeletedSnapshot;
9
use Spatie\DbSnapshots\Events\DeletingSnapshot;
10
use Spatie\DbSnapshots\Events\LoadedSnapshot;
11
use Spatie\DbSnapshots\Events\LoadingSnapshot;
12
13
class Snapshot
14
{
15
    public Disk $disk;
16
17
    public string $fileName;
18
19
    public string $name;
20
21
    public ?string $compressionExtension = null;
22
23
    public function __construct(Disk $disk, string $fileName)
24
    {
25
        $this->disk = $disk;
26
27
        $this->fileName = $fileName;
28
29
        $pathinfo = pathinfo($fileName);
30
31
        if ($pathinfo['extension'] === 'gz') {
32
            $this->compressionExtension = $pathinfo['extension'];
33
            $fileName = $pathinfo['filename'];
34
        }
35
36
        $this->name = pathinfo($fileName, PATHINFO_FILENAME);
37
    }
38
39
    public function load(string $connectionName = null)
40
    {
41
        event(new LoadingSnapshot($this));
42
43
        if ($connectionName !== null) {
44
            DB::setDefaultConnection($connectionName);
45
        }
46
47
        $this->dropAllCurrentTables();
48
49
        $dbDumpContents = $this->disk->get($this->fileName);
50
51
        if ($this->compressionExtension === 'gz') {
52
            $dbDumpContents = gzdecode($dbDumpContents);
53
        }
54
55
        DB::connection($connectionName)->unprepared($dbDumpContents);
56
57
        event(new LoadedSnapshot($this));
58
    }
59
60
    public function delete()
61
    {
62
        event(new DeletingSnapshot($this));
63
64
        $this->disk->delete($this->fileName);
65
66
        event(new DeletedSnapshot($this->fileName, $this->disk));
67
    }
68
69
    public function size(): int
70
    {
71
        return $this->disk->size($this->fileName);
72
    }
73
74
    public function createdAt(): Carbon
75
    {
76
        return Carbon::createFromTimestamp($this->disk->lastModified($this->fileName));
0 ignored issues
show
Bug Best Practice introduced by
The expression return Carbon\Carbon::cr...ified($this->fileName)) could return the type string which is incompatible with the type-hinted return Carbon\Carbon. Consider adding an additional type-check to rule them out.
Loading history...
77
    }
78
79
    protected function dropAllCurrentTables()
80
    {
81
        DB::connection(DB::getDefaultConnection())
82
            ->getSchemaBuilder()
83
            ->dropAllTables();
84
85
        DB::reconnect();
86
    }
87
}
88