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

Media::temporaryDirectoryDisk()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Filesystem;
4
5
use Illuminate\Filesystem\FilesystemAdapter;
6
7
class Media
8
{
9
    use HasInputOptions;
10
11
    /**
12
     * @var \ProtoneMedia\LaravelFFMpeg\Filesystem\Disk
13
     */
14
    private $disk;
15
16
    /**
17
     * @var string
18
     */
19
    private $path;
20
21
    /**
22
     * @var string
23
     */
24
    private $temporaryDirectory;
25
26
    public function __construct(Disk $disk, string $path)
27
    {
28
        $this->disk = $disk;
29
        $this->path = $path;
30
31
        $this->makeDirectory();
32
    }
33
34
    public static function make($disk, string $path): self
35
    {
36
        return new static(Disk::make($disk), $path);
37
    }
38
39
    public function getDisk(): Disk
40
    {
41
        return $this->disk;
42
    }
43
44
    public function getPath(): string
45
    {
46
        return $this->path;
47
    }
48
49
    public function getDirectory(): ?string
50
    {
51
        $directory = rtrim(pathinfo($this->getPath())['dirname'], DIRECTORY_SEPARATOR);
52
53
        if ($directory === '.') {
54
            $directory = '';
55
        }
56
57
        if ($directory) {
58
            $directory .= DIRECTORY_SEPARATOR;
59
        }
60
61
        return $directory;
62
    }
63
64
    private function makeDirectory(): void
65
    {
66
        $disk = $this->getDisk();
67
68
        if (!$disk->isLocalDisk()) {
69
            $disk = $this->temporaryDirectoryDisk();
70
        }
71
72
        $directory = $this->getDirectory();
73
74
        if ($disk->has($directory)) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on ProtoneMedia\LaravelFFMpeg\Filesystem\Disk. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        if ($disk->/** @scrutinizer ignore-call */ has($directory)) {
Loading history...
75
            return;
76
        }
77
78
        $disk->makeDirectory($directory);
0 ignored issues
show
Bug introduced by
The method makeDirectory() does not exist on ProtoneMedia\LaravelFFMpeg\Filesystem\Disk. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        $disk->/** @scrutinizer ignore-call */ 
79
               makeDirectory($directory);
Loading history...
79
    }
80
81
    public function getFilenameWithoutExtension(): string
82
    {
83
        return pathinfo($this->getPath())['filename'];
84
    }
85
86
    public function getFilename(): string
87
    {
88
        return pathinfo($this->getPath())['basename'];
89
    }
90
91
    private function temporaryDirectoryDisk(): Disk
92
    {
93
        return Disk::make($this->temporaryDirectoryAdapter());
94
    }
95
96
    private function temporaryDirectoryAdapter(): FilesystemAdapter
97
    {
98
        if (!$this->temporaryDirectory) {
99
            $this->temporaryDirectory = $this->getDisk()->getTemporaryDirectory();
100
        }
101
102
        return app('filesystem')->createLocalDriver(
103
            ['root' => $this->temporaryDirectory]
104
        );
105
    }
106
107
    public function getLocalPath(): string
108
    {
109
        $disk = $this->getDisk();
110
        $path = $this->getPath();
111
112
        if ($disk->isLocalDisk()) {
113
            return $disk->path($path);
114
        }
115
116
        $temporaryDirectoryDisk = $this->temporaryDirectoryDisk();
117
118
        if ($disk->exists($path) && !$temporaryDirectoryDisk->exists($path)) {
0 ignored issues
show
Bug introduced by
The method exists() does not exist on ProtoneMedia\LaravelFFMpeg\Filesystem\Disk. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
        if ($disk->/** @scrutinizer ignore-call */ exists($path) && !$temporaryDirectoryDisk->exists($path)) {
Loading history...
119
            $temporaryDirectoryDisk->writeStream($path, $disk->readStream($path));
0 ignored issues
show
Bug introduced by
The method writeStream() does not exist on ProtoneMedia\LaravelFFMpeg\Filesystem\Disk. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
            $temporaryDirectoryDisk->/** @scrutinizer ignore-call */ 
120
                                     writeStream($path, $disk->readStream($path));
Loading history...
Bug introduced by
The method readStream() does not exist on ProtoneMedia\LaravelFFMpeg\Filesystem\Disk. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
            $temporaryDirectoryDisk->writeStream($path, $disk->/** @scrutinizer ignore-call */ readStream($path));
Loading history...
120
        }
121
122
        return $temporaryDirectoryDisk->path($path);
123
    }
124
125
    public function copyAllFromTemporaryDirectory(string $visibility = null)
126
    {
127
        if (!$this->temporaryDirectory) {
128
            return $this;
129
        }
130
131
        $temporaryDirectoryDisk = $this->temporaryDirectoryDisk();
132
133
        $destinationAdapater = $this->getDisk()->getFilesystemAdapter();
134
135
        foreach ($temporaryDirectoryDisk->allFiles() as $path) {
0 ignored issues
show
Bug introduced by
The method allFiles() does not exist on ProtoneMedia\LaravelFFMpeg\Filesystem\Disk. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
        foreach ($temporaryDirectoryDisk->/** @scrutinizer ignore-call */ allFiles() as $path) {
Loading history...
136
            $destinationAdapater->writeStream($path, $temporaryDirectoryDisk->readStream($path));
137
138
            if ($visibility) {
139
                $destinationAdapater->setVisibility($path, $visibility);
140
            }
141
        }
142
143
        return $this;
144
    }
145
146
    public function setVisibility(string $visibility = null)
147
    {
148
        $disk = $this->getDisk();
149
150
        if ($visibility && $disk->isLocalDisk()) {
151
            $disk->setVisibility($visibility);
0 ignored issues
show
Bug introduced by
The method setVisibility() does not exist on ProtoneMedia\LaravelFFMpeg\Filesystem\Disk. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
            $disk->/** @scrutinizer ignore-call */ 
152
                   setVisibility($visibility);
Loading history...
152
        }
153
154
        return $this;
155
    }
156
}
157