Completed
Pull Request — master (#189)
by Barry vd.
02:21
created

BackupJob   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 8
Bugs 0 Features 1
Metric Value
c 8
b 0
f 1
dl 0
loc 191
rs 10
wmc 16
lcom 1
cbo 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setDbDumpers() 0 6 1
A backupOnlyTo() 0 12 2
A run() 0 20 3
A createZipContainingAllFilesToBeBackedUp() 0 12 1
A __construct() 0 7 1
A doNotBackupFilesystem() 0 6 1
A doNotBackupDatabases() 0 6 1
A setFileSelection() 0 6 1
A setBackupDestinations() 0 6 1
A addSelectedFilesToZip() 0 8 1
A addDatabaseDumpsToZip() 0 13 1
A copyToBackupDestinations() 0 23 2
1
<?php
2
3
namespace Spatie\Backup\Tasks\Backup;
4
5
use Illuminate\Support\Collection;
6
use Spatie\Backup\BackupDestination\Backup;
7
use Spatie\Backup\BackupDestination\BackupDestination;
8
use Spatie\Backup\Events\BackupHasFailed;
9
use Spatie\Backup\Events\BackupWasSuccessful;
10
use Spatie\Backup\Events\BackupZipWasCreated;
11
use Spatie\Backup\Exceptions\InvalidBackupJob;
12
use Spatie\Backup\Helpers\Format;
13
use Spatie\DbDumper\DbDumper;
14
use Exception;
15
16
class BackupJob
17
{
18
    /**  @var \Spatie\Backup\Tasks\Backup\FileSelection */
19
    protected $fileSelection;
20
21
    /** @var \Illuminate\Support\Collection */
22
    protected $dbDumpers;
23
24
    /** @var \Illuminate\Support\Collection */
25
    protected $backupDestinations;
26
27
    /** @var \Spatie\Backup\Tasks\Backup\TemporaryDirectory */
28
    protected $temporaryDirectory;
29
30
    public function __construct()
31
    {
32
        $this->doNotBackupFilesystem();
33
        $this->doNotBackupDatabases();
34
35
        $this->backupDestinations = new Collection();
36
    }
37
38
    /**
39
     * @return \Spatie\Backup\Tasks\Backup\BackupJob
40
     */
41
    public function doNotBackupFilesystem()
42
    {
43
        $this->fileSelection = FileSelectionFactory::noFiles();
44
45
        return $this;
46
    }
47
48
    /**
49
     * @return \Spatie\Backup\Tasks\Backup\BackupJob
50
     */
51
    public function doNotBackupDatabases()
52
    {
53
        $this->dbDumpers = new Collection();
54
55
        return $this;
56
    }
57
58
    /**
59
     * @param \Spatie\Backup\Tasks\Backup\FileSelection $fileSelection
60
     *
61
     * @return \Spatie\Backup\Tasks\Backup\BackupJob
62
     */
63
    public function setFileSelection(FileSelection $fileSelection)
64
    {
65
        $this->fileSelection = $fileSelection;
66
67
        return $this;
68
    }
69
70
    /**
71
     * @param array $dbDumpers
72
     *
73
     * @return \Spatie\Backup\Tasks\Backup\BackupJob
74
     */
75
    public function setDbDumpers(array $dbDumpers)
76
    {
77
        $this->dbDumpers = Collection::make($dbDumpers);
78
79
        return $this;
80
    }
81
82
    /**
83
     * @param string $diskName
84
     *
85
     * @return \Spatie\Backup\Tasks\Backup\BackupJob
86
     *
87
     * @throws \Spatie\Backup\Exceptions\InvalidBackupJob
88
     */
89
    public function backupOnlyTo($diskName)
90
    {
91
        $this->backupDestinations = $this->backupDestinations->filter(function (BackupDestination $backupDestination) use ($diskName) {
92
           return $backupDestination->getDiskName() === $diskName;
93
        });
94
95
        if (!count($this->backupDestinations)) {
96
            throw InvalidBackupJob::destinationDoesNotExist($diskName);
97
        }
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param \Illuminate\Support\Collection $backupDestinations
104
     *
105
     * @return \Spatie\Backup\Tasks\Backup\BackupJob
106
     */
107
    public function setBackupDestinations(Collection $backupDestinations)
108
    {
109
        $this->backupDestinations = $backupDestinations;
110
111
        return $this;
112
    }
113
114
    public function run()
115
    {
116
        try {
117
            if (!count($this->backupDestinations)) {
118
                throw InvalidBackupJob::noDestinationsSpecified();
119
            }
120
121
            $this->temporaryDirectory = TemporaryDirectory::create();
122
123
            $zip = $this->createZipContainingAllFilesToBeBackedUp();
124
125
            $this->copyToBackupDestinations($zip);
126
127
            $this->temporaryDirectory->delete();
128
        } catch (Exception $exception) {
129
            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...
130
131
            event(new BackupHasFailed($exception));
132
        }
133
    }
134
135
    /**
136
     * @return \Spatie\Backup\Tasks\Backup\Zip
137
     */
138
    protected function createZipContainingAllFilesToBeBackedUp()
139
    {
140
        $zip = Zip::create($this->temporaryDirectory->getPath(date('Y-m-d-His').'.zip'));
141
142
        $this->addDatabaseDumpsToZip($zip);
143
144
        $this->addSelectedFilesToZip($zip);
145
146
        event(new BackupZipWasCreated($zip));
147
148
        return $zip;
149
    }
150
151
    /**
152
     * @param \Spatie\Backup\Tasks\Backup\Zip $zip
153
     */
154
    protected function addSelectedFilesToZip(Zip $zip)
155
    {
156
        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...
157
158
        $zip->add($this->fileSelection->getSelectedFiles());
0 ignored issues
show
Documentation introduced by
$this->fileSelection->getSelectedFiles() is of type object<Generator>, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
159
160
        consoleOutput()->info("Zipped {$zip->count()} 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...
161
    }
162
163
    /**
164
     * @param \Spatie\Backup\Tasks\Backup\Zip $zip
165
     */
166
    protected function addDatabaseDumpsToZip(Zip $zip)
167
    {
168
        $this->dbDumpers->each(function (DbDumper $dbDumper) use ($zip) {
169
170
            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...
171
172
            $fileName = $dbDumper->getDbName().'.sql';
173
            $temporaryFile = $this->temporaryDirectory->getPath($fileName);
174
            $dbDumper->dumpToFile($temporaryFile);
175
176
            $zip->add($temporaryFile, $fileName);
177
        });
178
    }
179
180
    /**
181
     * @param \Spatie\Backup\Tasks\Backup\Zip $zip
182
     */
183
    protected function copyToBackupDestinations(Zip $zip)
184
    {
185
        $this->backupDestinations->each(function (BackupDestination $backupDestination) use ($zip) {
186
187
            try {
188
                $fileSize = Format::getHumanReadableSize($zip->getSize());
189
190
                $fileName = pathinfo($zip->getPath(), PATHINFO_BASENAME);
191
192
                consoleOutput()->info("Copying {$fileName} (size: {$fileSize}) to disk named {$backupDestination->getDiskName()}...");
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...
193
194
                $backupDestination->write($zip->getPath());
195
196
                consoleOutput()->info("Successfully copied .zip file to disk named {$backupDestination->getDiskName()}.");
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...
197
198
                event(new BackupWasSuccessful($backupDestination));
199
            } catch (Exception $exception) {
200
                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...
201
202
                event(new BackupHasFailed($exception));
203
            }
204
        });
205
    }
206
}
207