CleanupCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 24 3
1
<?php
2
3
namespace Spatie\Backup\Commands;
4
5
use Exception;
6
use Spatie\Backup\BackupDestination\BackupDestinationFactory;
7
use Spatie\Backup\Events\CleanupHasFailed;
8
use Spatie\Backup\Tasks\Cleanup\CleanupJob;
9
use Spatie\Backup\Tasks\Cleanup\CleanupStrategy;
10
11
class CleanupCommand extends BaseCommand
12
{
13
    /** @var string */
14
    protected $signature = 'backup:clean {--disable-notifications}';
15
16
    /** @var string */
17
    protected $description = 'Remove all backups older than specified number of days in config.';
18
19
    /** @var \Spatie\Backup\Tasks\Cleanup\CleanupStrategy */
20
    protected $strategy;
21
22
    public function __construct(CleanupStrategy $strategy)
23
    {
24
        parent::__construct();
25
26
        $this->strategy = $strategy;
27
    }
28
29
    public function handle()
30
    {
31
        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...
32
33
        $disableNotifications = $this->option('disable-notifications');
34
35
        try {
36
            $config = config('backup');
37
38
            $backupDestinations = BackupDestinationFactory::createFromArray($config['backup']);
39
40
            $cleanupJob = new CleanupJob($backupDestinations, $this->strategy, $disableNotifications);
41
42
            $cleanupJob->run();
43
44
            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...
45
        } catch (Exception $exception) {
46
            if (! $disableNotifications) {
47
                event(new CleanupHasFailed($exception));
48
            }
49
50
            return 1;
51
        }
52
    }
53
}
54