Completed
Push — master ( 99577b...901a4d )
by Freek
03:36
created

Snapshot::getTableDropper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Spatie\DbSnapshots;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\DB;
7
use Spatie\DbSnapshots\Events\LoadedSnapshot;
8
use Spatie\DbSnapshots\Events\DeletedSnapshot;
9
use Spatie\DbSnapshots\Events\LoadingSnapshot;
10
use Spatie\DbSnapshots\Events\DeletingSnapshot;
11
use Spatie\MigrateFresh\TableDropperFactory;
12
use Spatie\MigrateFresh\TableDroppers\TableDropper;
13
use Illuminate\Filesystem\FilesystemAdapter as Disk;
14
15
class Snapshot
16
{
17
    /** @var \Illuminate\Filesystem\FilesystemAdapter */
18
    public $disk;
19
20
    /** @var string */
21
    public $fileName;
22
23
    /** @var string */
24
    public $name;
25
26
    public function __construct(Disk $disk, string $fileName)
27
    {
28
        $this->disk = $disk;
29
30
        $this->fileName = $fileName;
31
32
        $this->name = pathinfo($fileName, PATHINFO_FILENAME);
33
    }
34
35
    public function load()
36
    {
37
        event(new LoadingSnapshot($this));
38
39
        $this->dropAllCurrentTables();
40
41
        $dbDumpContents = $this->disk->get($this->fileName);
42
43
        foreach (explode(PHP_EOL, $dbDumpContents) as $statement) {
44
            DB::statement($statement);
45
        }
46
47
        event(new LoadedSnapshot($this));
48
    }
49
50
    public function delete()
51
    {
52
        event(new DeletingSnapshot($this));
53
54
        $this->disk->delete($this->fileName);
55
56
        event(new DeletedSnapshot($this->fileName, $this->disk));
57
    }
58
59
    public function size(): int
60
    {
61
        return $this->disk->size($this->fileName);
62
    }
63
64
    public function createdAt(): Carbon
65
    {
66
        return Carbon::createFromTimestamp($this->disk->lastModified($this->fileName));
0 ignored issues
show
Documentation introduced by
$this->disk->lastModified($this->fileName) is of type string|false, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
    }
68
69
    protected function dropAllCurrentTables()
70
    {
71
        $tableDropper = TableDropperFactory::create(DB::getDriverName());
72
73
        $tableDropper->dropAllTables();
74
75
        DB::reconnect();
76
    }
77
}
78