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

Media::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Pbmedia\LaravelFFMpeg\Filesystem;
4
5
use Illuminate\Filesystem\FilesystemAdapter;
6
use Spatie\TemporaryDirectory\TemporaryDirectory;
7
8
class Media
9
{
10
    private Disk $disk;
11
    private $path;
12
    private ?TemporaryDirectory $temporaryDirectory = null;
13
14
    public function __construct(Disk $disk, $path)
15
    {
16
        $this->disk = $disk;
17
        $this->path = $path;
18
    }
19
20
    public static function make($disk, $path): self
21
    {
22
        return new static(Disk::make($disk), $path);
23
    }
24
25
    public function getDisk(): Disk
26
    {
27
        return $this->disk;
28
    }
29
30
    public function getPath(): string
31
    {
32
        return $this->path;
33
    }
34
35
    public function getFilenameWithoutExtension(): string
36
    {
37
        return pathinfo($this->getPath())['filename'];
38
    }
39
40
    private function temporaryDirectoryAdapter(): FilesystemAdapter
41
    {
42
        return app('filesystem')->createLocalDriver(
43
            ['root' => $this->temporaryDirectory->path()]
0 ignored issues
show
Bug introduced by
The method path() does not exist on null. ( Ignorable by Annotation )

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

43
            ['root' => $this->temporaryDirectory->/** @scrutinizer ignore-call */ path()]

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
        );
45
    }
46
47
    public function getLocalPath(): string
48
    {
49
        $disk = $this->getDisk();
50
        $path = $this->getPath();
51
52
        if ($disk->isLocalDisk()) {
53
            return $disk->path($path);
0 ignored issues
show
Bug introduced by
The method path() does not exist on Pbmedia\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

53
            return $disk->/** @scrutinizer ignore-call */ path($path);
Loading history...
54
        }
55
56
        if (!$this->temporaryDirectory) {
57
            $this->temporaryDirectory = $disk->getTemporaryDirectory();
58
59
            if ($disk->exists($path)) {
0 ignored issues
show
Bug introduced by
The method exists() does not exist on Pbmedia\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

59
            if ($disk->/** @scrutinizer ignore-call */ exists($path)) {
Loading history...
60
                $this->temporaryDirectoryAdapter()->writeStream($path, $disk->readStream($path));
0 ignored issues
show
Bug introduced by
The method readStream() does not exist on Pbmedia\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

60
                $this->temporaryDirectoryAdapter()->writeStream($path, $disk->/** @scrutinizer ignore-call */ readStream($path));
Loading history...
61
            }
62
        }
63
64
        return $this->temporaryDirectoryAdapter()->path($path);
65
    }
66
67
    public function copyAllFromTemporaryDirectory(string $visibility = null)
68
    {
69
        if (!$this->temporaryDirectory) {
70
            return $this;
71
        }
72
73
        $temporaryDirectoryAdapter = $this->temporaryDirectoryAdapter();
74
75
        $destinationAdapater = $this->getDisk()->getFilesystemAdapter();
76
77
        foreach ($temporaryDirectoryAdapter->allFiles() as $path) {
78
            $destinationAdapater->writeStream($path, $temporaryDirectoryAdapter->readStream($path));
79
80
            if ($visibility) {
81
                $destinationAdapater->setVisibility($path, $visibility);
82
            }
83
        }
84
85
        return $this;
86
    }
87
88
    public function setVisibility(string $visibility = null)
89
    {
90
        $disk = $this->getDisk();
91
92
        if ($visibility && $disk->isLocalDisk()) {
93
            $disk->setVisibility($visibility);
0 ignored issues
show
Bug introduced by
The method setVisibility() does not exist on Pbmedia\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

93
            $disk->/** @scrutinizer ignore-call */ 
94
                   setVisibility($visibility);
Loading history...
94
        }
95
96
        return $this;
97
    }
98
}
99