Completed
Branch v7 (f01542)
by Pascal
08:46
created

Disk::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Pbmedia\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
    private $disk;
22
    private ?TemporaryDirectory $temporaryDirectory = null;
23
    private ?FilesystemAdapter $filesystemAdapter   = null;
24
25
    public function __construct($disk)
26
    {
27
        $this->disk = $disk;
28
    }
29
30
    public static function make($disk): self
31
    {
32
        if ($disk instanceof self) {
33
            return $disk;
34
        }
35
36
        if (is_string($disk)) {
37
            return new static($disk);
38
        }
39
40
        if ($disk instanceof Filesystem) {
41
            return new static($disk);
42
        }
43
    }
44
45
    public function clone(): self
46
    {
47
        return new Disk($this->disk);
48
    }
49
50
    public function getTemporaryDirectory(): TemporaryDirectory
51
    {
52
        if ($this->temporaryDirectory) {
53
            return $this->temporaryDirectory;
54
        }
55
56
        return  $this->temporaryDirectory = TemporaryDirectories::create();
57
    }
58
59
    public function makeMedia(string $path): Media
60
    {
61
        return Media::make($this, $path);
62
    }
63
64
    public function getName(): string
65
    {
66
        if (is_string($this->disk)) {
67
            return $this->disk;
68
        }
69
70
        return get_class($this->getFlysystemAdapter()) . "_" . md5(json_encode(serialize($this->getFlysystemAdapter())));
71
    }
72
73
    public function getFilesystemAdapter(): FilesystemAdapter
74
    {
75
        if ($this->filesystemAdapter) {
76
            return $this->filesystemAdapter;
77
        }
78
79
        if ($this->disk instanceof Filesystem) {
80
            return $this->filesystemAdapter = $this->disk;
81
        }
82
83
        return $this->filesystemAdapter = Storage::disk($this->disk);
84
    }
85
86
    private function getFlysystemDriver(): LeagueFilesystem
87
    {
88
        return $this->getFilesystemAdapter()->getDriver();
89
    }
90
91
    private function getFlysystemAdapter(): AdapterInterface
92
    {
93
        return $this->getFlysystemDriver()->getAdapter();
94
    }
95
96
    public function isLocalDisk(): bool
97
    {
98
        return $this->getFlysystemAdapter() instanceof Local;
99
    }
100
101
    public function __call($method, $parameters)
102
    {
103
        return $this->forwardCallTo($this->getFilesystemAdapter(), $method, $parameters);
104
    }
105
}
106