Completed
Push — master ( 198554...54d251 )
by Freek
02:26
created

CleanupCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 26 3
1
<?php
2
3
namespace Spatie\Backup\Commands;
4
5
use Exception;
6
use Spatie\Backup\Events\CleanupHasFailed;
7
use Spatie\Backup\Tasks\Cleanup\CleanupJob;
8
use Spatie\Backup\BackupDestination\BackupDestinationFactory;
9
10
class CleanupCommand extends BaseCommand
11
{
12
    /** @var string */
13
    protected $signature = 'backup:clean {--disable-notifications}';
14
15
    /** @var string */
16
    protected $description = 'Remove all backups older than specified number of days in config.';
17
18
    public function handle()
19
    {
20
        consoleOutput()->comment('Starting cleanup...');
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
            $config = config('laravel-backup');
26
27
            $backupDestinations = BackupDestinationFactory::createFromArray($config['backup']);
28
29
            $strategy = app($config['cleanup']['strategy']);
30
31
            $cleanupJob = new CleanupJob($backupDestinations, $strategy, $disableNotifications);
0 ignored issues
show
Documentation introduced by
$disableNotifications is of type string|array, but the function expects a boolean.

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...
32
33
            $cleanupJob->run();
34
35
            consoleOutput()->comment('Cleanup 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...
36
        } catch (Exception $exception) {
37
            if (! $disableNotifications) {
38
                event(new CleanupHasFailed($exception));
39
            }
40
41
            return -1;
42
        }
43
    }
44
}
45