Passed
Push — master ( d7ad3b...186424 )
by Pascal
02:42
created

Disk::path()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
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
13
/**
14
 * @mixin \Illuminate\Filesystem\FilesystemAdapter
15
 */
16
class Disk
17
{
18
    use ForwardsCalls;
19
20
    /**
21
     * @var string|\Illuminate\Contracts\Filesystem\Filesystem
22
     */
23
    private $disk;
24
25
    /**
26
     * @var string
27
     */
28
    private $temporaryDirectory;
29
30
    /**
31
     * @var \Illuminate\Filesystem\FilesystemAdapter
32
     */
33
    private $filesystemAdapter;
34
35
    public function __construct($disk)
36
    {
37
        $this->disk = $disk;
38
    }
39
40
    /**
41
     * Little helper method to instantiate this class.
42
     */
43
    public static function make($disk): self
44
    {
45
        if ($disk instanceof self) {
46
            return $disk;
47
        }
48
49
        return new static($disk);
50
    }
51
52
    public static function makeTemporaryDisk(): self
53
    {
54
        $filesystemAdapter = app('filesystem')->createLocalDriver([
55
            'root' => app(TemporaryDirectories::class)->create(),
56
        ]);
57
58
        return new static($filesystemAdapter);
59
    }
60
61
    /**
62
     * Creates a fresh instance, mostly used to force a new TemporaryDirectory.
63
     */
64
    public function clone(): self
65
    {
66
        return new Disk($this->disk);
67
    }
68
69
    /**
70
     * Creates a new TemporaryDirectory instance if none is set, otherwise
71
     * it returns the current one.
72
     */
73
    public function getTemporaryDirectory(): string
74
    {
75
        if ($this->temporaryDirectory) {
76
            return $this->temporaryDirectory;
77
        }
78
79
        return $this->temporaryDirectory = app(TemporaryDirectories::class)->create();
80
    }
81
82
    public function makeMedia(string $path): Media
83
    {
84
        return Media::make($this, $path);
85
    }
86
87
    /**
88
     * Returns the name of the disk. It generates a name if the disk
89
     * is an instance of Flysystem.
90
     */
91
    public function getName(): string
92
    {
93
        if (is_string($this->disk)) {
94
            return $this->disk;
95
        }
96
97
        return get_class($this->getFlysystemAdapter()) . "_" . md5(json_encode(serialize($this->getFlysystemAdapter())));
98
    }
99
100
    public function getFilesystemAdapter(): FilesystemAdapter
101
    {
102
        if ($this->filesystemAdapter) {
103
            return $this->filesystemAdapter;
104
        }
105
106
        if ($this->disk instanceof Filesystem) {
107
            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...
108
        }
109
110
        return $this->filesystemAdapter = Storage::disk($this->disk);
111
    }
112
113
    private function getFlysystemDriver(): LeagueFilesystem
114
    {
115
        return $this->getFilesystemAdapter()->getDriver();
116
    }
117
118
    private function getFlysystemAdapter(): AdapterInterface
119
    {
120
        return $this->getFlysystemDriver()->getAdapter();
121
    }
122
123
    public function isLocalDisk(): bool
124
    {
125
        return $this->getFlysystemAdapter() instanceof Local;
126
    }
127
128
    public static function normalizePath($path): string
129
    {
130
        return str_replace('\\', '/', $path);
131
    }
132
133
    /**
134
     * Get the full path for the file at the given "short" path.
135
     *
136
     * @param  string  $path
137
     * @return string
138
     */
139
    public function path(string $path): string
140
    {
141
        $path = $this->getFilesystemAdapter()->path($path);
142
143
        return $this->isLocalDisk() ? static::normalizePath($path) : $path;
144
    }
145
146
    /**
147
     * Forwards all calls to Laravel's FilesystemAdapter which will pass
148
     * dynamic methods call onto Flysystem.
149
     */
150
    public function __call($method, $parameters)
151
    {
152
        return $this->forwardCallTo($this->getFilesystemAdapter(), $method, $parameters);
153
    }
154
}
155