Completed
Push — master ( b27dc5...9f6466 )
by Freek
01:34
created

BackupDestination::getDiskOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
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(
86
            $destination,
87
            $handle,
88
            $this->getDiskOptions()
89
        );
90
91
        if (is_resource($handle)) {
92
            fclose($handle);
93
        }
94
    }
95
96
    public function backupName(): string
97
    {
98
        return $this->backupName;
99
    }
100
101
    public function backups(): BackupCollection
102
    {
103
        if ($this->backupCollectionCache) {
104
            return $this->backupCollectionCache;
105
        }
106
107
        $files = is_null($this->disk) ? [] : $this->disk->allFiles($this->backupName);
108
109
        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...
110
            $this->disk,
111
            $files
112
        );
113
    }
114
115
    public function connectionError(): Exception
116
    {
117
        return $this->connectionError;
118
    }
119
120
    public function getDiskOptions(): array
121
    {
122
        return config("filesystems.disks.{$this->diskName()}.backup_options") ?? [];
123
    }
124
125
    public function isReachable(): bool
126
    {
127
        if (is_null($this->disk)) {
128
            return false;
129
        }
130
131
        try {
132
            $this->disk->allFiles($this->backupName);
133
134
            return true;
135
        } catch (Exception $exception) {
136
            $this->connectionError = $exception;
137
138
            return false;
139
        }
140
    }
141
142
    public function usedStorage(): int
143
    {
144
        return $this->backups()->size();
145
    }
146
147
    public function newestBackup(): ?Backup
148
    {
149
        return $this->backups()->newest();
150
    }
151
152
    public function oldestBackup(): ?Backup
153
    {
154
        return $this->backups()->oldest();
155
    }
156
157
    public function newestBackupIsOlderThan(Carbon $date): bool
158
    {
159
        $newestBackup = $this->newestBackup();
160
161
        if (is_null($newestBackup)) {
162
            return true;
163
        }
164
165
        return $newestBackup->date()->gt($date);
166
    }
167
168
    public function fresh(): self
169
    {
170
        $this->backupCollectionCache = null;
171
172
        return $this;
173
    }
174
}
175