Completed
Push — master ( 326b3e...061c53 )
by Freek
09:13
created

BackupDestination::usedStorage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
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 Carbon\Carbon;
6
use Illuminate\Contracts\Filesystem\Factory;
7
use Illuminate\Contracts\Filesystem\Filesystem;
8
use Exception;
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
    public function __construct(Filesystem $disk = null, string $backupName, string $diskName)
26
    {
27
        $this->disk = $disk;
28
29
        $this->diskName = $diskName;
30
31
        $this->backupName = preg_replace('/[^a-zA-Z0-9.]/', '-', $backupName);
32
    }
33
34
    public function disk(): Filesystem
35
    {
36
        return $this->disk;
37
    }
38
39
    public function diskName(): string
40
    {
41
        return $this->diskName;
42
    }
43
44
    public function filesystemType(): string
45
    {
46
        if (is_null($this->disk)) {
47
            return 'unknown';
48
        }
49
50
        $adapterClass = get_class($this->disk->getDriver()->getAdapter());
51
52
        $filesystemType = last(explode('\\', $adapterClass));
53
54
        return strtolower($filesystemType);
55
    }
56
57
    public static function create(string $diskName, string $backupName): BackupDestination
58
    {
59
        try {
60
            $disk = app(Factory::class)->disk($diskName);
61
62
            return new static($disk, $backupName, $diskName);
63
        } catch (Exception $exception) {
64
            $backupDestination = new static(null, $backupName, $diskName);
65
66
            $backupDestination->connectionError = $exception;
67
68
            return $backupDestination;
69
        }
70
    }
71
72
    public function write(string $file)
73
    {
74
        if (is_null($this->disk)) {
75
            throw InvalidBackupDestination::diskNotSet();
76
        }
77
78
        $destination = $this->backupName.'/'.pathinfo($file, PATHINFO_BASENAME);
79
80
        $handle = fopen($file, 'r+');
81
82
        $this->disk->getDriver()->writeStream($destination, $handle);
83
    }
84
85
    public function writeFilesFromManifestWithoutCreatingZipLocally(Manifest $manifest)
86
    {
87
        $destination = $this->backupName.'/'.'test'.date('Ymdhis').'.tar.gz';
88
89
        $stream = popen("cat {$manifest->getPath()} | zip @");
90
91
        $this->disk->getDriver()->writeStream($destination, $stream);
92
    }
93
94
    public function backupName(): string
95
    {
96
        return $this->backupName;
97
    }
98
99
    public function backups(): BackupCollection
100
    {
101
        $files = $this->isReachable() ? $this->disk->allFiles($this->backupName) : [];
102
103
        return BackupCollection::createFromFiles(
104
            $this->disk,
105
            $files
106
        );
107
    }
108
109
    public function connectionError(): Exception
110
    {
111
        return $this->connectionError;
112
    }
113
114
    public function isReachable(): bool
115
    {
116
        if (is_null($this->disk)) {
117
            return false;
118
        }
119
120
        try {
121
            $this->disk->allFiles($this->backupName);
122
123
            return true;
124
        } catch (Exception $exception) {
125
            $this->connectionError = $exception;
126
127
            return false;
128
        }
129
    }
130
131
    public function usedStorage(): int
132
    {
133
        return $this->backups()->size();
134
    }
135
136
    /**
137
     * @return \Spatie\Backup\BackupDestination\Backup|null
138
     */
139
    public function newestBackup()
140
    {
141
        return $this->backups()->newest();
142
    }
143
144
    /**
145
     * @return \Spatie\Backup\BackupDestination\Backup|null
146
     */
147
    public function oldestBackup()
148
    {
149
        return $this->backups()->oldest();
150
    }
151
152
    public function newestBackupIsOlderThan(Carbon $date): bool
153
    {
154
        $newestBackup = $this->newestBackup();
155
156
        if (is_null($newestBackup)) {
157
            return true;
158
        }
159
160
        return $newestBackup->date()->gt($date);
161
    }
162
}
163