Completed
Push — master ( f62f21...f72c97 )
by Freek
05:09
created

BackupJob::copyToBackupDestinations()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 8.8571
cc 3
eloc 14
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Backup\Tasks\Backup;
4
5
use Illuminate\Support\Collection;
6
use Spatie\Backup\BackupDestination\BackupDestination;
7
use Spatie\Backup\Events\BackupHasFailed;
8
use Spatie\Backup\Events\BackupWasSuccessful;
9
use Spatie\Backup\Events\BackupZipWasCreated;
10
use Spatie\Backup\Helpers\Format;
11
use Spatie\DbDumper\DbDumper;
12
use Exception;
13
14
class BackupJob
15
{
16
    /**  @var \Spatie\Backup\Tasks\Backup\FileSelection */
17
    protected $fileSelection;
18
19
    /** @var \Illuminate\Support\Collection */
20
    protected $dbDumpers;
21
22
    /** @var \Illuminate\Support\Collection */
23
    protected $backupDestinations;
24
25
    /** @var \Spatie\Backup\Tasks\Backup\TemporaryDirectory */
26
    protected $temporaryDirectory;
27
28
    public function __construct()
29
    {
30
        $this->doNotBackupFilesystem();
31
        $this->doNotBackupDatabases();
32
33
        $this->backupDestinations = new Collection();
34
    }
35
36
    /**
37
     * @return \Spatie\Backup\Tasks\Backup\BackupJob
38
     */
39
    public function doNotBackupFilesystem()
40
    {
41
        $this->fileSelection = FileSelectionFactory::noFiles();
42
43
        return $this;
44
    }
45
46
    /**
47
     * @return \Spatie\Backup\Tasks\Backup\BackupJob
48
     */
49
    public function doNotBackupDatabases()
50
    {
51
        $this->dbDumpers = new Collection();
52
53
        return $this;
54
    }
55
56
    /**
57
     * @param \Spatie\Backup\Tasks\Backup\FileSelection $fileSelection
58
     *
59
     * @return \Spatie\Backup\Tasks\Backup\BackupJob
60
     */
61
    public function setFileSelection(FileSelection $fileSelection)
62
    {
63
        $this->fileSelection = $fileSelection;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param array $dbDumpers
70
     *
71
     * @return \Spatie\Backup\Tasks\Backup\BackupJob
72
     */
73
    public function setDbDumpers(array $dbDumpers)
74
    {
75
        $this->dbDumpers = Collection::make($dbDumpers);
76
77
        return $this;
78
    }
79
80
    /**
81
     * @param \Illuminate\Support\Collection $backupDestinations
82
     *
83
     * @return \Spatie\Backup\Tasks\Backup\BackupJob
84
     */
85
    public function setBackupDestinations(Collection $backupDestinations)
86
    {
87
        $this->backupDestinations = $backupDestinations;
88
89
        return $this;
90
    }
91
92
    public function run()
93
    {
94
        try {
95
            $this->temporaryDirectory = TemporaryDirectory::create();
96
97
            $zip = $this->createZipContainingAllFilesToBeBackedUp();
98
99
            $this->copyToBackupDestinations($zip);
100
101
            $this->temporaryDirectory->delete();
102
        } catch (Exception $exception) {
103
            consoleOutput()->error("Backup failed because {$exception->getMessage()}");
0 ignored issues
show
Documentation Bug introduced by
The method error does not exist on object<Spatie\Backup\Helpers\ConsoleOutput>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
104
105
            event(new BackupHasFailed($exception));
106
        }
107
    }
108
109
    /**
110
     * @return \Spatie\Backup\Tasks\Backup\Zip
111
     */
112
    protected function createZipContainingAllFilesToBeBackedUp()
113
    {
114
        $zip = Zip::create($this->temporaryDirectory->getPath(date('Y-m-d-His').'.zip'));
115
116
        $this->addDatabaseDumpsToZip($zip);
117
118
        $this->addSelectedFilesToZip($zip);
119
120
        event(new BackupZipWasCreated($zip));
121
122
        return $zip;
123
    }
124
125
    /**
126
     * @param \Spatie\Backup\Tasks\Backup\Zip $zip
127
     */
128
    protected function addSelectedFilesToZip(Zip $zip)
129
    {
130
        consoleOutput()->info('Determining files to backup...');
0 ignored issues
show
Documentation Bug introduced by
The method info does not exist on object<Spatie\Backup\Helpers\ConsoleOutput>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
131
132
        $files = $this->fileSelection->getSelectedFiles();
133
134
        consoleOutput()->info('Zipping '.count($files).' files...');
0 ignored issues
show
Documentation Bug introduced by
The method info does not exist on object<Spatie\Backup\Helpers\ConsoleOutput>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
135
136
        $zip->add($files);
137
    }
138
139
    /**
140
     * @param \Spatie\Backup\Tasks\Backup\Zip $zip
141
     */
142
    protected function addDatabaseDumpsToZip(Zip $zip)
143
    {
144
        $this->dbDumpers->each(function (DbDumper $dbDumper) use ($zip) {
145
146
            consoleOutput()->info("Dumping database {$dbDumper->getDbName()}...");
0 ignored issues
show
Documentation Bug introduced by
The method info does not exist on object<Spatie\Backup\Helpers\ConsoleOutput>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
147
148
            $fileName = $dbDumper->getDbName().'.sql';
149
            $temporaryFile = $this->temporaryDirectory->getPath($fileName);
150
            $dbDumper->dumpToFile($temporaryFile);
151
152
            consoleOutput()->info("Dumped database {$dbDumper->getDbName()}");
0 ignored issues
show
Documentation Bug introduced by
The method info does not exist on object<Spatie\Backup\Helpers\ConsoleOutput>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
153
154
            $zip->add($temporaryFile, $fileName);
155
        });
156
    }
157
158
    /**
159
     * @param \Spatie\Backup\Tasks\Backup\Zip $zip
160
     */
161
    protected function copyToBackupDestinations(Zip $zip)
162
    {
163
        $this->backupDestinations->each(function (BackupDestination $backupDestination) use ($zip) {
164
165
            try {
166
                if (!$backupDestination->isReachable()) {
167
                    throw new Exception("Could not connect to {$backupDestination->getFilesystemType()} because: {$backupDestination->getConnectionError()}");
168
                };
169
170
                $fileSize = Format::getHumanReadableSize($zip->getSize());
171
172
                $fileName = pathinfo($zip->getPath(), PATHINFO_BASENAME);
173
174
                consoleOutput()->info("Copying {$fileName} (size: {$fileSize}) to {$backupDestination->getFilesystemType()}-filesystem...");
0 ignored issues
show
Documentation Bug introduced by
The method info does not exist on object<Spatie\Backup\Helpers\ConsoleOutput>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
175
176
                $backupDestination->write($zip->getPath());
177
178
                consoleOutput()->info("Successfully copied zip to {$backupDestination->getFilesystemType()}-filesystem");
0 ignored issues
show
Documentation Bug introduced by
The method info does not exist on object<Spatie\Backup\Helpers\ConsoleOutput>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
179
180
                event(new BackupWasSuccessful($backupDestination));
181
            } catch (Exception $exception) {
182
                consoleOutput()->error("Copying zip-file failed because: {$exception->getMessage()}");
0 ignored issues
show
Documentation Bug introduced by
The method error does not exist on object<Spatie\Backup\Helpers\ConsoleOutput>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
183
184
                event(new BackupHasFailed($exception));
185
            }
186
        });
187
    }
188
}
189