Completed
Push — master ( fc5aa9...0aaed0 )
by Freek
12s
created

BackupDestination::fresh()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Backup\BackupDestination;
4
5
use Exception;
6
use Carbon\Carbon;
7
use Illuminate\Contracts\Filesystem\Factory;
8
use Illuminate\Contracts\Filesystem\Filesystem;
9
use Spatie\Backup\Exceptions\InvalidBackupDestination;
10
11
class BackupDestination
12
{
13
    /** @var \Illuminate\Contracts\Filesystem\Filesystem */
14
    protected $disk;
15
16
    /** @var string */
17
    protected $diskName;
18
19
    /** @var string */
20
    protected $backupName;
21
22
    /** @var Exception */
23
    public $connectionError;
24
25
    /** @var null|\Spatie\Backup\BackupDestination\BackupCollection */
26
    protected $backupCollectionCache = null;
27
28
    public function __construct(Filesystem $disk = null, string $backupName, string $diskName)
29
    {
30
        $this->disk = $disk;
31
32
        $this->diskName = $diskName;
33
34
        $this->backupName = preg_replace('/[^a-zA-Z0-9.]/', '-', $backupName);
35
    }
36
37
    public function disk(): Filesystem
38
    {
39
        return $this->disk;
40
    }
41
42
    public function diskName(): string
43
    {
44
        return $this->diskName;
45
    }
46
47
    public function filesystemType(): string
48
    {
49
        if (is_null($this->disk)) {
50
            return 'unknown';
51
        }
52
53
        $adapterClass = get_class($this->disk->getDriver()->getAdapter());
54
55
        $filesystemType = last(explode('\\', $adapterClass));
56
57
        return strtolower($filesystemType);
58
    }
59
60
    public static function create(string $diskName, string $backupName): self
61
    {
62
        try {
63
            $disk = app(Factory::class)->disk($diskName);
64
65
            return new static($disk, $backupName, $diskName);
66
        } catch (Exception $exception) {
67
            $backupDestination = new static(null, $backupName, $diskName);
68
69
            $backupDestination->connectionError = $exception;
70
71
            return $backupDestination;
72
        }
73
    }
74
75
    public function write(string $file)
76
    {
77
        if (is_null($this->disk)) {
78
            throw InvalidBackupDestination::diskNotSet();
79
        }
80
81
        $destination = $this->backupName.'/'.pathinfo($file, PATHINFO_BASENAME);
82
83
        $handle = fopen($file, 'r+');
84
85
        $this->disk->getDriver()->writeStream($destination, $handle);
86
87
        if (is_resource($handle)) {
88
            fclose($handle);
89
        }
90
    }
91
92
    public function backupName(): string
93
    {
94
        return $this->backupName;
95
    }
96
97
    public function backups(): BackupCollection
98
    {
99
        if ($this->backupCollectionCache) {
100
            return $this->backupCollectionCache;
101
        }
102
103
        $files = is_null($this->disk) ? [] : $this->disk->allFiles($this->backupName);
104
105
        return $this->backupCollectionCache = BackupCollection::createFromFiles(
0 ignored issues
show
Documentation Bug introduced by
It seems like \Spatie\Backup\BackupDes...es($this->disk, $files) of type object<self> is incompatible with the declared type null|object<Spatie\Backu...ation\BackupCollection> of property $backupCollectionCache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
106
            $this->disk,
107
            $files
108
        );
109
    }
110
111
    public function connectionError(): Exception
112
    {
113
        return $this->connectionError;
114
    }
115
116
    public function isReachable(): bool
117
    {
118
        if (is_null($this->disk)) {
119
            return false;
120
        }
121
122
        try {
123
            $this->disk->allFiles($this->backupName);
124
125
            return true;
126
        } catch (Exception $exception) {
127
            $this->connectionError = $exception;
128
129
            return false;
130
        }
131
    }
132
133
    public function usedStorage(): int
134
    {
135
        return $this->backups()->size();
136
    }
137
138
    public function newestBackup(): ?Backup
139
    {
140
        return $this->backups()->newest();
141
    }
142
143
    public function oldestBackup(): ?Backup
144
    {
145
        return $this->backups()->oldest();
146
    }
147
148
    public function newestBackupIsOlderThan(Carbon $date): bool
149
    {
150
        $newestBackup = $this->newestBackup();
151
152
        if (is_null($newestBackup)) {
153
            return true;
154
        }
155
156
        return $newestBackup->date()->gt($date);
157
    }
158
159
    public function fresh(): self
160
    {
161
        $this->backupCollectionCache = null;
162
163
        return $this;
164
    }
165
}
166