Completed
Push — master ( 40cd32...2cd668 )
by Pascal
02:13
created

MediaExporter::accurate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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