Completed
Push — master ( f62f21...f72c97 )
by Freek
05:09
created

CleanupJob::run()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 2
Metric Value
c 7
b 1
f 2
dl 0
loc 23
rs 9.0856
cc 3
eloc 13
nc 1
nop 0
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\Strategies\CleanupStrategy */
18
    protected $strategy;
19
20
    /**
21
     * @param \Illuminate\Support\Collection               $backupDestinations
22
     * @param \Spatie\Backup\Tasks\Cleanup\CleanupStrategy $strategy
23
     */
24
    public function __construct(Collection $backupDestinations, CleanupStrategy $strategy)
25
    {
26
        $this->backupDestinations = $backupDestinations;
27
        $this->strategy = $strategy;
0 ignored issues
show
Documentation Bug introduced by
It seems like $strategy of type object<Spatie\Backup\Tas...leanup\CleanupStrategy> is incompatible with the declared type object<Spatie\Backup\Tas...tegies\CleanupStrategy> of property $strategy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
    }
29
30
    public function run()
31
    {
32
        $this->backupDestinations->each(function (BackupDestination $backupDestination) {
33
34
            try {
35
                if (!$backupDestination->isReachable()) {
36
                    throw new Exception("Could not connect to {$backupDestination->getFilesystemType()} because: {$backupDestination->getConnectionError()}");
37
                };
38
39
                consoleOutput()->info("Cleaning backups of {$backupDestination->getBackupName()} on {$backupDestination->getFilesystemType()}-filesystem");
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...
40
41
                $this->strategy->deleteOldBackups($backupDestination->getBackups());
42
                event(new CleanupWasSuccessFul($backupDestination));
43
44
                $usedStorage = Format::getHumanReadableSize($backupDestination->getUsedStorage());
45
                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...
46
            } catch (Exception $exception) {
47
                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...
48
49
                event(new CleanupHasFailed($exception));
50
            }
51
        });
52
    }
53
}
54