CleanupJob   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A run() 0 24 3
A sendNotification() 0 6 2
1
<?php
2
3
namespace Spatie\Backup\Tasks\Cleanup;
4
5
use Exception;
6
use Illuminate\Support\Collection;
7
use Spatie\Backup\BackupDestination\BackupDestination;
8
use Spatie\Backup\Events\CleanupHasFailed;
9
use Spatie\Backup\Events\CleanupWasSuccessful;
10
use Spatie\Backup\Helpers\Format;
11
12
class CleanupJob
13
{
14
    /** @var \Illuminate\Support\Collection */
15
    protected $backupDestinations;
16
17
    /** @var \Spatie\Backup\Tasks\Cleanup\CleanupStrategy */
18
    protected $strategy;
19
20
    /** @var bool */
21
    protected $sendNotifications = true;
22
23
    public function __construct(Collection $backupDestinations, CleanupStrategy $strategy, bool $disableNotifications = false)
24
    {
25
        $this->backupDestinations = $backupDestinations;
26
27
        $this->strategy = $strategy;
28
29
        $this->sendNotifications = ! $disableNotifications;
30
    }
31
32
    public function run()
33
    {
34
        $this->backupDestinations->each(function (BackupDestination $backupDestination) {
35
            try {
36
                if (! $backupDestination->isReachable()) {
37
                    throw new Exception("Could not connect to disk {$backupDestination->diskName()} because: {$backupDestination->connectionError()}");
38
                }
39
40
                consoleOutput()->info("Cleaning backups of {$backupDestination->backupName()} on disk {$backupDestination->diskName()}...");
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...
41
42
                $this->strategy->deleteOldBackups($backupDestination->backups());
43
                $this->sendNotification(new CleanupWasSuccessful($backupDestination));
44
45
                $usedStorage = Format::humanReadableSize($backupDestination->fresh()->usedStorage());
46
                consoleOutput()->info("Used storage after cleanup: {$usedStorage}.");
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...
47
            } catch (Exception $exception) {
48
                consoleOutput()->error("Cleanup 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...
49
50
                $this->sendNotification(new CleanupHasFailed($exception));
51
52
                throw $exception;
53
            }
54
        });
55
    }
56
57
    protected function sendNotification($notification)
58
    {
59
        if ($this->sendNotifications) {
60
            event($notification);
61
        }
62
    }
63
}
64