Completed
Push — master ( f6a9d8...d6d550 )
by Freek
02:15
created

writeFilesFromManifestWithoutCreatingZipLocally()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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