Completed
Push — master ( e2f295...2fe1b4 )
by Pascal
05:22
created

MediaExporter::getMedia()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Pbmedia\LaravelFFMpeg;
4
5
use FFMpeg\Format\FormatInterface;
6
7
class MediaExporter
8
{
9
    protected $media;
10
11
    protected $disk;
12
13
    protected $format;
14
15
    protected $visibility;
16
17
    protected $saveMethod = 'saveAudioOrVideo';
18
19
    public function __construct(Media $media)
20
    {
21
        $this->media = $media;
22
23
        $this->disk = $media->getFile()->getDisk();
24
    }
25
26
    public function getMedia(): Media
27
    {
28
        return $this->media;
29
    }
30
31
    public function getFormat(): FormatInterface
32
    {
33
        return $this->format;
34
    }
35
36
    public function inFormat(FormatInterface $format): MediaExporter
37
    {
38
        $this->format = $format;
39
40
        return $this;
41
    }
42
43
    protected function getDisk(): Disk
44
    {
45
        return $this->disk;
46
    }
47
48
    public function toDisk($diskOrName): MediaExporter
49
    {
50
        if ($diskOrName instanceof Disk) {
51
            $this->disk = $diskOrName;
52
        } else {
53
            $this->disk = Disk::fromName($diskOrName);
54
        }
55
56
        return $this;
57
    }
58
59
    public function withVisibility(string $visibility = null)
60
    {
61
        $this->visibility = $visibility;
62
63
        return $this;
64
    }
65
66
    public function save(string $path): Media
67
    {
68
        $disk = $this->getDisk();
69
        $file = $disk->newFile($path);
70
71
        $destinationPath = $this->getDestinationPathForSaving($file);
72
73
        $this->createDestinationPathForSaving($file);
74
75
        $this->{$this->saveMethod}($destinationPath);
76
77
        if (!$disk->isLocal()) {
78
            $this->moveSavedFileToRemoteDisk($destinationPath, $file);
79
        }
80
81
        if ($this->visibility !== null) {
82
            $disk->setVisibility($path, $this->visibility);
83
        }
84
85
        return $this->media;
86
    }
87
88
    protected function moveSavedFileToRemoteDisk($localSourcePath, File $fileOnRemoteDisk): bool
89
    {
90
        return $fileOnRemoteDisk->put($localSourcePath) && @unlink($localSourcePath);
91
    }
92
93
    private function getDestinationPathForSaving(File $file): string
94
    {
95
        if (!$file->getDisk()->isLocal()) {
96
            $tempName = FFMpeg::newTemporaryFile();
97
98
            return $tempName . '.' . $file->getExtension();
99
        }
100
101
        return $file->getFullPath();
102
    }
103
104
    private function createDestinationPathForSaving(File $file)
105
    {
106
        if (!$file->getDisk()->isLocal()) {
107
            return false;
108
        }
109
110
        $directory = pathinfo($file->getPath())['dirname'];
111
112
        return $file->getDisk()->createDirectory($directory);
113
    }
114
115
    private function saveAudioOrVideo(string $fullPath): MediaExporter
116
    {
117
        $this->media->save($this->getFormat(), $fullPath);
118
119
        return $this;
120
    }
121
}
122