Completed
Push — master ( 1abdda...74bc12 )
by Pascal
02:08
created

MediaExporter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
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 $frameMustBeAccurate = false;
14
15
    protected $format;
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): self
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(string $diskName): self
41
    {
42
        $this->disk = Disk::fromName($diskName);
43
44
        return $this;
45
    }
46
47
    public function accurate(): self
48
    {
49
        $this->frameMustBeAccurate = true;
50
51
        return $this;
52
    }
53
54
    public function unaccurate(): self
55
    {
56
        $this->frameMustBeAccurate = false;
57
58
        return $this;
59
    }
60
61
    public function getAccuracy(): bool
62
    {
63
        return $this->frameMustBeAccurate;
64
    }
65
66
    public function save(string $path): Media
67
    {
68
        $file = $this->getDisk()->newFile($path);
69
70
        $destinationPath = $this->getDestinationPathForSaving($file);
71
72
        $saveMethod = $this->media->isFrame() ? 'saveFrame' : 'saveAudioOrVideo';
73
74
        $this->{$saveMethod}($destinationPath);
75
76
        if (!$this->getDisk()->isLocal()) {
77
            $this->moveSavedFileToRemoteDisk($destinationPath, $file);
78
        }
79
80
        return $this->media;
81
    }
82
83
    private function moveSavedFileToRemoteDisk($localSourcePath, File $fileOnRemoteDisk): bool
84
    {
85
        $resource = fopen($localSourcePath, 'r');
86
87
        return $fileOnRemoteDisk->put($resource) && unlink($localSourcePath);
88
    }
89
90
    private function getDestinationPathForSaving(File $file): string
91
    {
92
        if (!$file->getDisk()->isLocal()) {
93
            $tempName = tempnam(sys_get_temp_dir(), 'laravel-ffmpeg');
94
95
            return $tempName . '.' . $file->getExtension();
96
        }
97
98
        return $file->getFullPath();
99
    }
100
101
    private function saveFrame(string $fullPath): self
102
    {
103
        $this->media->save($fullPath, $this->getAccuracy());
0 ignored issues
show
Documentation introduced by
$fullPath is of type string, 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...
104
105
        return $this;
106
    }
107
108
    private function saveAudioOrVideo(string $fullPath): self
109
    {
110
        $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...
111
112
        return $this;
113
    }
114
}
115