Completed
Push — master ( 3f3235...f2098d )
by Freek
01:35
created

Backup::disk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Backup\BackupDestination;
4
5
use Carbon\Carbon;
6
use Illuminate\Contracts\Filesystem\Filesystem;
7
8
class Backup
9
{
10
    /** @var \Illuminate\Contracts\Filesystem\Filesystem */
11
    protected $disk;
12
13
    /** @var string */
14
    protected $path;
15
16
    /** @var bool */
17
    protected $exists;
18
19
    /** @var Carbon */
20
    protected $date;
21
22
    /** @var int */
23
    protected $size;
24
25
    public function __construct(Filesystem $disk, string $path)
26
    {
27
        $this->disk = $disk;
28
29
        $this->path = $path;
30
    }
31
32
    public function disk(): Filesystem
33
    {
34
        return $this->disk;
35
    }
36
37
    public function path(): string
38
    {
39
        return $this->path;
40
    }
41
42
    public function exists(): bool
43
    {
44
        if ($this->exists === null) {
45
            $this->exists = $this->disk->exists($this->path);
46
        }
47
48
        return $this->exists;
49
    }
50
51
    public function date(): Carbon
52
    {
53
        if ($this->date === null) {
54
            $this->date = Carbon::createFromTimestamp($this->disk->lastModified($this->path));
55
        }
56
57
        return $this->date;
58
    }
59
60
    /**
61
     * Get the size in bytes.
62
     */
63
    public function size(): int
64
    {
65
        if ($this->size === null) {
66
            if (! $this->exists()) {
67
                return 0;
68
            }
69
70
            $this->size = $this->disk->size($this->path);
71
        }
72
73
        return $this->size;
74
    }
75
76
    public function stream()
77
    {
78
        return $this->disk->readStream($this->path);
79
    }
80
81
    public function delete()
82
    {
83
        $this->exists = null;
84
85
        $this->disk->delete($this->path);
86
87
        consoleOutput()->info("Deleted backup `{$this->path}`.");
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...
88
    }
89
}
90