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

Media::getFilenameWithoutExtension()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Filesystem;
4
5
use Illuminate\Filesystem\FilesystemAdapter;
6
7
class Media
8
{
9
    /**
10
     * @var \ProtoneMedia\LaravelFFMpeg\Filesystem\Disk
11
     */
12
    private $disk;
13
14
    /**
15
     * @var string
16
     */
17
    private $path;
18
19
    /**
20
     * @var \Spatie\TemporaryDirectory\TemporaryDirectory
21
     */
22
    private $temporaryDirectory;
23
24
    public function __construct(Disk $disk, string $path)
25
    {
26
        $this->disk = $disk;
27
        $this->path = $path;
28
29
        $this->makeDirectory();
30
    }
31
32
    public static function make($disk, string $path): self
33
    {
34
        return new static(Disk::make($disk), $path);
35
    }
36
37
    public function getDisk(): Disk
38
    {
39
        return $this->disk;
40
    }
41
42
    public function getPath(): string
43
    {
44
        return $this->path;
45
    }
46
47
    public function getDirectory(): ?string
48
    {
49
        $directory = rtrim(pathinfo($this->getPath())['dirname'], DIRECTORY_SEPARATOR);
50
51
        if ($directory === '.') {
52
            $directory = '';
53
        }
54
55
        if ($directory) {
56
            $directory .= DIRECTORY_SEPARATOR;
57
        }
58
59
        return $directory;
60
    }
61
62
    private function makeDirectory(): void
63
    {
64
        $directory = $this->getDirectory();
65
66
        $adapter = $this->getDisk()->isLocalDisk()
67
            ? $this->getDisk()->getFilesystemAdapter()
68
            : $this->temporaryDirectoryAdapter();
69
70
        if ($adapter->has($directory)) {
71
            return;
72
        }
73
74
        $adapter->makeDirectory($directory);
75
    }
76
77
    public function getFilenameWithoutExtension(): string
78
    {
79
        return pathinfo($this->getPath())['filename'];
80
    }
81
82
    public function getFilename(): string
83
    {
84
        return pathinfo($this->getPath())['basename'];
85
    }
86
87
    private function temporaryDirectoryAdapter(): FilesystemAdapter
88
    {
89
        if (!$this->temporaryDirectory) {
90
            $this->temporaryDirectory = $this->getDisk()->getTemporaryDirectory();
91
        }
92
93
        return app('filesystem')->createLocalDriver(
94
            ['root' => $this->temporaryDirectory->path()]
95
        );
96
    }
97
98
    public function getLocalPath(): string
99
    {
100
        $disk = $this->getDisk();
101
        $path = $this->getPath();
102
103
        if ($disk->isLocalDisk()) {
104
            return $disk->path($path);
0 ignored issues
show
Bug introduced by
The method path() 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

104
            return $disk->/** @scrutinizer ignore-call */ path($path);
Loading history...
105
        }
106
107
        $temporaryDirectoryAdapter = $this->temporaryDirectoryAdapter();
108
109
        if ($disk->exists($path) && !$temporaryDirectoryAdapter->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

109
        if ($disk->/** @scrutinizer ignore-call */ exists($path) && !$temporaryDirectoryAdapter->exists($path)) {
Loading history...
110
            $temporaryDirectoryAdapter->writeStream($path, $disk->readStream($path));
0 ignored issues
show
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

110
            $temporaryDirectoryAdapter->writeStream($path, $disk->/** @scrutinizer ignore-call */ readStream($path));
Loading history...
111
        }
112
113
        return $temporaryDirectoryAdapter->path($path);
114
    }
115
116
    public function copyAllFromTemporaryDirectory(string $visibility = null)
117
    {
118
        if (!$this->temporaryDirectory) {
119
            return $this;
120
        }
121
122
        $temporaryDirectoryAdapter = $this->temporaryDirectoryAdapter();
123
124
        $destinationAdapater = $this->getDisk()->getFilesystemAdapter();
125
126
        foreach ($temporaryDirectoryAdapter->allFiles() as $path) {
127
            $destinationAdapater->writeStream($path, $temporaryDirectoryAdapter->readStream($path));
128
129
            if ($visibility) {
130
                $destinationAdapater->setVisibility($path, $visibility);
131
            }
132
        }
133
134
        return $this;
135
    }
136
137
    public function setVisibility(string $visibility = null)
138
    {
139
        $disk = $this->getDisk();
140
141
        if ($visibility && $disk->isLocalDisk()) {
142
            $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

142
            $disk->/** @scrutinizer ignore-call */ 
143
                   setVisibility($visibility);
Loading history...
143
        }
144
145
        return $this;
146
    }
147
}
148