BackupCommand   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
D handle() 0 47 9
A guardAgainstInvalidOptions() 0 6 3
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
            if ($this->option('db-name')) {
33
                $backupJob->onlyDbName($this->option('db-name'));
34
            }
35
36
            if ($this->option('only-files')) {
37
                $backupJob->dontBackupDatabases();
38
            }
39
40
            if ($this->option('only-to-disk')) {
41
                $backupJob->onlyBackupTo($this->option('only-to-disk'));
42
            }
43
44
            if ($this->option('filename')) {
45
                $backupJob->setFilename($this->option('filename'));
46
            }
47
48
            if ($disableNotifications) {
49
                $backupJob->disableNotifications();
50
            }
51
52
            $backupJob->run();
53
54
            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...
55
        } catch (Exception $exception) {
56
            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...
57
58
            if (! $disableNotifications) {
59
                event(new BackupHasFailed($exception));
60
            }
61
62
            return 1;
63
        }
64
    }
65
66
    protected function guardAgainstInvalidOptions()
67
    {
68
        if ($this->option('only-db') && $this->option('only-files')) {
69
            throw InvalidCommand::create('Cannot use `only-db` and `only-files` together');
70
        }
71
    }
72
}
73