Completed
Pull Request — master (#636)
by
unknown
03:58
created

BackupCommand::handle()   F

Complexity

Conditions 9
Paths 512

Size

Total Lines 48
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 25
nc 512
nop 0
dl 0
loc 48
rs 3.646
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Backup\Commands;
4
5
use Exception;
6
use Spatie\Backup\Events\BackupHasFailed;
7
use Spatie\Backup\Exceptions\InvalidCommand;
8
use Spatie\Backup\Tasks\Backup\BackupJobFactory;
9
10
class BackupCommand extends BaseCommand
11
{
12
    /** @var string */
13
    protected $signature = 'backup:run {--filename=} {--only-db} {--db-name=*} {--only-files} {--only-to-disk=} {--disable-notifications}';
14
15
    /** @var string */
16
    protected $description = 'Run the backup.';
17
18
    public function handle()
19
    {
20
        consoleOutput()->comment('Starting backup...');
0 ignored issues
show
Documentation Bug introduced by
The method comment 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...
21
22
        $disableNotifications = $this->option('disable-notifications');
23
24
        try {
25
            $this->guardAgainstInvalidOptions();
26
27
            $backupJob = BackupJobFactory::createFromArray(config('backup'));
28
29
            if ($this->option('only-db')) {
30
                $backupJob->dontBackupFilesystem();
31
                
32
            }
33
			if ($this->option('db-name')) {
34
				$backupJob->onlyDbName($this->option('db-name'));
0 ignored issues
show
Bug introduced by
It seems like $this->option('db-name') targeting Illuminate\Console\Command::option() can also be of type string; however, Spatie\Backup\Tasks\Backup\BackupJob::onlyDbName() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
35
			}
36
37
            if ($this->option('only-files')) {
38
                $backupJob->dontBackupDatabases();
39
            }
40
41
            if ($this->option('only-to-disk')) {
42
                $backupJob->onlyBackupTo($this->option('only-to-disk'));
0 ignored issues
show
Bug introduced by
It seems like $this->option('only-to-disk') targeting Illuminate\Console\Command::option() can also be of type array; however, Spatie\Backup\Tasks\Back...ckupJob::onlyBackupTo() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
43
            }
44
45
            if ($this->option('filename')) {
46
                $backupJob->setFilename($this->option('filename'));
0 ignored issues
show
Bug introduced by
It seems like $this->option('filename') targeting Illuminate\Console\Command::option() can also be of type array; however, Spatie\Backup\Tasks\Back...ackupJob::setFilename() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
47
            }
48
49
            if ($disableNotifications) {
50
                $backupJob->disableNotifications();
51
            }
52
53
            $backupJob->run();
54
55
            consoleOutput()->comment('Backup completed!');
0 ignored issues
show
Documentation Bug introduced by
The method comment 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...
56
        } catch (Exception $exception) {
57
            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...
58
59
            if (! $disableNotifications) {
60
                event(new BackupHasFailed($exception));
61
            }
62
63
            return 1;
64
        }
65
    }
66
67
    protected function guardAgainstInvalidOptions()
68
    {
69
        if ($this->option('only-db') && $this->option('only-files')) {
70
            throw InvalidCommand::create('Cannot use `only-db` and `only-files` together');
71
        }
72
    }
73
}
74