Completed
Push — master ( e9008e...ae37ea )
by Pascal
05:20
created

MediaExporter::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
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 $saveMethod = 'saveAudioOrVideo';
16
17
    public function __construct(Media $media)
18
    {
19
        $this->media = $media;
20
21
        $this->disk = $media->getFile()->getDisk();
22
    }
23
24
    protected function getFormat(): FormatInterface
25
    {
26
        return $this->format;
27
    }
28
29
    public function inFormat(FormatInterface $format): MediaExporter
30
    {
31
        $this->format = $format;
32
33
        return $this;
34
    }
35
36
    protected function getDisk(): Disk
37
    {
38
        return $this->disk;
39
    }
40
41
    public function toDisk($diskOrName): MediaExporter
42
    {
43
        if ($diskOrName instanceof Disk) {
44
            $this->disk = $diskOrName;
45
        } else {
46
            $this->disk = Disk::fromName($diskOrName);
47
        }
48
49
        return $this;
50
    }
51
52
    public function save(string $path): Media
53
    {
54
        $file = $this->getDisk()->newFile($path);
55
56
        $destinationPath = $this->getDestinationPathForSaving($file);
57
58
        $this->{$this->saveMethod}($destinationPath);
59
60
        if (!$this->getDisk()->isLocal()) {
61
            $this->moveSavedFileToRemoteDisk($destinationPath, $file);
62
        }
63
64
        return $this->media;
65
    }
66
67
    protected function moveSavedFileToRemoteDisk($localSourcePath, File $fileOnRemoteDisk): bool
68
    {
69
        return $fileOnRemoteDisk->put($localSourcePath) && unlink($localSourcePath);
70
    }
71
72
    private function getDestinationPathForSaving(File $file): string
73
    {
74
        if (!$file->getDisk()->isLocal()) {
75
            $tempName = tempnam(sys_get_temp_dir(), 'laravel-ffmpeg');
76
77
            return $tempName . '.' . $file->getExtension();
78
        }
79
80
        return $file->getFullPath();
81
    }
82
83
    private function saveAudioOrVideo(string $fullPath): MediaExporter
84
    {
85
        $this->media->save($this->getFormat(), $fullPath);
0 ignored issues
show
Documentation introduced by
$this->getFormat() is of type object<FFMpeg\Format\FormatInterface>, but the function expects a object<Pbmedia\LaravelFFMpeg\FormatInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
87
        return $this;
88
    }
89
}
90