Passed
Push — master ( 4422d3...1f3291 )
by Pascal
19:42 queued 15:59
created

Disk::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Filesystem;
4
5
use Illuminate\Contracts\Filesystem\Filesystem;
6
use Illuminate\Filesystem\FilesystemAdapter;
7
use Illuminate\Support\Facades\Storage;
8
use Illuminate\Support\Traits\ForwardsCalls;
9
use League\Flysystem\Adapter\Local;
10
use League\Flysystem\AdapterInterface;
11
use League\Flysystem\Filesystem as LeagueFilesystem;
12
use Spatie\TemporaryDirectory\TemporaryDirectory;
13
14
/**
15
 * @mixin \Illuminate\Filesystem\FilesystemAdapter
16
 */
17
class Disk
18
{
19
    use ForwardsCalls;
20
21
    /**
22
     * @var string|\Illuminate\Contracts\Filesystem\Filesystem
23
     */
24
    private $disk;
25
26
    /**
27
     * @var \Spatie\TemporaryDirectory\TemporaryDirectory
28
     */
29
    private $temporaryDirectory;
30
31
    /**
32
     * @var \Illuminate\Filesystem\FilesystemAdapter
33
     */
34
    private $filesystemAdapter;
35
36
    public function __construct($disk)
37
    {
38
        $this->disk = $disk;
39
    }
40
41
    /**
42
     * Little helper method to instantiate this class.
43
     */
44
    public static function make($disk): self
45
    {
46
        if ($disk instanceof self) {
47
            return $disk;
48
        }
49
50
        return new static($disk);
51
    }
52
53
    /**
54
     * Creates a fresh instance, mostly used to force a new TemporaryDirectory.
55
     */
56
    public function clone(): self
57
    {
58
        return new Disk($this->disk);
59
    }
60
61
    /**
62
     * Creates a new TemporaryDirectory instance if none is set, otherwise
63
     * it returns the current one.
64
     */
65
    public function getTemporaryDirectory(): TemporaryDirectory
66
    {
67
        if ($this->temporaryDirectory) {
68
            return $this->temporaryDirectory;
69
        }
70
71
        return $this->temporaryDirectory = TemporaryDirectories::create();
72
    }
73
74
    public function makeMedia(string $path): Media
75
    {
76
        return Media::make($this, $path);
77
    }
78
79
    /**
80
     * Returns the name of the disk. It generates a name if the disk
81
     * is an instance of Flysystem.
82
     */
83
    public function getName(): string
84
    {
85
        if (is_string($this->disk)) {
86
            return $this->disk;
87
        }
88
89
        return get_class($this->getFlysystemAdapter()) . "_" . md5(json_encode(serialize($this->getFlysystemAdapter())));
90
    }
91
92
    public function getFilesystemAdapter(): FilesystemAdapter
93
    {
94
        if ($this->filesystemAdapter) {
95
            return $this->filesystemAdapter;
96
        }
97
98
        if ($this->disk instanceof Filesystem) {
99
            return $this->filesystemAdapter = $this->disk;
0 ignored issues
show
Documentation Bug introduced by
$this->disk is of type Illuminate\Contracts\Filesystem\Filesystem, but the property $filesystemAdapter was declared to be of type Illuminate\Filesystem\FilesystemAdapter. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
100
        }
101
102
        return $this->filesystemAdapter = Storage::disk($this->disk);
103
    }
104
105
    private function getFlysystemDriver(): LeagueFilesystem
106
    {
107
        return $this->getFilesystemAdapter()->getDriver();
108
    }
109
110
    private function getFlysystemAdapter(): AdapterInterface
111
    {
112
        return $this->getFlysystemDriver()->getAdapter();
113
    }
114
115
    public function isLocalDisk(): bool
116
    {
117
        return $this->getFlysystemAdapter() instanceof Local;
118
    }
119
120
    /**
121
     * Forwards all calls to Laravel's FilesystemAdapter which will pass
122
     * dynamic methods call onto Flysystem.
123
     */
124
    public function __call($method, $parameters)
125
    {
126
        return $this->forwardCallTo($this->getFilesystemAdapter(), $method, $parameters);
127
    }
128
}
129