Completed
Push — master ( 2cd668...e9008e )
by Pascal
04:53
created

MediaExporter::setTempDir()   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 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 $tmpDir;
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
        $this->tmpDir = sys_get_temp_dir();
26
    }
27
28
    protected function getFormat(): FormatInterface
29
    {
30
        return $this->format;
31
    }
32
33
    public function inFormat(FormatInterface $format): MediaExporter
34
    {
35
        $this->format = $format;
36
37
        return $this;
38
    }
39
40
    protected function getDisk(): Disk
41
    {
42
        return $this->disk;
43
    }
44
45
    public function toDisk($diskOrName): MediaExporter
46
    {
47
        if ($diskOrName instanceof Disk) {
48
            $this->disk = $diskOrName;
49
        } else {
50
            $this->disk = Disk::fromName($diskOrName);
51
        }
52
53
        return $this;
54
    }
55
56
    public function setTempDir(string $tmpDir): MediaExporter
57
    {
58
        $this->tmpDir = $tmpDir;
59
60
        return $this;
61
    }
62
63
    public function save(string $path): Media
64
    {
65
        $file = $this->getDisk()->newFile($path);
66
67
        $destinationPath = $this->getDestinationPathForSaving($file);
68
69
        $this->{$this->saveMethod}($destinationPath);
70
71
        if (!$this->getDisk()->isLocal()) {
72
            $this->moveSavedFileToRemoteDisk($destinationPath, $file);
73
        }
74
75
        return $this->media;
76
    }
77
78
    protected function moveSavedFileToRemoteDisk($localSourcePath, File $fileOnRemoteDisk): bool
79
    {
80
        return $fileOnRemoteDisk->put($localSourcePath) && unlink($localSourcePath);
81
    }
82
83
    private function getDestinationPathForSaving(File $file): string
84
    {
85
        if (!$file->getDisk()->isLocal()) {
86
            $tempName = tempnam($this->tmpDir, 'laravel-ffmpeg');
87
88
            return $tempName . '.' . $file->getExtension();
89
        }
90
91
        return $file->getFullPath();
92
    }
93
94
    private function saveAudioOrVideo(string $fullPath): MediaExporter
95
    {
96
        $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...
97
98
        return $this;
99
    }
100
}
101